Object Storage Service (OSS) vous permet de définir une politique de rétention temporelle pour un bucket. La période de rétention peut varier de 1 jour à 70 ans. Cette rubrique explique comment créer, interroger, verrouiller et annuler une politique de rétention.
Notes d'utilisation
Le code exemple de cette rubrique utilise l'ID de région
cn-hangzhoupour la région Chine (Hangzhou) à titre d'exemple. Par défaut, un endpoint public est utilisé. Si vous souhaitez accéder à OSS depuis d'autres produits Alibaba Cloud dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les correspondances entre les régions OSS et les endpoints, consultez Régions et endpoints.
Code exemple
Créer une politique de rétention
Utilisez le code suivant pour créer une politique de rétention :
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser and describe the script's purpose: This sample shows how to initialize the WORM configuration for an OSS bucket.
parser = argparse.ArgumentParser(description="initiate bucket worm sample")
# Add the --region command-line argument, which specifies the bucket region. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the bucket name. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --retention_period_in_days command-line argument, which specifies the retention period in days. This argument is required.
parser.add_argument('--retention_period_in_days', help='The number of days for which objects can be retained.', required=True)
def main():
# Parse the command-line arguments to get the user-provided values.
args = parser.parse_args()
# Load the access credentials for OSS from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Create a configuration object using the SDK's default configurations and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint property in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client with the configuration to interact with OSS.
client = oss.Client(cfg)
# Send a request to initialize the WORM configuration for the specified bucket.
result = client.initiate_bucket_worm(oss.InitiateBucketWormRequest(
bucket=args.bucket, # Bucket name.
initiate_worm_configuration=oss.InitiateWormConfiguration(
retention_period_in_days=int(args.retention_period_in_days), # Retention period in days, converted to an integer.
),
))
# Print the status code, request ID, and WORM ID of the operation to confirm the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' worm id: {result.worm_id}'
)
# When this script is run directly, call the main function to start the processing logic.
if __name__ == "__main__":
main() # Script entry point, where the program flow starts.
Annuler une politique de rétention non verrouillée
Utilisez le code suivant pour annuler une politique de rétention non verrouillée :
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser and describe the script's purpose: This sample shows how to abort the WORM configuration for an OSS bucket.
parser = argparse.ArgumentParser(description="abort bucket worm sample")
# Add the --region command-line argument, which specifies the bucket region. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the bucket name. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
# Parse the command-line arguments to get the user-provided values.
args = parser.parse_args()
# Load the access credentials for OSS from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Create a configuration object using the SDK's default configurations and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint property in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client with the configuration to interact with OSS.
client = oss.Client(cfg)
# Send a request to abort the WORM configuration for the specified bucket.
result = client.abort_bucket_worm(oss.AbortBucketWormRequest(
bucket=args.bucket, # Bucket name.
))
# Print the status code and request ID of the operation to confirm the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id}'
)
# When this script is run directly, call the main function to start the processing logic.
if __name__ == "__main__":
main() # Script entry point, where the program flow starts.
Verrouiller une politique de rétention
Utilisez le code suivant pour verrouiller une politique de rétention :
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser and describe the script's purpose: This sample shows how to complete the WORM configuration for an OSS bucket.
parser = argparse.ArgumentParser(description="complete bucket worm sample")
# Add the --region command-line argument, which specifies the bucket region. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the bucket name. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --worm_id command-line argument, which specifies the ID of the WORM policy to complete. This argument is required.
parser.add_argument('--worm_id', help='The ID of the retention policy.', required=True)
def main():
# Parse the command-line arguments to get the user-provided values.
args = parser.parse_args()
# Load the access credentials for OSS from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Create a configuration object using the SDK's default configurations and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint property in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client with the configuration to interact with OSS.
client = oss.Client(cfg)
# Send a request to complete the WORM configuration for the specified bucket.
result = client.complete_bucket_worm(oss.CompleteBucketWormRequest(
bucket=args.bucket, # Bucket name.
worm_id=args.worm_id, # WORM policy ID.
))
# Print the status code and request ID of the operation to confirm the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id}'
)
# When this script is run directly, call the main function to start the processing logic.
if __name__ == "__main__":
main() # Script entry point, where the program flow starts.
Récupérer une politique de rétention
Utilisez le code suivant pour récupérer les détails de configuration d'une politique de rétention :
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser and describe the script's purpose: This sample shows how to get the WORM configuration information for an OSS bucket.
parser = argparse.ArgumentParser(description="get bucket worm sample")
# Add the --region command-line argument, which specifies the bucket region. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the bucket name. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
# Parse the command-line arguments to get the user-provided values.
args = parser.parse_args()
# Load the access credentials for OSS from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Create a configuration object using the SDK's default configurations and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint property in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client with the configuration to interact with OSS.
client = oss.Client(cfg)
# Send a request to get the WORM configuration information for the specified bucket.
result = client.get_bucket_worm(oss.GetBucketWormRequest(
bucket=args.bucket, # Bucket name.
))
# Print the status code, request ID, and WORM configuration details of the operation.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' worm id: {result.worm_configuration.worm_id},' # WORM policy ID.
f' state: {result.worm_configuration.state},' # Current WORM state.
f' retention period in days: {result.worm_configuration.retention_period_in_days},' # Retention period in days.
f' creation date: {result.worm_configuration.creation_date},' # Creation date.
f' expiration date: {result.worm_configuration.expiration_date},' # Expiration date.
)
# When this script is run directly, call the main function to start the processing logic.
if __name__ == "__main__":
main() # Script entry point, where the program flow starts.
Prolonger la période de protection d'un objet
Utilisez le code suivant pour prolonger la période de rétention d'une politique de rétention verrouillée :
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser and describe the script's purpose: This sample shows how to extend the retention period of the WORM configuration for an OSS bucket.
parser = argparse.ArgumentParser(description="extend bucket worm sample")
# Add the --region command-line argument, which specifies the bucket region. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the bucket name. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --worm_id command-line argument, which specifies the ID of the WORM policy to extend. This argument is required.
parser.add_argument('--worm_id', help='The ID of the retention policy.', required=True)
# Add the --retention_period_in_days command-line argument, which specifies the new retention period in days. This argument is required.
parser.add_argument('--retention_period_in_days', help='The number of days for which objects can be retained.', required=True)
def main():
# Parse the command-line arguments to get the user-provided values.
args = parser.parse_args()
# Load the access credentials for OSS from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Create a configuration object using the SDK's default configurations and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region
# If a custom endpoint is provided, update the endpoint property in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client with the configuration to interact with OSS.
client = oss.Client(cfg)
# Send a request to extend the retention period of the WORM configuration for the specified bucket.
result = client.extend_bucket_worm(oss.ExtendBucketWormRequest(
bucket=args.bucket, # Bucket name.
worm_id=args.worm_id, # WORM policy ID.
extend_worm_configuration=oss.ExtendWormConfiguration(
retention_period_in_days=int(args.retention_period_in_days), # New retention period in days, converted to an integer.
),
))
# Print the status code and request ID of the operation to confirm the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id}'
)
# When this script is run directly, call the main function to start the processing logic.
if __name__ == "__main__":
main() # Script entry point, where the program flow starts.
Références
Pour résoudre les erreurs courantes liées aux politiques de rétention, consultez 27-WORM.