本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭GetObjectRetention
配使用 AWS SDK或 CLI
下列程式碼範例會示範如何使用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關詳細資訊,請參閱 GetObjectRetention(AWS SDK for .NET API參考。
-
- CLI
-
- AWS CLI
-
若要擷取物件的物件保留組態
下列
get-object-retention
範例會擷取指定物件的物件保留組態。aws s3api get-object-retention \ --bucket
my-bucket-with-object-lock
\ --keydoc1.rtf
輸出:
{ "Retention": { "Mode": "GOVERNANCE", "RetainUntilDate": "2025-01-01T00:00:00.000Z" } }
-
有API關詳細資訊,請參閱 GetObjectRetention
(AWS CLI 指令參考。
-
- Go
-
- SDK對於轉到 V2
-
注意
還有更多關於 GitHub。尋找完整的範例,並瞭解如何設定和執行 AWS 代碼示例存儲庫
。 // S3Actions wraps S3 service actions. type S3Actions struct { S3Client *s3.Client S3Manager *manager.Uploader } // GetObjectRetention retrieves the object retention configuration for an S3 object. func (actor S3Actions) GetObjectRetention(ctx context.Context, bucket string, key string) (*types.ObjectLockRetention, error) { var retention *types.ObjectLockRetention input := &s3.GetObjectRetentionInput{ Bucket: aws.String(bucket), Key: aws.String(key), } output, err := actor.S3Client.GetObjectRetention(ctx, input) if err != nil { var noKey *types.NoSuchKey var apiErr *smithy.GenericAPIError if errors.As(err, &noKey) { log.Printf("Object %s does not exist in bucket %s.\n", key, bucket) err = noKey } else if errors.As(err, &apiErr) { switch apiErr.ErrorCode() { case "NoSuchObjectLockConfiguration": err = nil case "InvalidRequest": log.Printf("Bucket %s does not have locking enabled.", bucket) err = nil } } } else { retention = output.Retention } return retention, err }
-
有API關詳細資訊,請參閱 GetObjectRetention
(AWS SDK for Go API參考。
-
- Java
-
- SDK對於爪哇 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關詳細資訊,請參閱 GetObjectRetention(AWS SDK for Java 2.x API參考。
-
- JavaScript
-
- SDK對於 JavaScript (3)
-
注意
還有更多關於 GitHub。尋找完整的範例,並瞭解如何設定和執行 AWS 代碼示例存儲庫
。 import { fileURLToPath } from "url"; import { GetObjectRetentionCommand, S3Client } from "@aws-sdk/client-s3"; /** * @param {S3Client} client * @param {string} bucketName * @param {string} objectKey */ export const main = async (client, bucketName, objectKey) => { const command = new GetObjectRetentionCommand({ Bucket: bucketName, Key: objectKey, // Optionally, you can provide additional parameters // ExpectedBucketOwner: "ACCOUNT_ID", // RequestPayer: "requester", // VersionId: "OBJECT_VERSION_ID", }); try { const { Retention } = await client.send(command); console.log(`Object Retention Settings: ${Retention.Status}`); } 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", "OBJECT_KEY"); }
-
有API關詳細資訊,請參閱 GetObjectRetention(AWS SDK for JavaScript API參考。
-
- PowerShell
-
- 適用的工具 PowerShell
-
實施例 1:該命令返回模式和日期,直到對象將被保留。
Get-S3ObjectRetention -BucketName 's3buckettesting' -Key 'testfile.txt'
-
有API關詳細資訊,請參閱 GetObjectRetention(AWS Tools for PowerShell 指令程式參照。
-
有關的完整列表 AWS SDK開發人員指南和代碼示例,請參閱搭配 AWS SDK 使用此服務。本主題也包含有關入門的資訊以及舊SDK版的詳細資訊。