Tous les produits
Search
Centre de documentation

Object Storage Service:Politiques de conservation des données (SDK Node.js)

Dernière mise à jour :Aug 18, 2026

Les politiques de conservation des données d'Object Storage Service (OSS) reposent sur le principe WORM (Write Once, Read Many). Cette fonctionnalité vous permet de stocker et d'utiliser les données en interdisant leur suppression et leur modification. Pour empêcher tous les utilisateurs, y compris le propriétaire de la ressource, de modifier ou de supprimer des objets dans un bucket OSS pendant une période donnée, définissez une politique de conservation pour ce bucket. Tant que la période de conservation n'est pas expirée, vous pouvez uniquement charger et lire des objets dans le bucket. Une fois cette période écoulée, vous pouvez modifier ou supprimer les objets.

Remarque

Avant de configurer les politiques de conservation, assurez-vous de bien comprendre leur fonctionnement. Pour plus d'informations, consultez Politiques de conservation.

Créer une politique de conservation des données

Le code suivant crée une politique de conservation des données.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Create a data retention policy.
async function initiateBucketWorm() {
 // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
  // Specify the retention period in days.
  const days = '<Retention Days>'
    const res = await client.initiateBucketWorm(bucket, days)
  console.log(res.wormId)
}

initiateBucketWorm()

Annuler une politique de conservation des données non verrouillée

Le code suivant annule une politique de conservation des données non verrouillée.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Cancel the data retention policy.
async function abortBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
    try {
    await client.abortBucketWorm(bucket)
    console.log('abort success')
  } catch(err) {
        console.log('err: ', err)
    }
}

abortBucketWorm()

Verrouiller une politique de conservation des données

Le code suivant verrouille une politique de conservation des données.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Lock the data retention policy.
async function completeBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
  const wormId = 'Your Worm Id'
    try {
        await client.completeBucketWorm(bucket, wormId)
  } catch(err) {
      console.log(err)
  }
}

completeBucketWorm()

Obtenir une politique de conservation des données

Le code suivant récupère une politique de conservation des données.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Get the data retention policy.
async function getBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
    try {
        const res = await client.getBucketWorm(bucket)
    // View the data retention policy ID.
    console.log(res.wormId)
    // View the status of the data retention policy. The status can be "InProgress" (unlocked) or "Locked".
    console.log(res.state)
    // View the retention period for objects.
    console.log(res.days)
  } catch(err) {
      console.log(err)
  }
}

getBucketWorm()

Prolonger la période de conservation des objets

Le code suivant prolonge la période de conservation des objets associés à une politique de conservation des données verrouillée.

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 this 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 your bucket name.
  bucket: 'yourBucketName',
});
// Extend the retention period of objects in a locked data retention policy.
async function extendBucketWorm() {
  // Set bucket to your bucket name.
  const bucket = 'yourbucketname'
  const wormId = 'Your Worm Id'
  const days = 'Retention Days'
    try {
        const res = await client.extendBucketWorm(bucket, wormId, days)
    console.log(res)
  } catch(err) {
      console.log(err)
  }
}

extendBucketWorm()

Références

  • Pour obtenir l'exemple de code complet relatif aux politiques de conservation des données, consultez les exemples GitHub.

  • Pour plus d'informations sur l'opération API permettant de créer une politique de conservation des données, consultez InitiateBucketWorm.

  • Pour plus d'informations sur l'opération API permettant d'annuler une politique de conservation des données non verrouillée, consultez AbortBucketWorm.

  • Pour plus d'informations sur l'opération API permettant de verrouiller une politique de conservation des données, consultez CompleteBucketWorm.

  • Pour plus d'informations sur l'opération API permettant d'obtenir une politique de conservation des données, consultez GetBucketWorm.

  • Pour plus d'informations sur l'opération API permettant de prolonger la période de conservation des objets, consultez ExtendBucketWorm.