【问题标题】:Running an AWS cli command from a local python script? [duplicate]从本地 python 脚本运行 AWS cli 命令? [复制]
【发布时间】:2016-10-25 17:16:35
【问题描述】:

我正在尝试运行这个 aws s3 ls 命令:

aws s3 ls s3://path/to/my/bucket/12434 --recursive --human-readable --summarize

用这条蟒蛇:

command = 'aws s3 ls s3://path/to/my/bucket/12434 --recursive --human-readable --summarize'
s3_folder_data  = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
print s3_folder_data

但它失败并出现此错误:

subprocess.CalledProcessError: Command 'aws s3 ls s3://path/to/my/bucket/12434 --recursive --human-readable --summarize' returned non-zero exit status 1

当我运行它时,该命令本身就起作用了。同一台机器上的同一用户正在调用 python 脚本。什么给了?

【问题讨论】:

  • 有一个用于aws的python api叫做boto2,用它代替
  • 谢谢,我知道了。但这应该是一个快速而肮脏的脚本。如果我可以使用 CLI,我不想经历配置 boto 的麻烦。
  • 没有问题,就是这样,使用起来非常简单,顺便看看我链接的另一个问题
  • 你在开玩笑吗? get_bucket() 并列出它

标签: python amazon-web-services amazon-s3 aws-cli


【解决方案1】:

按照其他人的建议,使用Boto3 S3 库来获得你想要的东西。但是如果你坚持subprocess,试试:

subprocess.check_output(['aws', 's3', 'ls', 's3://path/to/my/bucket/12434', '--recursive', '--human-readable', '--summarize'])

subprocess.call(['aws', 's3', 'ls', 's3://path/to/my/bucket/12434', '--recursive', '--human-readable', '--summarize'])

并以此为基础。

【讨论】:

    【解决方案2】:

    Python 3.5 新增,也可以使用subprocess.run()

    subprocess.run(['aws', 's3', 'ls', 's3://path/to/my/bucket/12434', '--recursive', '--human-readable', '--summarize'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      相关资源
      最近更新 更多