Copy an object within a bucket or across buckets in the same region using OSS SDK for Browser.js.
Prerequisites
Before you begin, make sure that you have:
Installed OSS SDK for Browser.js. If you use a packaging tool such as Webpack or Browserify, run
npm install ali-ossto install the SDK.Configured cross-origin resource sharing (CORS) rules on both the source and destination buckets. For details, see Preparations.
Obtained temporary access credentials from Security Token Service (STS): an AccessKey ID, an AccessKey secret, and a security token. Using STS credentials prevents exposing your long-term AccessKey pair in browser-side code. For details, see Use STS for temporary access authorization.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default and must be granted the required actions through RAM Policy or bucket policies.
| API | Action | When required |
|---|---|---|
| CopyObject | oss:GetObject | Always required |
| CopyObject | oss:PutObject | Always required |
| CopyObject | oss:GetObjectVersion | Required when specifying the source object version via versionId |
| CopyObject | oss:GetObjectTagging | Required when copying object tags via x-oss-tagging |
| CopyObject | oss:PutObjectTagging | Required when copying object tags via x-oss-tagging |
| CopyObject | oss:GetObjectVersionTagging | Required when specifying tags of a specific source object version via versionId |
| CopyObject | kms:GenerateDataKey | Required when the destination object metadata contains X-Oss-Server-Side-Encryption: KMS |
| CopyObject | kms:Decrypt | Required when the destination object metadata contains X-Oss-Server-Side-Encryption: KMS |
Copy an object within a bucket
The following example copies srcobject.txt to destobject.txt within examplebucket. Initialize the OSS client with STS credentials, then call client.copy() with the destination and source object names.
<!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.
// Example: if the bucket is in China (Hangzhou), set region to oss-cn-hangzhou.
region: 'yourRegion',
authorizationV4: true,
// STS temporary credentials.
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
stsToken: 'yourSecurityToken',
bucket: 'examplebucket',
});
const upload = document.getElementById('upload');
const copy = document.getElementById('copy');
// Content and name of the object to upload.
const file = new Blob(['examplecontent']);
const fileName = 'srcobject.txt';
// Upload the source object.
upload.addEventListener('click', () => {
client.put(fileName, file).then(r => console.log(r));
});
// Copy the object within the same bucket.
copy.addEventListener('click', () => {
client.copy('destobject.txt', fileName)
.then(r => console.log(r.res.status));
});
</script>
</body>
</html>Replace the following placeholders before running the example:
| Placeholder | Description | Example |
|---|---|---|
yourRegion | Region where the bucket is located | oss-cn-hangzhou |
yourAccessKeyId | Temporary AccessKey ID from STS | STS.NUgYrLnoC55... |
yourAccessKeySecret | Temporary AccessKey secret from STS | xXxXxXx... |
yourSecurityToken | Security token from STS | CAISxgF1q6Ft5B2yfSjI... |
A successful copy returns HTTP status 200.
Set HTTP headers and object metadata (optional)
Pass a third argument to client.copy() to customize the destination object. If omitted, the destination object inherits the HTTP headers and metadata of the source object.
client.copy('destobject.txt', fileName, {
headers: {
// Set the Cache-Control header for the destination object.
'Cache-Control': 'no-cache',
// Conditional copy: copy only if the source ETag matches.
'if-match': '5B3C1A2E053D763E1B002CC607C5****',
// Conditional copy: copy only if the source ETag does not match.
'if-none-match': '5B3C1A2E053D763E1B002CC607C5****',
// Conditional copy: copy only if the source object was modified after this time.
'if-modified-since': '2021-12-09T07:01:56.000Z',
// Conditional copy: copy only if the source object was not modified after this time.
'if-unmodified-since': '2021-12-09T07:01:56.000Z',
// Set the access control list (ACL) for the destination object.
// private: only the object owner and authorized users have read and write access.
'x-oss-object-acl': 'private',
// Attach tags to the destination object. Separate multiple tags with ampersands.
'x-oss-tagging': 'Tag1=1&Tag2=2',
// Prevent overwriting a destination object with the same name.
'x-oss-forbid-overwrite': 'true',
},
meta: {
// Custom metadata for the destination object.
location: 'hangzhou',
year: 2015,
people: 'mary',
},
});Copy an object between buckets
The following example copies srcobject.txt from srcbucket to destobject.txt in destbucket. Both buckets must be in the same region. Initialize the client with the destination bucket, then pass the source bucket name as the third argument to client.copy().
<!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({
region: 'yourRegion',
authorizationV4: true,
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
stsToken: 'yourSecurityToken',
// Specify the destination bucket.
bucket: 'destbucket',
});
const copy = document.getElementById('copy');
copy.addEventListener('click', () => {
client
.copy(
'srcobject.txt', // Destination object name.
'destobject.txt', // Source object name.
'srcbucket' // Source bucket name.
)
.then(r => console.log(r));
});
</script>
</body>
</html>A successful copy returns HTTP status 200.
What's next
For complete sample code, see GitHub examples.
For the underlying API, see CopyObject.