O Object Storage Service (OSS) permite definir uma política de retenção baseada em tempo para um bucket. O período de retenção varia de 1 dia a 70 anos. Este tópico descreve como criar, consultar, bloquear e cancelar uma política de retenção.
Observações de uso
Os códigos de exemplo deste tópico usam o ID da região
cn-hangzhou, referente à região China (Hangzhou). Por padrão, o sistema usa um endpoint público. Para acessar o OSS a partir de outros produtos da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre os mapeamentos entre regiões e endpoints do OSS, consulte Regiões e endpoints.
Código de exemplo
Crie uma política de retenção
Use o código a seguir para criar uma política de retenção:
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.
Cancele uma política de retenção desbloqueada
Use o código a seguir para cancelar uma política de retenção desbloqueada:
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.
Bloquear uma política de retenção
Use o código a seguir para bloquear uma política de retenção:
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.
Consultar uma política de retenção
Use o código a seguir para obter os detalhes de configuração de uma política de retenção:
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.
Estender o período de proteção de um objeto
Use o código a seguir para estender o período de retenção de uma política de retenção bloqueada:
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.
Referências
Para solucionar erros comuns relacionados a políticas de retenção, consulte 27-WORM.