2016-09-20 60 views

回答

1

它可以通過copy_from進行()方法 -

import boto3 

s3 = boto3.resource('s3') 
s3_object = s3.Object('bucket-name', 'key') 
s3_object.metadata.update({'id':'value'}) 
s3_object.copy_from(CopySource={'Bucket':'bucket-name', 'Key':'key'}, Metadata=s3_object.metadata, MetadataDirective='REPLACE') 
0

您可以通過添加一些或更新一個新的電流元數據值或者更新元數據,這裏是一段代碼,我使用:

import sys 
import os 
import boto3 
import pprint 
from boto3 import client 
from botocore.utils import fix_s3_host 
param_1= YOUR_ACCESS_KEY 
param_2= YOUR_SECRETE_KEY 
param_3= YOUR_END_POINT 
param_4= YOUR_BUCKET 

#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, 
    ) 
# Building a list of of object per bucket 
def BuildObjectListPerBucket (variablebucket): 
    global listofObjectstobeanalyzed 
    listofObjectstobeanalyzed = [] 
    extensions = ['.jpg','.png'] 
    for key in s3ressource.list_objects(Bucket=variablebucket)["Contents"]: 
     #print (key ['Key']) 
     onemoreObject=key['Key'] 
     if onemoreObject.endswith(tuple(extensions)): 
      listofObjectstobeanalyzed.append(onemoreObject) 
    #print listofObjectstobeanalyzed 
     else : 
      s3ressource.delete_object(Bucket=variablebucket,Key=onemoreObject)   
    return listofObjectstobeanalyzed 

# for a given existing object, create metadata 
def createmetdata(bucketname,objectname): 
    s3ressource.upload_file(objectname, bucketname, objectname, ExtraArgs={"Metadata": {"metadata1":"ImageName","metadata2":"ImagePROPERTIES" ,"metadata3":"ImageCREATIONDATE"}}) 

# for a given existing object, add new metadata 
def ADDmetadata(bucketname,objectname): 
    s3_object = s3ressource.get_object(Bucket=bucketname, Key=objectname) 
    k = s3ressource.head_object(Bucket = bucketname, Key = objectname) 
    m = k["Metadata"] 
    m["new_metadata"] = "ImageNEWMETADATA" 
    s3ressource.copy_object(Bucket = bucketname, Key = objectname, CopySource = bucketname + '/' + objectname, Metadata = m, MetadataDirective='REPLACE') 

# for a given existing object, update a metadata with new value 
def CHANGEmetadata(bucketname,objectname): 
    s3_object = s3ressource.get_object(Bucket=bucketname, Key=objectname) 
    k = s3ressource.head_object(Bucket = bucketname, Key = objectname) 
    m = k["Metadata"] 
    m.update({'watson_visual_rec_dic':'ImageCREATIONDATEEEEEEEEEEEEEEEEEEEEEEEEEE'}) 
    s3ressource.copy_object(Bucket = bucketname, Key = objectname, CopySource = bucketname + '/' + objectname, Metadata = m, MetadataDirective='REPLACE') 

def readmetadata (bucketname,objectname): 
    ALLDATAOFOBJECT = s3ressource.get_object(Bucket=bucketname, Key=objectname) 
    ALLDATAOFOBJECTMETADATA=ALLDATAOFOBJECT['Metadata'] 
    print ALLDATAOFOBJECTMETADATA 



# create the list of object on a per bucket basis 
BuildObjectListPerBucket (param_4) 

# Call functions to see the results 
for objectitem in listofObjectstobeanalyzed: 
    # CALL The function you want 
    readmetadata(param_4,objectitem) 
    ADDmetadata(param_4,objectitem) 
    readmetadata(param_4,objectitem) 
    CHANGEmetadata(param_4,objectitem) 
    readmetadata(param_4,objectitem) 
相關問題