Use the copy_object method to copy an object smaller than 5 GiB from a source bucket to a destination bucket in the same region. The destination can be the same bucket or a different bucket.
Prerequisites
Before you begin, make sure you have:
Read permissions on the source object
Read and write permissions on the destination bucket
No retention policy configured on either bucket — if a retention policy exists, the copy fails with
The object you specified is immutable
The source and destination buckets must be in the same region. For example, objects in a China (Hangzhou) bucket cannot be copied to a China (Qingdao) bucket.
Permissions
By default, an Alibaba Cloud account has full permissions on its resources. RAM users and RAM roles have no permissions by default and must be granted access through RAM Policy or Bucket Policy.
The CopyObject API requires the following permissions:
| Permission | When required |
|---|---|
oss:GetObject | Always |
oss:PutObject | Always |
oss:GetObjectVersion | When specifying a source object version via versionId |
oss:GetObjectTagging | When copying object tags via x-oss-tagging |
oss:PutObjectTagging | When copying object tags via x-oss-tagging |
oss:GetObjectVersionTagging | When specifying tags of a specific version via versionId |
kms:GenerateDataKey | When the destination object metadata includes X-Oss-Server-Side-Encryption: KMS |
kms:Decrypt | When the destination object metadata includes X-Oss-Server-Side-Encryption: KMS |
Method signature
copy_object(request: CopyObjectRequest, **kwargs) -> CopyObjectResultRequest syntax
result = client.copy_object(
oss.CopyObjectRequest(
bucket='string', # destination bucket name
key='string', # destination object key
source_bucket='string', # source bucket name
source_key='string', # source object key
forbid_overwrite='true'|'false',
tagging='string', # e.g., 'TagA=A&TagB=B'
tagging_directive='Copy'|'Replace'
)
)For the full list of request parameters, see CopyObjectRequest.
Key parameters
| Parameter | Type | Description |
|---|---|---|
bucket | str | Name of the destination bucket |
key | str | Key of the destination object |
source_bucket | str | Name of the source bucket |
source_key | str | Key of the source object |
forbid_overwrite | str | Specifies whether the CopyObject operation overwrites an existing object that has the same name |
tagging | str | Tags for the destination object, in Key=Value&Key=Value format |
tagging_directive | str | How to set tags on the destination object. Copy (default): copy tags from the source object. Replace: use the tags specified in tagging |
Response syntax
# CopyObjectResult
{
'status_code': int,
'request_id': 'string',
'version_id': 'string',
'source_version_id': 'string',
'hash_crc64': 'string', # CRC-64 hash of the destination object
'etag': 'string',
'last_modified': datetime,
'server_side_encryption': 'string',
'server_side_data_encryption': 'string',
}For the full response definition, see CopyObjectResult and copy_object.
Copy an object
The following example copies an object that is less than 5 GiB in size from a source bucket to a destination bucket:
import argparse
import alibabacloud_oss_v2 as oss
parser = argparse.ArgumentParser(description="copy object sample")
parser.add_argument('--region', help='The region where the buckets are located.', required=True)
parser.add_argument('--bucket', help='The name of the destination bucket.', required=True)
parser.add_argument('--endpoint', help='The endpoint used to access OSS.')
parser.add_argument('--key', help='The key of the destination object.', required=True)
parser.add_argument('--source_key', help='The key of the source object.', required=True)
parser.add_argument('--source_bucket', help='The name of the source bucket.', required=True)
def main():
args = parser.parse_args()
# Load credentials from environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Initialize the SDK configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
if args.endpoint is not None:
cfg.endpoint = args.endpoint
client = oss.Client(cfg)
# Copy the object
result = client.copy_object(oss.CopyObjectRequest(
bucket=args.bucket,
key=args.key,
source_key=args.source_key,
source_bucket=args.source_bucket,
))
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
f' hash crc64: {result.hash_crc64},'
f' source version id: {result.source_version_id},'
f' server side encryption: {result.server_side_encryption},'
f' server side data encryption: {result.server_side_data_encryption},'
f' last modified: {result.last_modified},'
f' etag: {result.etag},'
)
if __name__ == "__main__":
main()Run the script:
python copy_object.py \
--region cn-hangzhou \
--bucket dest-bucket \
--key dest-object.txt \
--source_bucket src-bucket \
--source_key src-object.txtThe sample code uses cn-hangzhou as the region and the public endpoint by default. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For region IDs and endpoint formats, see Regions and endpoints.What's next
For the complete sample code, see copy_object.py on GitHub.