使用预签名URL将一个存储桶中的对象复制到另一个存储桶的解决方法如下所示:
import boto3
def copy_object_between_buckets(source_bucket, source_key, destination_bucket, destination_key):
# 创建S3客户端
s3_client = boto3.client('s3')
# 生成预签名URL来获取源对象
source_url = s3_client.generate_presigned_url(
ClientMethod='get_object',
Params={
'Bucket': source_bucket,
'Key': source_key
}
)
# 使用预签名URL将对象复制到目标存储桶
s3_client.copy_object(
CopySource=source_url,
Bucket=destination_bucket,
Key=destination_key
)
# 示例用法
copy_object_between_buckets('source-bucket', 'source-object-key', 'destination-bucket', 'destination-object-key')
上述代码使用boto3
库创建了一个S3客户端,并使用generate_presigned_url
方法生成了一个预签名URL来获取源对象。然后,使用copy_object
方法将源对象的预签名URL作为CopySource
参数传递,并指定目标存储桶和目标对象的键。这将复制源对象到目标存储桶中。
请确保已安装并配置了AWS CLI,并且已通过aws configure
命令设置了您的AWS凭据。