O Object Storage Service (OSS) oferece as seguintes classes de armazenamento para diversos cenários, desde dados quentes até frios: Standard, Infrequent Access (IA), Archive, Cold Archive e Deep Cold Archive. No OSS, o conteúdo de um objeto não pode ser modificado após a criação. Para converter a classe de armazenamento de um objeto, use o método Bucket.CopyObject para copiá-lo e criar um novo objeto com a classe desejada.
Observações de uso
Ao usar ferramentas de empacotamento como Webpack e Browserify, instale o OSS SDK for Browser.js executando o comando npm install ali-oss.
Se você tentar acessar um bucket do OSS pelo navegador sem regras CORS configuradas, o navegador rejeitará a solicitação. Portanto, configure regras CORS no bucket para permitir o acesso via navegador. Para mais informações, consulte Instalação.
-
O OSS SDK for Browser.js é usado principalmente em navegadores. Para evitar a exposição do seu par de AccessKey, use credenciais de acesso temporárias obtidas pelo Security Token Service (STS) para acessar o OSS.
As credenciais de acesso temporárias consistem em um par de AccessKey e um token de segurança. O par de AccessKey inclui um AccessKey ID e um AccessKey secret. Para saber como obter credenciais de acesso temporárias, consulte Usar STS para autorização de acesso temporário.
Código de exemplo
Converter a classe de armazenamento de um objeto de Standard ou Infrequent Access para 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>
Converter a classe de armazenamento de um objeto de Archive para Infrequent Access ou 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>
Referências
Para ver o código de exemplo completo sobre conversão de classes de armazenamento de objetos, consulte os exemplos no GitHub.
Para obter mais informações sobre a operação de API que converte classes de armazenamento de objetos, consulte CopyObject.