6

Is there a way to upload file to AWS S3 with Tags(not add Tags to an existing File/Object in S3). I need to have the file appear in S3 with my Tags , ie in a single API call.

I need this because I use a Lambda Function (that uses these S3 object Tags) is triggered by S3 ObjectCreation

mahes
  • 1,074
  • 3
  • 12
  • 20

3 Answers3

11

You can inform the Tagging attribute on the put operation.

Here's an example using Boto3:

import boto3

client = boto3.client('s3')

client.put_object(
    Bucket='bucket', 
    Key='key',
    Body='bytes', 
    Tagging='Key1=Value1'
)

As per the docs, the Tagging attribute must be encoded as URL Query parameters. (For example, "Key1=Value1")

Tagging — (String) The tag-set for the object. The tag-set must be encoded as URL Query parameters. (For example, "Key1=Value1")

EDIT: I only noticed the boto3 tag after a while, so I edited my answer to match boto3's way of doing it accordingly.

Thales Minussi
  • 6,965
  • 1
  • 30
  • 48
  • 3
    Perfect, thank you. This also works with other methods, like upload_fileobj with the parameter `ExtraArgs`. – Tanner May 20 '20 at 14:54
  • 2
    Use `&` as a delimiter between tag values like `"Key1=Value1"&"Key2=Value2"`. If you need to upload without a value its just `"Key1&Key2&Key3"`. – mRyan May 19 '21 at 11:53
  • 1
    There's a little quoting issue in the above example. It should look like this: `Tagging='key1=value1&key2=value2'`. – Sébastien Lavoie May 20 '22 at 20:03
5

Tagging directive is now supported by boto3. You can do the following to add tags if you are using upload_file()

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

If you're uploading a file using client.upload_file() or other methods that have the ExtraArgs parameter, you specify the tags differently you need to add tags in a separate request. You can add metadata as follows, but this is not the same thing. For an explanation of the difference, see this SO question:

import boto3

client = boto3.client('s3')

client.upload_file(
    Filename=path_to_your_file,
    Bucket='bucket', 
    Key='key',
    ExtraArgs={"Metadata": {"mykey": "myvalue"}}
)

There's an example of this on the S3 docs, but you have to know that "metadata" corresponds to tags be aware that metadata is not exactly the same thing as tags though it can function similarly.

s3.upload_file(
    "tmp.txt", "bucket-name", "key-name",
    ExtraArgs={"Metadata": {"mykey": "myvalue"}}
)
leafmeal
  • 1,824
  • 15
  • 15