简体   繁体   English

使用 boto3 返回 s3 前缀中的对象列表

[英]return list of objects in s3 prefix using boto3

I have the boto3 code below.我有下面的boto3代码。 In the code I am trying to get a list of objects in an s3 prefix.在代码中,我试图获取 s3 前缀中的对象列表。 The complete path to the object in the s3 prefix is: s3 前缀中 object 的完整路径是:

path to file:文件路径:

s3://our_data/our_folder/part-00000-bf597559-8a11-4e6b-8631-ef7c138b4583-c000.txt

When I run the code below it doesn't return anything, but I could swear a couple months ago it worked.当我运行下面的代码时,它不会返回任何东西,但我可以发誓几个月前它有效。 Did anything change with boto3 related to this recently, or am I missing something in my code?最近与此相关的 boto3 有什么变化,还是我的代码中遗漏了什么?

code:代码:

import boto3

s3_client = boto3.client('s3',
                         region_name='us-west-2')


trz_bucket = 'our_data'


trz_prefix = 'our_folder/'


result = s3_client.list_objects(Bucket=trz_bucket, Prefix=trz_prefix, Delimiter='v')

result.get('CommonPrefixes')

The CommonPrefixes will be returned if you provide a Delimiter :如果您提供Delimiter ,则将返回CommonPrefixes

list_objects(Bucket=trz_bucket, Prefix=trz_prefix, Delimiter='/')

Here' an example of using Delimiter and CommonPrefixes using the AWS CLI (which would work the same as using boto3):这是一个使用 AWS CLI 使用DelimiterCommonPrefixes的示例(其工作方式与使用 boto3 相同):

$ aws s3 mb s3://test-delimiter
make_bucket: test-delimiter

$ aws s3 cp x.py s3://test-delimiter/folder1/
upload: ./x.py to s3://test-delimiter/folder1/x.py                

$ aws s3 cp x.py s3://test-delimiter/folder2/
upload: ./x.py to s3://test-delimiter/folder2/x.py                

$ aws s3api list-objects --bucket test-delimiter --delimiter '/'
{
    "CommonPrefixes": [
        {
            "Prefix": "folder1/"
        },
        {
            "Prefix": "folder2/"
        }
    ]
}

$ aws s3 cp x.py s3://test-delimiter/folder1/folder3/
upload: ./x.py to s3://test-delimiter/folder1/folder3/x.py        

$ aws s3api list-objects --bucket test-delimiter --delimiter '/' --prefix folder1/
{
    "Contents": [
        {
            "Key": "folder1/x.py",
            "LastModified": "2021-06-05T07:52:35+00:00",
            "ETag": "\"1083f8ddcea96dd6b9d8a3a582373c71\"",
            "Size": 727,
            "StorageClass": "STANDARD"
            }
        }
    ],
    "CommonPrefixes": [
        {
            "Prefix": "folder1/folder3/"
        }
    ]
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 boto3 在 Python 中列出上一小时上传的 S3 对象 - How to list S3 objects uploaded in last hour in Python using boto3 使用 Boto3 Python 列出 AWS S3 存储桶中的所有对象及其存储 class - List all objects in AWS S3 bucket with their storage class using Boto3 Python Boto3:使用boto3.resource('s3')列出所有S3存储桶 - Boto3: Using boto3.resource('s3') to list all S3 buckets 查找 s3 存储桶的 1 级前缀大小,同时包括使用 boto3 和 python 的版本 - finding s3 bucket's level 1 prefix sizes while including versions using boto3 and python 使用boto3列出S3对象时,强制AWS Lambda使用UTC - Force AWS Lambda to use UTC when listing S3 objects using boto3 通过 Boto3 使用 S3 Bucket 中多个文件夹中的所有对象在 Athena 中创建表 - Create table in Athena using all objects from multiple folders in S3 Bucket via Boto3 如何使用 Boto3 按上次修改日期过滤 s3 对象 - How to filter s3 objects by last modified date with Boto3 AWS:使用boto3检索具有交叉账户访问权限的S3存储桶的列表的Python脚本 - AWS : Python Script to Retrieve list of S3 bucket having Cross account access using boto3 如何使用 Python Boto3 列出和读取 S3 存储桶的特定文件夹中的每个文件 - How to list and read each of the files in specific folder of an S3 bucket using Python Boto3 使用 boto3 从给定的列表变量中清空 S3 桶 - Empty S3 buckets from a given list variable using boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM