将 GetObjectRetention 与 AWS SDK 或命令行工具结合使用 - Amazon Simple Storage Service

GetObjectRetention 与 AWS SDK 或命令行工具结合使用

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

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

.NET
AWS SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

/// <summary> /// Get the retention period for an S3 object. /// </summary> /// <param name="bucketName">The bucket of the object.</param> /// <param name="objectKey">The object key.</param> /// <returns>The object retention details.</returns> public async Task<ObjectLockRetention> GetObjectRetention(string bucketName, string objectKey) { try { var request = new GetObjectRetentionRequest() { BucketName = bucketName, Key = objectKey }; var response = await _amazonS3.GetObjectRetentionAsync(request); Console.WriteLine($"\tObject retention for {objectKey} in {bucketName}: " + $"\n\t{response.Retention.Mode} until {response.Retention.RetainUntilDate:d}."); return response.Retention; } catch (AmazonS3Exception ex) { Console.WriteLine($"\tUnable to fetch object lock retention: '{ex.Message}'"); return new ObjectLockRetention(); } }
  • 有关 API 详细信息,请参阅《AWS SDK for .NET API 参考》中的 GetObjectRetention

CLI
AWS CLI

检索对象的对象保留配置

以下 get-object-retention 示例检索指定对象的对象保留配置。

aws s3api get-object-retention \ --bucket my-bucket-with-object-lock \ --key doc1.rtf

输出:

{ "Retention": { "Mode": "GOVERNANCE", "RetainUntilDate": "2025-01-01T00:00:00.000Z" } }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetObjectRetention

Java
SDK for Java 2.x
注意

查看 GitHub,了解更多信息。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

// Get the retention period for an S3 object. public ObjectLockRetention getObjectRetention(String bucketName, String key){ try { GetObjectRetentionRequest retentionRequest = GetObjectRetentionRequest.builder() .bucket(bucketName) .key(key) .build(); GetObjectRetentionResponse response = getClient().getObjectRetention(retentionRequest); System.out.println("tObject retention for "+key +" in "+ bucketName +": " + response.retention().mode() +" until "+ response.retention().retainUntilDate() +"."); return response.retention(); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); return null; } }
  • 有关 API 详细信息,请参阅《AWS SDK for Java 2.x API 参考》中的 GetObjectRetention

PowerShell
适用于 PowerShell 的工具

示例 1:该命令返回保留对象之前的模式和日期。

Get-S3ObjectRetention -BucketName 's3buckettesting' -Key 'testfile.txt'
  • 有关 API 详细信息,请参阅《AWS Tools for PowerShell Cmdlet 参考》中的 GetObjectRetention

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