将 GetObjectLockConfiguration 与 Amazon SDK 或 CLI 配合使用 - Amazon Simple Storage Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

GetObjectLockConfiguration 与 Amazon SDK 或 CLI 配合使用

以下代码示例演示如何使用 GetObjectLockConfiguration

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

/// <summary> /// Get the object lock configuration details for an S3 bucket. /// </summary> /// <param name="bucketName">The bucket to get details.</param> /// <returns>The bucket's object lock configuration details.</returns> public async Task<ObjectLockConfiguration> GetBucketObjectLockConfiguration(string bucketName) { try { var request = new GetObjectLockConfigurationRequest() { BucketName = bucketName }; var response = await _amazonS3.GetObjectLockConfigurationAsync(request); Console.WriteLine($"\tBucket object lock config for {bucketName} in {bucketName}: " + $"\n\tEnabled: {response.ObjectLockConfiguration.ObjectLockEnabled}" + $"\n\tRule: {response.ObjectLockConfiguration.Rule?.DefaultRetention}"); return response.ObjectLockConfiguration; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to fetch object lock config: '{ex.Message}'"); return new ObjectLockConfiguration(); } }
CLI
Amazon CLI

检索存储桶的对象锁定配置

以下 get-object-lock-configuration 示例检索指定存储桶的对象锁定配置。

aws s3api get-object-lock-configuration \ --bucket my-bucket-with-object-lock

输出:

{ "ObjectLockConfiguration": { "ObjectLockEnabled": "Enabled", "Rule": { "DefaultRetention": { "Mode": "COMPLIANCE", "Days": 50 } } } }
Go
适用于 Go V2 的 SDK
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

// S3Actions wraps S3 service actions. type S3Actions struct { S3Client *s3.Client S3Manager *manager.Uploader } // GetObjectLockConfiguration retrieves the object lock configuration for an S3 bucket. func (actor S3Actions) GetObjectLockConfiguration(ctx context.Context, bucket string) (*types.ObjectLockConfiguration, error) { var lockConfig *types.ObjectLockConfiguration input := &s3.GetObjectLockConfigurationInput{ Bucket: aws.String(bucket), } output, err := actor.S3Client.GetObjectLockConfiguration(ctx, input) if err != nil { var noBucket *types.NoSuchBucket var apiErr *smithy.GenericAPIError if errors.As(err, &noBucket) { log.Printf("Bucket %s does not exist.\n", bucket) err = noBucket } else if errors.As(err, &apiErr) && apiErr.ErrorCode() == "ObjectLockConfigurationNotFoundError" { log.Printf("Bucket %s does not have an object lock configuration.\n", bucket) err = nil } } else { lockConfig = output.ObjectLockConfiguration } return lockConfig, err }
Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

// Get the object lock configuration details for an S3 bucket. public void getBucketObjectLockConfiguration(String bucketName) { GetObjectLockConfigurationRequest objectLockConfigurationRequest = GetObjectLockConfigurationRequest.builder() .bucket(bucketName) .build(); GetObjectLockConfigurationResponse response = getClient().getObjectLockConfiguration(objectLockConfigurationRequest); System.out.println("Bucket object lock config for "+bucketName +": "); System.out.println("\tEnabled: "+response.objectLockConfiguration().objectLockEnabled()); System.out.println("\tRule: "+ response.objectLockConfiguration().rule().defaultRetention()); }
JavaScript
适用于 JavaScript 的 SDK(v3)
注意

查看 GitHub,了解更多信息。例如,如果您将大小为 128 KB 到 1024 KB 的对象设置为从 S3 标准存储类移到 S3标准-IA 存储类,则大小精确为 1024 KB 和 128 KB 的对象将不会转换到 S3 标准-IA。

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { fileURLToPath } from "url"; import { GetObjectLockConfigurationCommand, S3Client, } from "@aws-sdk/client-s3"; /** * @param {S3Client} client * @param {string} bucketName */ export const main = async (client, bucketName) => { const command = new GetObjectLockConfigurationCommand({ Bucket: bucketName, // Optionally, you can provide additional parameters // ExpectedBucketOwner: "ACCOUNT_ID", }); try { const { ObjectLockConfiguration } = await client.send(command); console.log(`Object Lock Configuration: ${ObjectLockConfiguration}`); } catch (err) { console.error(err); } }; // Invoke main function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { main(new S3Client(), "BUCKET_NAME"); }
PowerShell
适用于 PowerShell 的工具

示例 1:如果为给定的 S3 存储桶启用了对象锁定配置,则此命令将返回值“Enabled”。

Get-S3ObjectLockConfiguration -BucketName 's3buckettesting' -Select ObjectLockConfiguration.ObjectLockEnabled

输出:

Value ----- Enabled

有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。