【问题标题】:How to get contents of an AWS S3 bucket programmatically in Python如何在 Python 中以编程方式获取 AWS S3 存储桶的内容
【发布时间】:2020-01-29 18:42:33
【问题描述】:

我可以在终端中成功运行命令 aws --profile minio s3 ls s3://aa/bb/ 以获取 minio 上特定存储桶的内容,但是当我在 Python 中运行以下代码时,它返回一个空字符串。

import os
stream = os.popen('aws --profile minio s3 ls s3://aa/bb/')
stream.read()

当我更改第二行以便查询 本地 文件夹的内容时,例如 stream = os.popen('ls /Users/cc/'),该本地文件夹的内容也会成功打印。

当我使用os.system('aws --profile minio s3 ls s3://aa/bb/') 执行第一个命令时,它会返回256 作为输出。

那么如何在 Python 中以编程方式访问 minio 存储桶的内容?

【问题讨论】:

  • this 是否相关/相关?
  • 是的,完美,谢谢!

标签: python command-line minio


【解决方案1】:

需要注意的是,我没有使用过 minio,以下是我如何在 python 脚本中使用 boto3(AWS python sdk)来执行 CLI 命令的操作:

import boto3

session = boto3.session.Session(profile_name='minio')
client = session.client('s3')

response = client.list_objects_v2(
    Bucket='aa',
    Prefix='bb',
)

for item in response['Contents']:
    print(item['Key'])

boto3 on GitHub

boto3 docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    相关资源
    最近更新 更多