由于AWS S3 暂时没有直接重命名的接口.所以我们只能通过先用新名字复制文件.然后再删除原文件的方法来达到重名的的效果.
运行脚本需要本地先配置好AWS CLI
- 首先遍历出你需要重命名的存储桶下面所有文件的key.我在网上找到了一个很好的例子 https://alexwlchan.net/2017/07/listing-s3-keys/ 这里我稍微引用下里面的方法.
# -*- coding: utf-8 -*-
import boto3
# 这里输入你存储桶的名字
BUCKET = 'bucket'
s3_resource = boto3.resource('s3')
s3 = boto3.client('s3')
# 这个方法可以过滤出 某个存储桶下面的所有key
def get_all_s3_keys(bucket):
"""Get a list of all keys in an S3 bucket."""
keys = []
s3_client = boto3.client('s3')
kwargs = {'Bucket': bucket}
while True:
resp = s3_client.list_objects_v2(**kwargs)
for obj in resp['Contents']:
keys.append(obj['Key'])
try:
kwargs['ContinuationToken'] = resp['NextContinuationToken']
except KeyError:
break
return keys
# 这里是一个迭代器.而且可以过滤出你想要的对应的前缀和后缀的key
def get_matching_s3_keys(bucket, prefix='', suffix=''):
"""
Generate the keys in an S3 bucket.
:param bucket: Name of the S3 bucket.
:param prefix: Only fetch keys that start with this prefix (optional).
:param suffix: Only fetch keys that end with this suffix (optional).
"""
kwargs = {'Bucket': bucket, 'Prefix': prefix}
while True:
resp = s3.list_objects_v2(**kwargs)
for obj in resp['Contents']:
key = obj['Key']
if key.endswith(suffix):
yield key
try:
kwargs['ContinuationToken'] = resp['NextContinuationToken']
except KeyError:
break
- 上面的代码帮我们遍历出来了所有key,然后利用boto3提供的copy()方法复制文件. 其实copy_object()也能复制,但是copy_object()如果复制单个文件大于5G 会报错 ,所以我么你这里选择copy()
copy_source = {
'Bucket': 'mybucket',
'Key': 'mykey'
}
s3_resource.meta.client.copy(copy_source, 'newrbucket', 'new_file_key')
- 最后用delete()方法删除原有文件.
s3_resource.Object('my_bucket','old_file_key').delete()