我正在尝试将文件移动到同一存储桶中的另一个文件夹中,我的代码没有返回任何错误,但是该文件夹尚未移动。 一个文件夹的名称中有一个空格。
s3 = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
copy_source = {
'Bucket': 'bucket',
'Key': 'config/L C/aa/bb/cc/Lab/xx.tgz'
}
s3.meta.client.copy(copy_source, 'bucket', 'config/L C/aa/bb/cc/Pilot/')
答案 0 :(得分:0)
我知道了,它对我有用。我没有找到直接移动文件的方法,但是先复制粘贴然后删除。
#copy first
s3 = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
copy_source = {
'Bucket': 'bucket',
'Key': 'xx/aa/m.tgz'
}
s3.meta.client.copy(copy_source, 'bucket', 'xx/bb/m.tgz')
#delete the orginal, but assert if the one has been moved
s3 = boto3.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
bucketname='pp'
bucket = s3.Bucket(bucketname)
key = 'xx/tt/m.tgz'
objs = list(bucket.objects.filter(Prefix=key))
if len(objs) > 0 and objs[0].key == key:
print("exist")
# here we delete the previous file
session = boto3.session.Session()
s3s = session.resource('s3', aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
obj = s3s.Object(bucketname, "xx/bb/m.tgz")
obj.delete()