Boto3: How do I create an S3 object with metadata using boto3?

Created on 18 Nov 2015  路  2Comments  路  Source: boto/boto3

In the example below I want to set a timestamp metadata attribute when created an S3 object. How do I do that? The documentation is not clear.

import uuuid
import json
import boto3
import botocore
import time

from boto3.session import Session
session = Session(aws_access_key_id='XXX',
aws_secret_access_key='XXX')

s3 = session.resource('s3')

bucket = s3.Bucket('blah')

for filename in glob.glob('json/*.json'):
with open(filename, 'rb') as f:
data = f.read().decode('utf-8')
timestamp = str(round(time.time(),10))
my_s3obj = s3.Object('blah', str(uuid.uuid4())).put(Body=json.dumps(data))

question s3

Most helpful comment

Guess you've figured it out. For the sake of helping others, I'm providing a feedback anyway. The Metadata is defined as a dictionary of string-to-string mapping (see documentation). You can use the following code snippet to set it. You can find more information about the Metadata in AWS web console.

import boto3
s3 = boto3.resource('s3')
s3.Object('mybucket', 'myfile.txt').put(Body='hello world', Metadata={'foo': 'bar'})

All 2 comments

Guess you've figured it out. For the sake of helping others, I'm providing a feedback anyway. The Metadata is defined as a dictionary of string-to-string mapping (see documentation). You can use the following code snippet to set it. You can find more information about the Metadata in AWS web console.

import boto3
s3 = boto3.resource('s3')
s3.Object('mybucket', 'myfile.txt').put(Body='hello world', Metadata={'foo': 'bar'})

how to add the log into the end of the file ?
s3.Object('xxx', 'xxxx').put(Body = message) overwrite the first line.
Is there any attribute I missed?
Can you help me?

Was this page helpful?
0 / 5 - 0 ratings