Todos los productos
Search
Centro de documentos

Object Storage Service:Restaurar archivos (SDK de Browser.js)

Última actualización: Sep 22, 2026

Los objetos Archive y Cold Archive deben restaurarse antes de poder leerse. En este tema se describe cómo restaurar objetos Archive y Cold Archive.

Notas de uso

  • Cuando utilice herramientas de empaquetado como Webpack y Browserify, instale el SDK de OSS para Browser.js con el comando npm install ali-oss.

  • Si desea acceder a un bucket de OSS desde un navegador, pero no hay reglas de CORS configuradas para ese bucket, el navegador rechazará la solicitud. Por lo tanto, configure las reglas de CORS del bucket si quiere acceder a él desde un navegador. Para obtener más información, consulte Installation.

  • En la mayoría de los casos, el SDK de OSS para Browser.js se utiliza en navegadores. Para evitar que su par de AccessKey quede expuesto, se recomienda usar credenciales de acceso temporales obtenidas de Security Token Service (STS) para acceder a OSS.

    Las credenciales de acceso temporales constan de un par de AccessKey y un token de seguridad. El par de AccessKey está formado por un AccessKey ID y un AccessKey secret. Para obtener más información sobre cómo conseguir credenciales de acceso temporales, consulte Use STS for temporary access authorization.

Restaurar un objeto Archive

A continuación se muestra cómo restaurar un objeto 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>

Restaurar un objeto Cold Archive

A continuación se muestra cómo restaurar un objeto 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>

Referencias

  • Para ver ejemplos de código completos sobre cómo restaurar objetos Archive y Cold Archive, consulte Ejemplos en GitHub.

  • Para obtener más información sobre la operación de API que permite restaurar objetos Archive y Cold Archive, consulte RestoreObject.