Search code examples
amazon-web-servicesamazon-s3aws-s3-client

how to add metadata to s3 file/object while uploading object programatically?


I have a bucket created via terraform, when file is uploaded to this bucket , can we add metadata to the file/object? if yes, how would i add metatdata to the file/object being uploaded?

import boto3

s3 = boto3.client("s3")

s3.upload_file("local_file.txt", "my-bucket", "test.txt")
resource "aws_s3_bucket" "s3_bucket" {
  bucket = "test-bucket007"
  tags = {
    Name        = "bucket"
    Environment = "test"
  }
}

Solution

  • Like this:

    s3.upload_file(
        "local_file.txt",
        "my-bucket",
        "test.txt",
        ExtraArgs={"Metadata": {"k1": "v1", "k2": "v2"}},
    )