This topic describes how to copy an object from a source bucket to the same or a different destination bucket in the same region.
Usage 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 about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.
To copy an object, you must have read permissions on the source object and read and write permissions on the destination bucket.
Make sure that no retention policies are configured for the source bucket and the destination bucket. Otherwise, the error message The object you specified is immutable. is returned.
The source bucket and destination bucket must be in the same region. For example, objects cannot be copied from a bucket located in the China (Hangzhou) region to another bucket located in the China (Qingdao) region.
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 Policy or Bucket policies.
API | Action | Definition |
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.