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.
For more information about how to use STS, see Use temporary credentials provided by STS to access OSS in OSS Developer Guide. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain temporary access credentials. Temporary access credentials include a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.
Copy an object within a bucket
The following code provides an example on how to copy an object within the same 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 An Object</button>
<!-- Import the SDK file -->
<script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.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. An 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: exampledir/exampleobject.txt.
const fileName = 'exampledir/exampleobject.txt'
// Upload the local file.
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('newexampleobject.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 that is 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 that is 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 object. You can specify multiple tags at a time.
//'x-oss-tagging': 'Tag1=1&Tag2=2',
// Specify whether the CopyObject operation overwrites an object with the same name. In this example, this parameter is set to true, which indicates that the object with the same name cannot be overwritten.
//'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 An Object</button>
<!-- Import the SDK file -->
<script
type="text/javascript"
src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.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. An 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");
const fileContent = Array(1024 * 1024 * 5)
.fill("a")
.join("");
const file = new File([fileContent], fileName);
copy.addEventListener("click", () => {
client
.copy(
// Specify the name of the destination object.
"destobject.txt",
// Specify the name of the source object.
"srcobject.txt",
// Specify the name of the source bucket.
"sourcebucket"
)
.then((r) => console.log(r));
});
</script>
</body>
</html>
References
- For more information about 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.