问题
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))
回答1:
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"}})
回答2:
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()
.
来源:https://stackoverflow.com/questions/33771318/boto3-how-to-create-object-with-metadata