迁移阿里云oss目录到aws
1 # 同步阿里云oss文件到aws s3 2 import oss2 3 import boto3 4 import os 5 from boto3.s3.transfer import TransferConfig 6 7 8 9 # 阿里云OSS配置 10 aliyun_access_key_id = 'xxx' 11 aliyun_access_key_secret = 'xxx' 12 aliyun_endpoint = 'oss-cn-shenzhen.aliyuncs.com' 13 aliyun_bucket_name = 'cdn' 14 aliyun_prefix = 'magicon_img/' # 例如 'images/' 15 16 # AWS S3配置 17 aws_access_key_id = 'xxx' 18 aws_secret_access_key = 'xxx' 19 aws_bucket_name = 'static' 20 aws_prefix = 'magicon_img/' # 例如 'images/' 21 22 # 初始化阿里云OSS 23 aliyun_bucket = oss2.Bucket(oss2.Auth(aliyun_access_key_id, aliyun_access_key_secret), aliyun_endpoint, aliyun_bucket_name) 24 25 # 初始化AWS S3 26 s3_client = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) 27 transfer_config = TransferConfig(multipart_threshold=1024*25, max_concurrency=10, multipart_chunksize=1024*25, use_threads=True) 28 29 # 列出阿里云OSS中的文件 30 for obj in oss2.ObjectIterator(aliyun_bucket, prefix=aliyun_prefix): 31 source_key = obj.key 32 target_key = aws_prefix + source_key[len(aliyun_prefix):] # 根据需要调整目标路径 33 # 下载阿里云OSS文件到本地 34 local_filename = source_key.split('/')[-1] 35 if local_filename: 36 aliyun_bucket.get_object_to_file(source_key, local_filename) 37 38 # 上传文件到AWS S3 39 s3_client.upload_file(local_filename, aws_bucket_name, target_key, Config=transfer_config) 40 # 删除本地文件(如果不需要保留) 41 os.remove(local_filename) 42 43 print(f'文件 {source_key} 已迁移到 {target_key}') 44 45 print('所有文件迁移完成。')