问题将字典传递到Fargate Docker容器中

发布于 2025-02-11 09:38:21 字数 1029 浏览 2 评论 0原文

我有一个容器,该容器在本地运行良好,该容器由一个简单的Python脚本组成,该脚本接受一个字典,该字典转换为系统参数,并打印出键/值对:

Calc.py:

import sys
import json
if __name__ == "__main__":
    print("Entered in main")
    print(f"Function name: {sys.argv[0]}")
    #load
    print(sys.argv[1])
    payload = json.loads(sys.argv[1])
    for k,v in payload.items():
        print(f"Key: {k},Value: {v}")

我使用带有docker容器的入门点,使用带有docker的键盘,环境变化:

   ENTRYPOINT python3 calc.py "${LAMBDA_PAYLOAD}"

这使我可以运行一个测试Docker脚本

docker run -e LAMBDA_PAYLOAD="{\"names\":\"J.J. Robichard\",\"Age\":25}" calc

,该脚本成功地打印出键/值对。

当我使用boto3将其放入AWS ECS FARGATE任务中时,并通过使用以下方式覆盖环境变量:

'environment': [
      {
       'name': 'LAMBDA_PAYLOAD',
        'value': "{\"names\":[\"J.J. Robichard\",\"April\"],\"years\":[25,29]}"
       },
   ],

我将print(sys.argv [1])返回:

{"names":["J.J.

一切都会崩溃。我如何获得接受带有空间的字符串的任务?我已经尝试了“ \”和f“”“ {}”“”,但是什么都没有起作用。

I have a container that runs fine locally consisting of a simple python script that accepts a dictionary converted to a string as a system argument and prints out the key/value pairs:

calc.py:

import sys
import json
if __name__ == "__main__":
    print("Entered in main")
    print(f"Function name: {sys.argv[0]}")
    #load
    print(sys.argv[1])
    payload = json.loads(sys.argv[1])
    for k,v in payload.items():
        print(f"Key: {k},Value: {v}")

I build this as a docker container using an entrypoint with an environmental varible:

   ENTRYPOINT python3 calc.py "${LAMBDA_PAYLOAD}"

This allows me to run a test docker script of

docker run -e LAMBDA_PAYLOAD="{\"names\":\"J.J. Robichard\",\"Age\":25}" calc

which successfully prints out the key/value pairs.

When I put this into an AWS ECS Fargate task using boto3 and overriding the environmental variable by using:

'environment': [
      {
       'name': 'LAMBDA_PAYLOAD',
        'value': "{\"names\":[\"J.J. Robichard\",\"April\"],\"years\":[25,29]}"
       },
   ],

I get the print(sys.argv[1]) returns:

{"names":["J.J.

and everything breaks down. How do I get the task to accept a string with space? I've tried "\ " and f"""{}""", but nothing is working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2025-02-18 09:38:21

作为解决方法,我能够通过以下编码来传递字典:

#inital dictionary
payload = {"names":"J.J. Robichard","April","years":[25,29]}
#converted to string
payload_s = json.dumps(payload).replace("\"","\\\"")
#this was input into the boto3 client as the value

在另一侧,我必须这样做:

input_1 = sys.argv[1]
payload = json.loads(input_1.replace("\\\"", "\""))

As a workaround, I was able to pass the dictionary through by encoding it as follows:

#inital dictionary
payload = {"names":"J.J. Robichard","April","years":[25,29]}
#converted to string
payload_s = json.dumps(payload).replace("\"","\\\"")
#this was input into the boto3 client as the value

on the other side, I had to do this:

input_1 = sys.argv[1]
payload = json.loads(input_1.replace("\\\"", "\""))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文