在Heroku(通过powershell)中使用特殊字符设置环境变量



Django 1.8, Heroku, Powershell.

我试图设置一个环境变量为我的django秘钥在Heroku:

(venv) PS WORKFOLDER> heroku config:set SECRET_KEY=eoik6-&dnr9elgmrt7-%3hu_&37$3hg!9c6x!^khjr3!z*z&b4

我得到这个错误消息(3次-因为我在字符串中有3个&号):

At line:1 char:77
+ heroku config:set SECRET_KEY=eoik6-&dnr9elgmrt7-%3hu_&37$3hg!9c6x!^khjr3!z*z&b4
+                                                                             ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double
quotation marks ("&") to pass it as part of a string.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

当我尝试建议的解决方案时(在&号周围加上双引号):

(venv) PS WORKFOLDER> heroku config:set SECRET_KEY=eoik6-"&"dnr9elgmrt7-%3hu_"&"37$3hg!9c6x!^khjr3!z*z"&"b4

我得到这个错误:

SECRET_KEY: eoik6-
'dnr9elgmrt7-%3hu_' is not recognized as an internal or external command, operable program or batch file.
'37$3hg!9c6x!^khjr3!z*z' is not recognized as an internal or external command, operable program or batch file.
'b4' is not recognized as an internal or external command, operable program or batch file.

我也试过用斜杠转义,在整个字符串周围加上引号,等等。相同的结果。那么如何设置环境变量呢?

我刚刚尝试了一些事情,当你在整个VALUE周围加上单引号(并在与号周围加上双引号)时,它会起作用:

heroku config:set SECRET_KEY='eoik6-"&"dnr9elgmrt7-%3hu_"&"37$3hg!9c6x!^khjr3!z*z"&"b4'

可通过命令heroku config

验证

我不熟悉Heroku,但假设heroku是可执行的,config:set SECRET_KEY=eo...是一个命令行参数,你可以试试这个:

& 'heroku' @('config:set', 'SECRET_KEY=eoik6-"&"dnr9elgmrt7-%3hu_"&"37$3hg!9c6x!^khjr3!z*z"&"b4')

还有一个字符可能会引起问题,')'。它产生了一个类似的错误:x was unexpected at this time.幸运的是,修复方法与使用&号完全相同——只需在它周围加上双引号就可以解决问题。工作溶液示例:

heroku config:set SECRET_KEY='7d"("5r9"^"dfghfd=fsdghw%9$")"zz")"yer68"^"346k-4_qndr%!c4h=r'
编辑:另一个有问题的字符,'^'。这很难发现,因为powershell只是从SECRET_KEY中省略了它。也需要用双引号括起来

最新更新