搭GetBucketLifecycleConfiguration配 AWS SDK或使用 CLI - Amazon Simple Storage Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

GetBucketLifecycleConfiguration配 AWS SDK或使用 CLI

下列程式碼範例會示範如何使用GetBucketLifecycleConfiguration

.NET
AWS SDK for .NET
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/// <summary> /// Returns a configuration object for the supplied bucket name. /// </summary> /// <param name="client">The S3 client object used to call /// the GetLifecycleConfigurationAsync method.</param> /// <param name="bucketName">The name of the S3 bucket for which a /// configuration will be created.</param> /// <returns>Returns a new LifecycleConfiguration object.</returns> public static async Task<LifecycleConfiguration> RetrieveLifecycleConfigAsync(IAmazonS3 client, string bucketName) { var request = new GetLifecycleConfigurationRequest() { BucketName = bucketName, }; var response = await client.GetLifecycleConfigurationAsync(request); var configuration = response.Configuration; return configuration; }
CLI
AWS CLI

下列命令會擷取名為的值區的生命週期組態my-bucket

aws s3api get-bucket-lifecycle-configuration --bucket my-bucket

輸出:

{ "Rules": [ { "ID": "Move rotated logs to Glacier", "Prefix": "rotated/", "Status": "Enabled", "Transitions": [ { "Date": "2015-11-10T00:00:00.000Z", "StorageClass": "GLACIER" } ] }, { "Status": "Enabled", "Prefix": "", "NoncurrentVersionTransitions": [ { "NoncurrentDays": 0, "StorageClass": "GLACIER" } ], "ID": "Move old versions to Glacier" } ] }
Python
SDK對於 Python(肉毒桿菌 3)
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class BucketWrapper: """Encapsulates S3 bucket actions.""" def __init__(self, bucket): """ :param bucket: A Boto3 Bucket resource. This is a high-level resource in Boto3 that wraps bucket actions in a class-like structure. """ self.bucket = bucket self.name = bucket.name def get_lifecycle_configuration(self): """ Get the lifecycle configuration of the bucket. :return: The lifecycle rules of the specified bucket. """ try: config = self.bucket.LifecycleConfiguration() logger.info( "Got lifecycle rules %s for bucket '%s'.", config.rules, self.bucket.name, ) except: logger.exception( "Couldn't get lifecycle rules for bucket '%s'.", self.bucket.name ) raise else: return config.rules

如需 AWS SDK開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS SDK 使用此服務。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。