Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer le versioning (SDK Node.js)

Dernière mise à jour :Aug 18, 2026

Le versioning s'applique à tous les objets d'un bucket. Il vous permet de restaurer une version antérieure d'un objet en cas de suppression ou d'écrasement accidentel.

Un bucket peut se trouver dans l'un des trois états de versioning suivants : sans versioning (par défaut), versioning activé ou versioning suspendu. Pour plus d'informations sur le versioning, consultez la rubrique Versioning.

Remarque

Le versioning est pris en charge par les versions 6.8.0 et ultérieures du SDK.

Définir l'état de versioning d'un bucket

L'extrait de code suivant montre comment activer ou suspendre le versioning pour 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();

Récupérer l'état de versioning d'un bucket

L'extrait de code suivant montre comment récupérer l'état de versioning d'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();

Lister toutes les versions d'objets dans un bucket

L'extrait de code suivant montre comment lister toutes les versions d'objets, y compris les marqueurs de suppression, dans un bucket spécifié.

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();

Références

  • Pour obtenir l'exemple de code complet relatif à la gestion du versioning, consultez l'exemple GitHub.

  • Pour plus d'informations sur l'opération API permettant de définir l'état de versioning d'un bucket, consultez la rubrique PutBucketVersioning.

  • Pour plus d'informations sur l'opération API permettant de récupérer l'état de versioning d'un bucket, consultez la rubrique GetBucketVersioning.

  • Pour plus d'informations sur l'opération API permettant de lister toutes les versions d'objets dans un bucket, y compris les marqueurs de suppression, consultez la rubrique ListObjectVersions (GetBucketVersions).