我使用Amazon boto v2.38编写python脚本来访问我的S3存储桶。
我想更新我的存储桶中的文件(我知道它在S3中称为“密钥”)。路径为<TreeView x:Name="tvMain" Grid.Row="2">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding rsParentChild}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding production_year}" />
<TextBlock Text="{Binding original_title}" Margin="10,0,0,0"/>
<TextBlock x:Name="program_id" Text="{Binding program_id}" Margin="10,0,0,0" Visibility="Hidden"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
。此外,我还要更新名为“MyBucket/myfile/demo.txt
”的元数据。这是我试过的代码:
name
然而,它不起作用......我没有看到元数据得到更新。为什么呢?
答案 0 :(得分:5)
您只需更新密钥的本地元数据,然后执行文件更新:
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')
现在name
应该在S3中显示为元数据。但请注意,执行此操作时ACL可能会更改。
也可以使用key.set_remote_metadata()
完成。这不要求您更新密钥的内容(但如果需要,可以使用):
conn = boto.connect_s3()
bucket = conn.get_bucket('MyBucket')
key = bucket.get_key('myfile/demo.txt')
key.set_remote_metadata({'name': 'xyz'}, {}, True)
答案 1 :(得分:0)
以下代码更改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
。我测试了它,并且下面的代码与boto2
一起使用并更改了元数据:
import boto as aws
cx=aws.connect_s3()
bucket=cx.get_bucket('MyBucket')
obj=bucket.get_key('objectKey')
obj.set_metadata('name', 'newName')
答案 2 :(得分:0)
使用Boto3时,请注意使用&#34; put_object&#34;使用元数据,这将改变您的实际元数据,如果您想使用元数据创建对象,然后添加元数据或更新现有元数据,请使用以下内容:
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)