[英]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:您的代码将:
list_objects_v2()
to obtain a list of the objects in the bucketlist_objects_v2()
获取桶中的对象列表head_object()
, which returns metadata
head_object()
,它返回metadata
metadata
for the tagsmetadata
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.