This topic describes how to copy an object within a bucket or across buckets in the same region.
Notes
When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.
In most cases, OSS SDK for Browser.js is used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use temporary access credentials obtained from Security Token Service (STS) to access OSS.
The temporary access credentials consist of an AccessKey pair and a security token. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to obtain temporary access credentials, see Use STS for temporary access authorization.
The source and destination buckets for the copy operation must have cross-origin resource sharing (CORS) rules configured. For more information, see Preparations.
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. | |
|
Copy an object within a bucket
The following code shows how to copy the srcobject.txt object to destobject.txt within the examplebucket bucket.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id='upload'>Upload</button>
<button id='copy'>Copy</button>
<!--Import the SDK file.-->
<script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
<script type="text/javascript">
const client = new OSS({
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
region: 'yourRegion',
authorizationV4: true,
// The temporary AccessKey ID and AccessKey secret obtained from STS.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// The security token obtained from STS.
stsToken: 'yourSecurityToken',
// Specify the bucket name. Example: examplebucket.
bucket: "examplebucket",
});
const upload = document.getElementById('upload')
const copy = document.getElementById('copy')
// Specify the content of the object to upload.
const file = new Blob(['examplecontent'])
// Specify the full path of the object to upload. Example: srcobject.txt.
const fileName = 'srcobject.txt'
// Upload the object.
upload.addEventListener('click', () => {
const result = client.put(fileName, file).then(r => console.log(r))
})
// Copy the object.
copy.addEventListener('click', () => {
// Specify the name of the destination object.
client.copy('destobject.txt', fileName
// Set the HTTP headers and custom metadata for the destination object.
//{
// Specify the headers parameter to set the HTTP headers for the destination object. If you do not specify this parameter, the destination object has the same HTTP headers as the source object.
// headers:{
//'Cache-Control': 'no-cache',
// The copy operation is performed only if the ETag of the source object matches the specified ETag.
//'if-match': '5B3C1A2E053D763E1B002CC607C5****',
// The copy operation is performed only if the ETag of the source object does not match the specified ETag.
//'if-none-match': '5B3C1A2E053D763E1B002CC607C5****',
// The copy operation is performed only if the source object has been modified since the specified time.
//'if-modified-since': '2021-12-09T07:01:56.000Z',
// The copy operation is performed only if the source object has not been modified since the specified time.
//'if-unmodified-since': '2021-12-09T07:01:56.000Z',
// Specify the access control list (ACL) for the destination object. In this example, the ACL is set to private. This means only the object owner and authorized users have read and write permissions. Other users cannot access the object.
//'x-oss-object-acl': 'private',
// Specify the tags for the object. You can specify multiple tags.
//'x-oss-tagging': 'Tag1=1&Tag2=2',
// Specify whether to overwrite a destination object that has the same name. In this example, the value is set to true to prevent overwriting.
//'x-oss-forbid-overwrite': 'true',
//},
// Specify the meta parameter to customize the metadata of the destination object. If you do not specify this parameter, the destination object has the same metadata as the source object.
// meta:{
// location: 'hangzhou',
// year: 2015,
// people: 'mary'
//}
// }
).then(r => {
console.log(r.res.status)
})
})
</script>
</body>
</html>Copy an object between buckets
The following code shows how to copy the srcobject.txt object from the srcbucket source bucket to the destobject.txt object in the destbucket destination bucket.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id="copy">Copy</button>
<!--Import the SDK file.-->
<script
type="text/javascript"
src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"
></script>
<script type="text/javascript">
const client = new OSS({
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
region: "yourRegion",
authorizationV4: true,
// The temporary AccessKey ID and AccessKey secret obtained from STS.
accessKeyId: "yourAccessKeyId",
accessKeySecret: "yourAccessKeySecret",
// The security token obtained from STS.
stsToken: "yourSecurityToken",
// Specify the destination bucket name.
bucket: "destbucket",
});
const copy = document.getElementById("copy");
copy.addEventListener("click", () => {
client
.copy(
// Specify the name of the destination object.
"srcobject.txt",
// Specify the name of the source object.
"destobject.txt",
// Specify the source bucket name.
"srcbucket"
)
.then((r) => console.log(r));
});
</script>
</body>
</html>
References
For the complete sample code for copying an object, see GitHub examples.
For more information about the API operation for copying an object, see CopyObject.