【问题标题】:How to get the URL of all the objects in our aws s3 bucket programmatically using python?如何使用 python 以编程方式获取我们的 aws s3 存储桶中所有对象的 URL?
【发布时间】:2020-10-22 16:47:48
【问题描述】:

我在这里发布这个是因为我发现很难找到使用 python 从我们的 s3 存储桶中获取所有对象的函数。当我试图找到 get_object_data 函数时,我被指示下载对象函数。 那么,我们如何使用 boto3(aws sdk for python)获取我们 AWS s3 存储桶中所有对象的数据?

【问题讨论】:

  • 你有多少个对象?如果是批量,使用 S3 库存可能更有效。
  • 嗨,Marcin,感谢您的阅读:)。这里我不担心数字对象。我想要一种方法来使用 SDK 获取我的 s3 存储桶中对象的 url。当然,S3 清单有助于管理大量对象。
  • 没问题。很高兴它的工作:-)

标签: python amazon-web-services url amazon-s3 boto3


【解决方案1】:
  1. 将 boto3 导入你的 python shell
  2. 连接到您的 AWS 账户并指定您要访问的资源(此处为 s3-bucket)? (确保您提供的 IAM 凭证有权访问该资源)
  3. 获取所需数据

代码看起来像这样

import boto3
s3_resource = boto3.resource(service_name='s3',
                             region_name='<your bucket region>'
                             aws_access_key_id='<your access key id>'
                             aws_secret_access_key='<your secret access key>')
a = s3_resource.Bucket('<your bucket name>') 
for obj in a.objects.all():
    #object URL
    print("https://<your bucket name>.s3.<your bucket region>.amazonaws.com/" + obj.key)
    #if you want to print all the data of object, just print obj

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2011-12-23
    • 1970-01-01
    • 2020-12-01
    • 2021-05-09
    相关资源
    最近更新 更多