支持boto3 upload_file方法中的对象级别标记 [英] Support for object level Tagging in boto3 upload_file method

查看:297
本文介绍了支持boto3 upload_file方法中的对象级别标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在将文件上传到S3时向它们添加标签. Boto3支持使用put_object方法指定标签,但是考虑到预期的文件大小,我正在使用用于处理分段上传的upload_file函数.但是此函数拒绝将标记"作为关键字参数.

I want to add tags to the files as I upload them to S3. Boto3 supports specifying tags with put_object method, however considering expected file size, I am using upload_file function which handles multipart uploads. But this function rejects 'Tagging' as keyword argument.

import boto3
client = boto3.client('s3', region_name='us-west-2')
client.upload_file('test.mp4', 'bucket_name', 'test.mp4',
                   ExtraArgs={'Tagging': 'type=test'})

ValueError: Invalid extra_args key 'Tagging', must be one of: ACL, CacheControl, ContentDisposition, ContentEncoding, ContentLanguage, ContentType, Expires, GrantFullControl, GrantRead, GrantReadACP, GrantWriteACP, Metadata, RequestPayer, ServerSideEncryption, StorageClass, SSECustomerAlgorithm, SSECustomerKey, SSECustomerKeyMD5, SSEKMSKeyId, WebsiteRedirectLocation

我找到了一种直接使用S3传输管理器并修改允许的关键字列表的方法.

I found a way to make this work by using S3 transfer manager directly and modifying allowed keyword list.

from s3transfer import S3Transfer
import boto3

client = boto3.client('s3', region_name='us-west-2')
transfer = S3Transfer(client)
transfer.ALLOWED_UPLOAD_ARGS.append('Tagging')
transfer.upload_file('test.mp4', 'bucket_name', 'test.mp4',
                     extra_args={'Tagging': 'type=test'})

即使这可行,我也不认为这是最好的方法.它可能会产生其他副作用.目前,我无法找到实现此目标的正确方法.任何建议都很好.谢谢.

Even though this works, I don't think this is the best way. It might create other side effects. Currently I am not able to find correct way to achieve this. Any advice would be great. Thanks.

推荐答案

The S3 Customization Reference — Boto 3 Docs documentation lists valid values for extra_args as:

ALLOWED_UPLOAD_ARGS = ['ACL','CacheControl','ContentDisposition','ContentEncoding','ContentLanguage','ContentType','Expires','GrantFullControl','GrantRead','GrantReadACP','GrantWriteACP', 'Metadata','RequestPayer','ServerSideEncryption','StorageClass','SSECustomerAlgorithm','SSECustomerKey','SSECustomerKeyMD5','SSEKMSKeyId','WebsiteRedirectLocation']

ALLOWED_UPLOAD_ARGS = ['ACL', 'CacheControl', 'ContentDisposition', 'ContentEncoding', 'ContentLanguage', 'ContentType', 'Expires', 'GrantFullControl', 'GrantRead', 'GrantReadACP', 'GrantWriteACP', 'Metadata', 'RequestPayer', 'ServerSideEncryption', 'StorageClass', 'SSECustomerAlgorithm', 'SSECustomerKey', 'SSECustomerKeyMD5', 'SSEKMSKeyId', 'WebsiteRedirectLocation']

因此,这似乎不是指定标签的有效方法.

Therefore, this does not appear to be a valid way to specify a tag.

似乎您可能需要致电 put_object_tagging() 创建对象后添加标签.

It appears that you might need to call put_object_tagging() to add the tag(s) after creating the object.

这篇关于支持boto3 upload_file方法中的对象级别标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆