This topic describes how to copy an object from a source bucket to the same or a different destination bucket in the same region.
Notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
-
In this topic, access credentials are obtained from environment variables. For more information, see Configure access credentials.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Client configuration.
-
You must have read permissions on the source object and read/write permissions on the destination bucket.
-
The source and destination buckets must not have retention policies configured. Otherwise, the copy fails and the error The object you specified is immutable. is returned.
-
Cross-region copy is not supported. For example, you cannot copy an object from a China (Hangzhou) bucket to a China (Qingdao) bucket.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
|
API |
Action |
Description |
|
CopyObject |
|
Copies objects within a bucket or between buckets in the same region. |
|
|
||
|
|
If you specify the source object version through versionId, this permission is also required. |
|
|
|
If you copy object tags through x-oss-tagging, these permissions are required. |
|
|
|
||
|
|
If you specify the tags of a specific version of the source object through versionId, this permission is also required. |
|
|
|
When copying an object, if the destination object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. |
|
|
|
Sample code
You can use bucket.copy_object to copy an object and specify the object metadata using the :meta_directive parameter. You can handle object metadata in one of the following ways during a copy operation:
-
If you do not specify the meta parameter, the metadata of the destination object is the same as that of the source object.
-
If you specify the meta parameter, the metadata of the source object is overwritten with the new metadata.
The following code shows how to copy an object:
require 'aliyun/oss'
client = Aliyun::OSS::Client.new(
# Specify the Endpoint for the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
access_key_id: ENV['OSS_ACCESS_KEY_ID'],
access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)
# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# Copy the object and its metadata.
bucket.copy_object(
# In this example, srcobject.txt is copied as destobject.txt.
'destobject.txt', 'srcobject.txt',
:meta_directive => Aliyun::OSS::MetaDirective::COPY)
# Copy the object and overwrite its metadata.
bucket.copy_object(
'destobject.txt', 'srcobject.txt',
:metas => {'year' => '2017'},
:meta_directive => Aliyun::OSS::MetaDirective::REPLACE)
References
For more information about the CopyObject API operation, see CopyObject.