Tous les produits
Search
Centre de documentation

Object Storage Service:Manage versioning (OSS SDK for Python 1.0)

Dernière mise à jour :Aug 18, 2026

L'état de versioning d'un bucket s'applique à tous les objets qu'il contient. Si vous activez le versioning pour un bucket, vous pouvez récupérer n'importe quelle version antérieure d'un objet en cas de suppression ou d'écrasement accidentel.

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

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez la rubrique Régions et points de terminaison.

  • Les identifiants d'accès utilisés dans cette rubrique sont extraits des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez la rubrique Configurer les identifiants d'accès à l'aide d'OSS SDK for Python 1.0.

  • Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour découvrir d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez la rubrique Initialisation.

  • Pour définir l'état de versioning d'un bucket, vous devez disposer de l'autorisation oss:PutBucketVersioning. Pour récupérer les informations relatives à l'état de versioning d'un bucket, vous devez disposer de l'autorisation oss:GetBucketVersioning. Pour plus d'informations, consultez la rubrique Accorder une politique d'accès personnalisée à un utilisateur RAM.

Configurer le versioning pour un bucket

L'exemple de code suivant montre comment activer ou suspendre le versioning pour un bucket :

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketVersioningConfig
# 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 configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Initialize versioning configurations for the bucket. 
config = BucketVersioningConfig()
# Set the versioning state to enabled or suspended. 
config.status = oss2.BUCKET_VERSIONING_ENABLE

# Configure versioning for the bucket. 
result = bucket.put_bucket_versioning(config)
# View the HTTP status code. 
print('http response code:', result.status)

Interroger l'état de versioning d'un bucket

L'exemple de code suivant montre comment interroger l'état de versioning d'un bucket :

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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 configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Query the versioning state of the bucket. 
versioning_info = bucket.get_bucket_versioning()
# Display the versioning state of the bucket. If versioning has been enabled, Enabled or Suspended is returned. If versioning has not been enabled, None is returned. 
print('bucket versioning status:', versioning_info.status)

Références

  • Pour consulter l'exemple de code complet utilisé pour configurer le versioning d'un bucket, rendez-vous sur GitHub.

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

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