首页 > 解决方案 > 如何在 put_object 中指定标记?

问题描述

我需要使用 put_object 指定标记,如下所示:

s3client = boto3.client('s3')

response = s3client.put_object(Bucket = dest_bucket_name, Body = body, Key = dest_key, Tagging = tagging, ACL = acl )

我的问题是我使用什么数据结构来标记变量?

我见过各种各样的例子,但我无法让它们发挥作用。例如:

tagging = {'TagSet' : [
            {
                'Key': 'voiceid',
                'Value': polly_voice
            },
            {
                'Key': 'author',
                'Value': book_author
            },
            . . .
          ]
     }

给我

 "errorMessage": "Parameter validation failed:\nInvalid type for parameter Tagging, value: {'TagSet': [{'Key': 'voiceid', 'Value': 'Matthew'}, {'Key': 'author', 'Value': 'some guy'},...]}, type: <class 'dict'>, valid types: <class 'str'>"

我看到它需要一个字符串,但是如何指定键:值对?我发现的所有示例都使用了一些特定的数据结构,例如我的示例。

我怀疑这有什么不同,但我在 lambda 函数中这样做。

标签: pythonpython-3.xamazon-web-servicesamazon-s3boto3

解决方案


要使其正常工作,请尝试以下代码:

tags = 'voiceid={}&author={}'.format(polly_voice, book_author)
s3client = boto3.client('s3')
response = s3client.put_object(
    Bucket=dest_bucket_name,
    Body=body,
    Key=dest_key,
    Tagging=tags
)

您也可以参考这个stackoverflow 答案。


推荐阅读