You need to enable JavaScript to run this app. AWS S3跨账号迁移:如何筛选增量文件复制到EBS并解决排序问题
优惠活动
大模型
产品
解决方案
定价
更多

AWS S3跨账号迁移:如何筛选增量文件复制到EBS并解决排序问题

AWS S3跨账号批量迁移解决方案

完全可以通过awk、sort等命令行工具实现按文件名数值段排序的增量迁移,且必须将文件名中的数值字段转换为整数处理,才能解决S3默认字典序的排序错误问题。

前置步骤:生成有序待迁移文件列表

1. 拉取源桶中所有符合my_file_0_1_*规则的文件名

aws s3 ls s3://my_source_bucket_name/my_folder/ --profile source_user | grep "my_file_0_1_" | awk '{print $4}' > all_0_1_files.txt

2. 按文件名中c位的数值大小排序

通过awk拆分文件名提取c段数值,用sort -n按整数排序,避免字典序导致10排在2之前的问题:

awk -F'[_.]' '{print $5, $0}' all_0_1_files.txt | sort -n | awk '{print $2}' > sorted_0_1_files.txt

3. 过滤已完成迁移的文件

拉取目标桶中已存在的my_file_0_1_*文件名,排除后得到待迁移列表:

# 拉取目标桶已完成文件列表
aws s3 ls s3://my_target_bucket_name/my_folder/ --profile source_user | grep "my_file_0_1_" | awk '{print $4}' > completed_0_1_files.txt
# 过滤得到待迁移列表
grep -vxFf completed_0_1_files.txt sorted_0_1_files.txt > todo_0_1_files.txt

迁移方案(二选一即可)

方案1:直接跨桶复制(推荐,完全不占用本地EBS空间)

只要你使用的账号同时拥有源桶读权限、目标桶写权限,就可以直接在两个S3路径之间复制,文件不会写入本地磁盘,完全规避EBS容量限制:

#!/bin/bash
while read filename; do
    aws s3 cp "s3://my_source_bucket_name/my_folder/$filename" "s3://my_target_bucket_name/my_folder/$filename" --profile source_user
done < todo_0_1_files.txt

方案2:分批拉取本地再上传(仅适用于无法直接跨桶复制的场景)

根据单个文件大小调整批次大小,确保单批次总大小不超过100GiB即可,示例中每批处理50个文件:

#!/bin/bash
BATCH_SIZE=50
TOTAL=$(wc -l < todo_0_1_files.txt)
for ((i=0; i<TOTAL; i+=BATCH_SIZE))
do
    # 切分当前批次文件列表
    sed -n "$((i+1)),$((i+BATCH_SIZE))p" todo_0_1_files.txt > current_batch.txt
    # 拉取当前批次文件到本地
    while read filename; do
        aws s3 cp "s3://my_source_bucket_name/my_folder/$filename" . --profile source_user
    done < current_batch.txt
    # 上传到目标桶
    aws s3 cp . s3://my_target_bucket_name/my_folder/ --recursive --profile source_user
    # 清理本地文件
    rm *.csv current_batch.txt
done

内容的提问来源于stack exchange,提问作者Gustav Rasmussen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 23:15:05