ChatGPT 帮助之 S3 移动对象

问题

如何将 s3://foobar/expense/2023/12/02 00:00:00/2023-12-02 00:00:00.json 这个路径移动为 s3://foobar/expense/2023/12/02/2023-12-02.json,并将2023-12-01 到今天的全部处理,最少python脚本实现

回答

要将 S3 中的对象从一个键移动到另一个键,您需要执行一个复制操作,然后删除原始对象。在AWS中,移动并不是一个原子操作,而是由复制(COPY)和删除(DELETE)操作组成的。

下面是一个Python脚本示例,它使用boto3库(这是AWS的官方Python SDK)来移动指定日期范围内的文件。这个脚本会遍历给定日期范围内的每一天,构建原始和目标键,并执行必要的复制和删除操作。

安装boto3库(如果尚未安装):

1
pip install boto3

Python脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import boto3
import datetime

# 初始化S3客户端
s3_client = boto3.client('s3')

# 定义S3桶名称
bucket_name = 'foobar'

# 设定日期范围
start_date = datetime.datetime(2023, 12, 1)
end_date = datetime.datetime.now()

# 遍历日期范围
current_date = start_date
while current_date <= end_date:
# 构建原始和目标键
original_key = f'expense/{current_date.year}/{current_date.month:02d}/{current_date.day:02d} 00:00:00/{current_date.strftime("%Y-%m-%d")} 00:00:00.json'
new_key = f'expense/{current_date.year}/{current_date.month:02d}/{current_date.day:02d}/{current_date.strftime("%Y-%m-%d")}.json'

# 复制对象
copy_source = {
'Bucket': bucket_name,
'Key': original_key
}
try:
s3_client.copy(copy_source, bucket_name, new_key)
print(f'Copied: {original_key} to {new_key}')

# 删除原始对象
s3_client.delete_object(Bucket=bucket_name, Key=original_key)
print(f'Deleted: {original_key}')
except s3_client.exceptions.NoSuchKey:
print(f'No such key: {original_key}')
except Exception as e:
print(f'Error: {e}')

# 移至下一天
current_date += datetime.timedelta(days=1)

请注意,这个脚本没有处理可能的分页。如果您有很多文件,您可能需要处理S3的分页响应。另外,请确保您的AWS凭证已经配置在环境变量中,或者您已经在.aws/credentials文件中配置了它们,否则您需要在创建boto3.client时手动提供它们。

在运行此脚本之前,请确保您有足够的权限来执行这些操作,并且您已经备份了重要数据,防止意外丢失。