[英]update metadata in s3
I use Amazon boto v2.38 to write python script to access my S3 bucket. 我使用Amazon boto v2.38编写python脚本来访问我的S3存储桶。
I want to update a file in my bucket (I know it is called " key " in S3). 我想更新存储桶中的文件(我知道它在S3中称为“ key ”)。 The path is MyBucket/myfile/demo.txt
. 路径是MyBucket/myfile/demo.txt
。 Besides, I want to also update its metadata named " name
". 此外,我还想更新其名为“ name
”的元数据 。 Here is the code I tried: 这是我尝试的代码:
# connect to key
conn = boto.connect_s3()
bucket = conn.get_bucket("MyBucket")
my_key = Key(bucket)
my_key.key = "myfile/demo.txt"
# if key exists, then update the file & its metadata
if my_key.exists():
new_meta_data = {"name": "xyz"}
# update metadata
my_key.copy("MyBucket", my_key.name, new_meta_data, preserve_acl=True)
# update file content in S3 by using the local file demo.txt
my_key.set_contents_from_filename("demo.txt")
However, it doesn't work... I don't see metadata get updated. 但是,它不起作用...我看不到元数据得到更新。 Why? 为什么?
You can just update the key's local metadata and then perform the file update: 您可以只更新密钥的本地元数据,然后执行文件更新:
import boto
conn = boto.connect_s3()
bucket = conn.get_bucket("MyBucket")
key = bucket.get_key('myfile/demo.txt')
key.set_metadata('name', 'xyz')
key.set_contents_from_filename('demo.txt')
Now name
should appear as metadata within S3. 现在, name
应在S3中显示为元数据。 Note, however, that the ACL might change when you do this. 但是请注意,执行此操作时ACL可能会更改。
It can also be done with key.set_remote_metadata()
. 也可以使用key.set_remote_metadata()
。 This does not require that you update the key's content (but you can if you want): 这不需要您更新密钥的内容(但是您可以根据需要):
conn = boto.connect_s3()
bucket = conn.get_bucket('MyBucket')
key = bucket.get_key('myfile/demo.txt')
key.set_remote_metadata({'name': 'xyz'}, {}, True)
The following code change the key metadata in boto3
: 以下代码更改boto3
的键元数据:
import boto3 as aws
s3 = aws.resource('s3')
obj = s3.Bucket('MyBucket').Object('objectKey')
obj.put(Metadata={'name':'newName'}
According to the docs , set_metadata
must be used. 根据docs ,必须使用set_metadata
。 I have tested it and the foloowing code works with boto2
and changes the meatadata: 我已经对其进行了测试,下面的代码可与boto2
配合boto2
并更改meatadata:
import boto as aws
cx=aws.connect_s3()
bucket=cx.get_bucket('MyBucket')
obj=bucket.get_key('objectKey')
obj.set_metadata('name', 'newName')
Using Boto3, be careful if using "put_object" with metadata, this will change your actual metadatas, if you want to to create Object with metadatas, then add metadata or update an existing metadata, use the below : 使用Boto3时,请注意如果将“ put_object”与元数据一起使用,则会更改您的实际元数据,如果要使用元数据创建Object,然后添加元数据或更新现有的元数据,请使用以下方法:
import sys
import os
import boto3
from boto3 import client
param_1= sys.argv[1]
param_2= sys.argv[2]
param_3= sys.argv[3]
param_4= sys.argv[4]
param_5= sys.argv[5]
objectNMAE='THIEF.jpg'
s3ressource = client(
service_name='s3',
endpoint_url= param_3,
aws_access_key_id= param_1,
aws_secret_access_key=param_2,
use_ssl=True,
)
def createmetdata(bucketname,objectname):
s3ressource.upload_file(objectname, bucketname, objectname, ExtraArgs={"Metadata": {"metadata1":"ImageName","metadata2":"ImagePROPERTIES" ,"metadata3":"ImageCREATIONDATE"}})
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')
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({'metadata3':'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
createmetdata(param_4,objectNMAE)
readmetadata(param_4,objectNMAE)
ADDmetadata(param_4,objectNMAE)
readmetadata(param_4,objectNMAE)
CHANGEmetadata(param_4,objectNMAE)
readmetadata(param_4,objectNMAE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.