user3819236
user3819236

Reputation: 189

Support for object level Tagging in boto3 upload_file method

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

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.

Upvotes: 6

Views: 4920

Answers (2)

user3099576
user3099576

Reputation:

Tagging directive is now supported by boto3. You can do the following to add tags:

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

Upvotes: 7

John Rotenstein
John Rotenstein

Reputation: 270184

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']

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

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

Upvotes: 0

Related Questions