Vous devez restaurer les objets de classe de stockage Archive et Cold Archive avant de pouvoir les lire. Cette rubrique explique comment restaurer ces objets.
Notes d'utilisation
Si vous utilisez des outils de regroupement tels que Webpack et Browserify, installez le SDK OSS pour Browser.js en exécutant la commande npm install ali-oss.
Si vous tentez d'accéder à un bucket OSS depuis un navigateur sans avoir configuré de règles CORS, le navigateur rejette la requête. Vous devez donc configurer des règles CORS pour le bucket afin d'y accéder depuis un navigateur. Pour plus d'informations, consultez la section Installation.
-
Le SDK OSS pour Browser.js s'utilise principalement dans les navigateurs. Pour éviter d'exposer votre paire de clés AccessKey, nous vous recommandons d'utiliser des identifiants temporaires obtenus via le Security Token Service (STS) pour accéder à OSS.
Les identifiants temporaires comprennent une paire de clés AccessKey et un jeton de sécurité. La paire de clés AccessKey se compose d'un AccessKey ID et d'un AccessKey Secret. Pour savoir comment obtenir des identifiants temporaires, consultez la section Utiliser STS pour une autorisation d'accès temporaire.
Restaurer un objet Archive
Le code suivant montre comment restaurer un objet Archive :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id='upload'>Upload</button>
<button id='restore'>Restore File</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 region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region 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 (SecurityToken) obtained from STS.
stsToken: 'yourSecurityToken',
// The bucket name. For example, examplebucket.
bucket: "examplebucket",
});
const upload = document.getElementById('upload')
const restore = document.getElementById('restore')
// Specify the content of the file to upload.
const file = new Blob(['examplecontent'])
// Specify the name of the object to be stored in the bucket.
const fileName = 'example.txt'
// Upload the file and set its storage class to Archive.
upload.addEventListener('click', () => {
client.put(fileName, file, {
headers: {
'x-oss-storage-class': 'Archive'
}
}).then(r => console.log(r))
})
// Restore the Archive object.
restore.addEventListener('click', () => {
client.restore(fileName).then(r => console.log(r))
})
</script>
</body>
</html>
Restaurer un objet Cold Archive
Le code suivant montre comment restaurer un objet Cold Archive :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id='upload'>Upload</button>
<button id='restore'>Restore File</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 region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region 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 (SecurityToken) obtained from STS.
stsToken: 'yourSecurityToken',
// The bucket name. For example, examplebucket.
bucket: "examplebucket",
});
const upload = document.getElementById("upload");
const restore = document.getElementById("restore");
// Specify the content of the file to upload.
const file = new Blob(["examplecontent"]);
// Specify the name of the object to be stored in the bucket.
const fileName = "example1.txt";
// Upload the file and set its storage class to ColdArchive.
upload.addEventListener("click", () => {
client
.put(fileName, file, {
headers: {
"x-oss-storage-class": "ColdArchive",
},
})
.then((r) => console.log(r));
});
// Expedited: The object is restored within 1 hour.
// Standard: The object is restored within 2 to 5 hours. If you do not specify the JobParameters parameter, Standard is used by default.
// Bulk: The object is restored within 5 to 12 hours.
// Days specifies the number of days that the object remains in the restored state. The valid range is 1 to 7.
// Restore the Cold Archive object.
restore.addEventListener("click", () => {
client
.restore(fileName, {
type: "ColdArchive",
// Specifies the restoration priority.
// Expedited: The object is restored within 1 hour.
// Standard: The object is restored within 2 to 5 hours.
// Bulk: The object is restored within 5 to 12 hours.
JobParameters: "Bulk",
// The number of days that the object remains in the restored state. The default value is 1.
Days: 2,
})
.then((r) => console.log(r));
});
</script>
</body>
</html>
Références
Pour consulter des exemples de code complets illustrant la restauration des objets Archive et Cold Archive, rendez-vous sur la page GitHub Examples.
Pour plus d'informations sur l'opération API permettant de restaurer les objets Archive et Cold Archive, consultez la documentation relative à l'opération RestoreObject.