简体   繁体   English

在 AWS S3 存储桶中获取文件的标签

[英]Getting files' tag in AWS S3 bucket

I have a bucket in AWS S3.我在 AWS S3 中有一个存储桶。 Lets call it MY_BUCKET.让我们称之为 MY_BUCKET。

I would like to iterate the files in this bucket (with Python) and get the tags keys and values.我想迭代这个存储桶中的文件(使用 Python)并获取标签键和值。

How do I do it?我该怎么做?

Code to get tags :获取标签的代码:

s3_resource = boto3.resource('s3')
client = boto3.client('s3')
bucket = s3_resource.Bucket("MY_BUCKET")

for name in bucket.objects.all() :
    response = client.get_object_tagging (
        Bucket = bucket.name,
        Key = name.key
)
print(name.key, response["TagSet"], sep = "\t")

First, it retrieves all objects of the bucket using bucket.objects.all() and for each object, call get_object_tagging which returns dictionary which contains Tagset of the object.首先,它使用bucket.objects.all()检索桶的所有对象,对于每个对象,调用get_object_tagging返回包含对象标签集的字典。

Thanks, Hope it helps !!谢谢,希望有帮助!!

Your code would:您的代码将:

The above functions use the client method of calling S3.以上函数使用调用S3的client方法。 You could probably do it with the resource method too if that is your preference.如果这是您的偏好,您也可以使用resource方法来做到这一点。

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

相关问题 仅从 S3 存储桶获取文件名而不下载文件 - Getting only filenames from S3 bucket without downloading files 将文件复制到不同的 aws s3 存储桶 Boto3 后,AWS 无法打开或下载 - AWS cannot open or download after copy files to different aws s3 bucket Boto3 从 AWS S3 存储桶读取缓慢 - Slow reading from AWS S3 bucket 使用 boto3 lib 和 AWS Lambda 从位于 S3 存储桶中的压缩文件中获取数据流 - Getting a data stream from a zipped file sitting in a S3 bucket using boto3 lib and AWS Lambda 使用boto3(python 3.x)将文件上传到AWS S3存储桶中以保持文件结构? - Uploading files to aws s3 bucket with boto3(python 3.x) maintaining the file structure? 如何运行AWS S3存储桶中的python脚本和文件 - How do I run a python script and files located in an aws s3 bucket Python从s3存储桶读取文件 - Python read files from s3 bucket 如何使用可用于 aws lambda 函数的 goofys 将 s3 存储桶上的文件安装到 aws ec2 实例? - How can I make files on s3 bucket mounted to aws ec2 instance using goofys available to aws lambda function? 我正在尝试使用 python 从 aws 下载整个 s3 存储桶,但出现 OSError[error22] - I am trying to down load the entire s3 bucket from aws using python but getting a OSError[error22] 使用python boto3将文件从一个S3存储桶传输到另一个S3存储桶 - Transfer files from one S3 bucket to another S3 bucket using python boto3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM