Todos los productos
Search
Centro de documentos

Object Storage Service:Gestión del control de versiones (SDK de Node.js)

Última actualización: Sep 22, 2026

El control de versiones se aplica a todos los objetos de un bucket. Esta función le permite recuperar objetos en una versión anterior si se sobrescriben o eliminan por accidente.

Un bucket puede encontrarse en uno de tres estados de versionado: sin versionado (predeterminado), con versionado habilitado o con versionado suspendido. Para obtener más información sobre el control de versiones, consulte Versioning.

Nota

El control de versiones es compatible con las versiones 6.8.0 y posteriores del kit de desarrollo de software (SDK).

Establezca el estado de versionado de un bucket

A continuación se muestra cómo habilitar o suspender el control de versiones de un bucket.

const OSS = require('ali-oss');

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',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function putBucketVersioning() {
  // Set the versioning state of the bucket to Enabled or Suspended.
  const status = 'Enabled'; // `Enabled` or `Suspended`
  const result = await client.putBucketVersioning('BucketName', status);
  console.log(result);
}
putBucketVersioning();

Obtenga el estado de versionado de un bucket

A continuación se muestra cómo obtener el estado de versionado de un bucket.

const OSS = require('ali-oss');

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',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function getBucketVersioning() {
  // Get the versioning state of the bucket.
  const result = await client.getBucketVersioning('BucketName');
  console.log(result.versionStatus);
}
getBucketVersioning();

Liste todas las versiones de los objetos de un bucket

A continuación se muestra cómo listar todas las versiones de los objetos de un bucket específico, incluidos los marcadores de eliminación.

const OSS = require('ali-oss');

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',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function getBucketVersions() {
  // List all object versions, including delete markers.
  const result = await client.getBucketVersions();
  console.log(result.objects); 
  console.log(result.deleteMarker);
}
getBucketVersions();

Referencias

  • Para consultar el código de ejemplo completo sobre la gestión del control de versiones, consulte el ejemplo de GitHub.

  • Para obtener información sobre la operación de API que establece el estado de versionado de un bucket, consulte PutBucketVersioning.

  • Si necesita detalles sobre la operación de API que obtiene el estado de versionado de un bucket, consulte GetBucketVersioning.

  • En cuanto a la operación de API para listar todas las versiones de los objetos de un bucket, incluidos los marcadores de eliminación, consulte ListObjectVersions (GetBucketVersions).