要将一个账户拥有的文件复制到另一个账户拥有的存储桶中,可以使用AWS SDK提供的API实现。以下是一个使用Python和Boto3库进行操作的示例代码:
import boto3
source_bucket_name = 'source-bucket' # 源存储桶名称
source_file_key = 'file.txt' # 源文件的Key
destination_bucket_name = 'destination-bucket' # 目标存储桶名称
destination_file_key = 'copied-file.txt' # 复制后的文件Key
# 创建源和目标S3客户端
source_s3 = boto3.client('s3', region_name='us-west-2', aws_access_key_id='SOURCE_ACCESS_KEY', aws_secret_access_key='SOURCE_SECRET_KEY')
destination_s3 = boto3.client('s3', region_name='us-west-2', aws_access_key_id='DESTINATION_ACCESS_KEY', aws_secret_access_key='DESTINATION_SECRET_KEY')
# 复制文件
copy_source = {'Bucket': source_bucket_name, 'Key': source_file_key}
destination_s3.copy(copy_source, destination_bucket_name, destination_file_key)
请确保在上述代码中将source-bucket
、file.txt
、destination-bucket
、copied-file.txt
替换为实际的存储桶名称和文件Key。同时,还需要提供源和目标账户的访问密钥(SOURCE_ACCESS_KEY
、SOURCE_SECRET_KEY
、DESTINATION_ACCESS_KEY
、DESTINATION_SECRET_KEY
)。