This topic describes how to use the CopyObject method of OSS SDK for Harmony to copy an object that is less than 5 GiB in size from a source bucket to a destination bucket in the same region. The destination bucket can be the source bucket or a different bucket.
Notes
For more information about supported regions and endpoints, see OSS regions and endpoints.
To copy an object, you must have the read permissions on the source object and read and write permissions on the destination bucket.
The source bucket and destination bucket must be located in the same region. For example, objects in a bucket located in the China (Hangzhou) region cannot be copied to a bucket located in the China (Qingdao) region.
Make sure that no retention policies are configured for the source bucket and the destination bucket. Otherwise, the The object you specified is immutable. error message is returned.
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
Below is a code example for copying objects within the same bucket.
import Client, { RequestError } from '@aliyun/oss';
// Create an OSSClient instance.
const client = new Client({
// Specify the AccessKey ID obtained from Security Token Service (STS).
accessKeyId: 'yourAccessKeyId',
// Specify the AccessKey secret obtained from STS.
accessKeySecret: 'yourAccessKeySecret',
// Specify the security token obtained from STS.
securityToken: 'yourSecurityToken',
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
/**
* Copy an object to the same bucket.
* Use the copyObject method to copy the source object to the destination object.
*/
const copyObject = async () => {
try {
// Use the copyObject method to copy an object to the same bucket.
const res = await client.copyObject({
bucket: 'targetBucket', // Name of the destination bucket.
key: 'targetKey', // Name of the destination object.
copySourceKey: 'sourceKey', // Name of the source object.
});
// Output the result.
console.log(JSON.stringify(res));
} catch (err) {
// Capture exceptions.
if (err instanceof RequestError) {
console.log('code: ', err.code); // Error code.
console.log('message: ', err.message); // Error message.
console.log('requestId: ', err.requestId); // Request ID.
console.log('status: ', err.status); // HTTP status code.
console.log('ec: ', err.ec); // EC
} else {
console.log('unknown error: ', err);
}
}
};
// Call the copyObject function to copy the object to the same bucket.
copyObject();