use*_*236 6 python amazon-s3 amazon-web-services awss3transfermanager boto3
我想在将文件上传到 S3 时为其添加标签。Boto3 支持使用 put_object 方法指定标签,但是考虑到预期的文件大小,我使用 upload_file 函数来处理分段上传。但此函数拒绝“标记”作为关键字参数。
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
Run Code Online (Sandbox Code Playgroud)
我找到了一种方法来完成这项工作,直接使用 S3 传输管理器并修改允许的关键字列表。
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'})
Run Code Online (Sandbox Code Playgroud)
尽管这有效,但我认为这不是最好的方法。它可能会产生其他副作用。目前我无法找到正确的方法来实现这一目标。任何建议都会很棒。谢谢。
小智 7
Taggingboto3现在支持指令。您可以执行以下操作来添加标签:
import boto3
from urllib import parse
s3 = boto3.client("s3")
tags = {"key1": "value1", "key2": "value2"}
s3.upload_file(
"file_path",
"bucket",
"key",
ExtraArgs={"Tagging": parse.urlencode(tags)},
)
Run Code Online (Sandbox Code Playgroud)
S3 Customization Reference \xe2\x80\x94 Boto 3 Docs文档列出了以下有效值extra_args:
\n\n\nALLOWED_UPLOAD_ARGS = [\'ACL\'、\'CacheControl\'、\'ContentDisposition\'、\'ContentEncoding\'、\'ContentLanguage\'、\'ContentType\'、\'Expires\'、\'GrantFullControl\' , \'GrantRead\', \'GrantReadACP\', \'GrantWriteACP\', \'元数据\', \'RequestPayer\', \'ServerSideEncryption\', \'StorageClass\', \'SSECustomerAlgorithm\', \ 'SSECustomerKey\'、\'SSECustomerKeyMD5\'、\'SSEKMSKeyId\'、\'WebsiteRedirectLocation\']
\n
因此,这似乎不是指定标签的有效方法。
\n\n看来您可能需要put_object_tagging()在创建对象后调用来添加标签。
| 归档时间: |
|
| 查看次数: |
3914 次 |
| 最近记录: |