Object Storage Service (OSS) provides the following storage classes to cover various data storage scenarios from hot data to cold data: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. In OSS, once an object is created, its content cannot be modified. If you want to convert the storage class of an object, you must use the Bucket.CopyObject method to copy the object to create a new object and convert the storage class of the new object.
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.
If you want to access an OSS bucket from a browser but no CORS rules are configured for the bucket, the browser rejects the request. Therefore, you must configure CORS rules for a bucket if you want to access the bucket from a browser. For more information, see Installation.
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.
Example code
Convert the storage class of an object from Standard or Infrequent Access to Archive
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id='upload'>Upload</button>
<button id='copy'>Convert object storage class</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({
// Set yourRegion to 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 file to upload.
const file = new Blob(['examplecontent'])
// Specify the full path of the object to upload to examplebucket. Example: exampledir/exampleobject.txt.
const fileName = 'exampledir/exampleobject.txt'
// Upload the file.
upload.addEventListener('click', () => {
client.put(fileName, file).then(r => console.log(r))
})
// Convert the object storage class using the copy method.
copy.addEventListener('click', () => {
// Specify the name of the destination object. Example: newexampleobject.txt.
client.copy('newexampleobject.txt', fileName, {
headers: {
// Set the storage class of the object to Archive. To change the storage class to Cold Archive, replace Archive with ColdArchive.
'x-oss-storage-class': 'Archive'
}
}
).then(r => {
console.log(r.res.status)
})
})
</script>
</body>
</html>Convert the storage class of an object from Archive to Infrequent Access or Standard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id="check">Check</button>
<button id="restore">Restore</button>
<button id="change">Convert</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({
// Set yourRegion to 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',
// 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 check = document.getElementById("check");
const change = document.getElementById("change");
const restore = document.getElementById("restore");
check.addEventListener("click", () => {
console.log("Check the object type");
// Check the Header in the developer tools to confirm the object storage class.
client.head("srcobject.txt").then((r) => console.log(r));
});
// Restore the object.
restore.addEventListener("click", () => {
// Specify the name of the object to restore. Example: srcobject.txt.
client.restore("srcobject.txt").then((r) => {
console.log(r);
console.log("Start restoring");
});
});
// After the object is restored, convert its storage class.
change.addEventListener("click", () => {
// The restoration time depends on the object size.
console.log("Start converting");
client
// Specify destobject.txt as the name of the destination object after srcobject.txt is copied.
.copy("destobject.txt", "srcobject.txt", {
// Set the storage class of the object to Infrequent Access (IA). To change the storage class to Standard, replace IA with Standard.
headers: { "x-oss-storage-class": "IA" },
})
.then((r) => console.log(r))
// A common cause for conversion errors is that the object is still being restored.
.catch((e) => console.log("Conversion error:", e));
});
</script>
</body>
</html>
References
For the complete sample code for converting object storage classes, see GitHub examples.
For more information about the API operation used to convert object storage classes, see CopyObject.