【问题标题】:Support for object level Tagging in boto3 upload_file method支持 boto3 upload_file 方法中的对象级标记
【发布时间】:2019-05-28 21:12:47
【问题描述】:

我想在将文件上传到 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

我找到了一种方法来直接使用 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'})

尽管这可行,但我认为这不是最好的方法。它可能会产生其他副作用。目前我无法找到正确的方法来实现这一目标。任何建议都会很棒。谢谢。

【问题讨论】:

    标签: python amazon-web-services amazon-s3 boto3 awss3transfermanager


    【解决方案1】:

    Tagging 指令现在由boto3 支持。您可以执行以下操作来添加标签:

    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)},
    )
    

    【讨论】:

      【解决方案2】:

      S3 Customization Reference — Boto 3 Docs 文档将extra_args 的有效值列为:

      ALLOWED_UPLOAD_ARGS = ['ACL'、'CacheControl'、'ContentDisposition'、'ContentEncoding'、'ContentLanguage'、'ContentType'、'Expires'、'GrantFullControl'、'GrantRead'、'GrantReadACP'、'GrantWriteACP'、 “元数据”、“RequestPayer”、“ServerSideEncryption”、“StorageClass”、“SSECustomerAlgorithm”、“SSECustomerKey”、“SSECustomerKeyMD5”、“SSEKMSKeyId”、“WebsiteRedirectLocation”]

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

      您可能需要在创建对象后调用put_object_tagging() 来添加标签。

      【讨论】:

        猜你喜欢
        • 2020-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        • 2011-05-13
        相关资源
        最近更新 更多