12

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))
Duke Dougal
  • 21,281
  • 28
  • 79
  • 112

2 Answers2

15

As for boto3, You have the upload_file possibility detailed in the boto3 website here .

import boto3
#Create the S3 client
s3ressource = client(
    service_name='s3', 
    endpoint_url= param_3,
    aws_access_key_id= param_1,
    aws_secret_access_key=param_2,
    use_ssl=True,
    )

uploading a file, you have to specify the key ( which is basically your robject/file name), and adding metadata when creating the key would be done using the "ExtraArgs" option :

s3ressource.upload_file(Filename, bucketname, key, ExtraArgs={"Metadata": {"metadata1":"ImageName","metadata2":"ImagePROPERTIES" ,"metadata3":"ImageCREATIONDATE"}})
MouIdri
  • 1,120
  • 1
  • 15
  • 37
  • 5
    Great answer- one minor addition: If you want to modify one of the defined Object metadata keys (Content-Encoding, Content-Type, etc.) you must use them directly in `ExtraArgs` (i.e. `ExtraArgs={"ContentEncoding": "gzip"}`) – Devin Cairns Jan 25 '19 at 21:26
  • Yes, it depends on the server... AWS has 2KB while onpremises system can have more. http://docs.netapp.com/sgws-112/topic/com.netapp.doc.sg-s3/GUID-0BEF8D16-B743-4BF6-8E27-A5FE4D33C1F0.html – MouIdri Mar 12 '21 at 14:41
  • I tried editing this answer but it's unclear to me why the python code coloring doesnt work! – Tommy Jun 16 '21 at 13:10
8

You can specify metadata for the object as key-value pairs like this:

s3.Object('bucket-name', 'uuid-key-name').put(Body='data', 
                                              Metadata={'key-name':'value'})

See the boto3 docs for other parameters you can use inside put().

David Morales
  • 860
  • 7
  • 14