Todos os produtos
Search
Central de documentação

Object Storage Service:Restore objects (Harmony SDK)

Última atualização: Sep 22, 2026

Os objetos nas classes de armazenamento Archive, Cold Archive e Deep Cold Archive devem ser restaurados antes que possam ser lidos. Durante a restauração, uma cópia temporária é criada, mantendo o objeto arquivado original intacto. Após a expiração do período de restauração, essa cópia temporária é excluída automaticamente. Este tópico descreve como usar o SDK Harmony do OSS para restaurar objetos dessas classes de armazenamento.

Precauções

  • O método RestoreObject é compatível apenas com objetos das classes de armazenamento Archive, Cold Archive e Deep Cold Archive.

  • Para mais informações sobre as regiões e os endpoints compatíveis com o OSS, consulte Regiões e endpoints.

  • Para restaurar um objeto, você deve ter a permissão oss:RestoreObject. Para mais informações, consulte Conceder permissões personalizadas a um usuário do RAM.

Código de exemplo

Utilize o código de exemplo a seguir para restaurar um objeto das classes de armazenamento Archive, Cold Archive ou Deep Cold Archive:

import Client, { RequestError } from '@aliyun/oss';

// Create an OSS client instance.
const client = new Client({
  // Replace with the Access Key ID for your STS temporary access credential.
  accessKeyId: 'yourAccessKeyId',
  // Replace with the Access Key Secret for your STS temporary access credential.
  accessKeySecret: 'yourAccessKeySecret',
  // Replace with the Security Token for your STS temporary access credential.
  securityToken: 'yourSecurityToken',
  // Specify 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket. Replace with your actual bucket name.
const bucket = 'yourBucketName';
// Specify the name of the object to restore. Replace with your actual object name.
const key = 'yourObjectName';

/**
 * Restore an archived object.
 * Use the restoreObject method to restore an object from the Archive or Cold Archive storage class to a readable state.
 */
const restoreObject = async () => {
  try {
    // Call the restoreObject method to restore the archived object in the specified bucket.
    const res = await client.restoreObject({
      bucket, // The name of the bucket.
      key,    // The name of the object.
      restoreRequest: {
        days: 2, // The number of days that you want the restored object to be available.
      },
    });

    // Print the restoration result.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Catch exceptions that occur during the request.
    if (err instanceof RequestError) {
      // If the error is a known type, print the error code, error message, request ID, status code, and EC code.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The error code.
    } else {
      // Print other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the restoreObject function to restore the archived object.
restoreObject();

Referências