This topic describes how to copy an object within a bucket or across buckets in the same region.
Usage 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.
You must configure cross-origin resource sharing (CORS) rules for the source and destination buckets. For more information, see Installation.
Copy an object within a bucket
The following code provides an example on how to copy the srcobject.txt object to the destobject.txt object within the bucket named examplebucket:
<!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 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: 'yourRegion',
// Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// Specify the security token that you obtained from STS.
stsToken: 'yourSecurityToken',
// Specify the name of the bucket. 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. 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
// Specify the HTTP headers and metadata of the destination object.
//{
// Configure the headers parameter to specify HTTP headers for the destination object. If you do not configure the headers parameter, the HTTP headers of the destination object are the same as the HTTP headers of the source object. The HTTP headers of the source object are copied.
// headers:{
//'Cache-Control': 'no-cache',
// If the ETag value of the source object is the same as the ETag value specified in the request, OSS copies the object and returns 200 OK.
//'if-match': '5B3C1A2E053D763E1B002CC607C5****',
// If the ETag value of the source object is different from the ETag value specified in the request, OSS copies the object and returns 200 OK.
//'if-none-match': '5B3C1A2E053D763E1B002CC607C5****',
// If the time that is specified in the request is earlier than the time when the object is modified, OSS copies the object and returns 200 OK.
//'if-modified-since': '2021-12-09T07:01:56.000Z',
// If the time that is specified in the request is later than the time when the object is modified, OSS copies the object and returns 200 OK.
//'if-unmodified-since': '2021-12-09T07:01:56.000Z',
// Specify the access control list (ACL) of the destination object. In this example, the ACL is set to private, which indicates that only the object owner and authorized users have read and write permissions on the object.
//'x-oss-object-acl': 'private',
// Specify tags for the destination object. You can specify multiple tags for the object at a time.
//'x-oss-tagging': 'Tag1=1&Tag2=2',
// Specify whether the CopyObject operation overwrites an object that has the same name. In this example, this parameter is set to true, which specifies that the operation does not overwrite an object that has the same name.
//'x-oss-forbid-overwrite': 'true',
//},
// Configure the meta parameter to specify metadata for the destination object. If you do not configure the meta parameter, the metadata of the destination object is the same as the metadata of the source object. The metadata of the source object is copied.
// meta:{
// location: 'hangzhou',
// year: 2015,
// people: 'mary'
//}
// }
).then(r => {
console.log(r.res.status)
})
})
</script>
</body>
</html>
Copy an object across buckets
The following code provides an example on how to copy the srcobject.txt object from the source bucket named srcbucket to the destobject.txt object in the destination bucket named destbucket:
<!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 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: "yourRegion",
// Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret.
accessKeyId: "yourAccessKeyId",
accessKeySecret: "yourAccessKeySecret",
// Specify the security token that you obtained from STS.
stsToken: "yourSecurityToken",
// Specify the name of the destination bucket.
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 name of the source bucket.
"srcbucket"
)
.then((r) => console.log(r));
});
</script>
</body>
</html>
References
For the complete sample code that is used to copy an object, visit GitHub.
For more information about the API operation that you can call to copy an object, see CopyObject.