Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions,
see Getting Started with Amazon Web Services in China
(PDF).
Use GetObjectLegalHold
with an Amazon SDK or CLI
The following code examples show how to use GetObjectLegalHold
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code examples:
- .NET
-
- Amazon SDK for .NET
-
/// <summary>
/// Get the legal hold details for an S3 object.
/// </summary>
/// <param name="bucketName">The bucket of the object.</param>
/// <param name="objectKey">The object key.</param>
/// <returns>The object legal hold details.</returns>
public async Task<ObjectLockLegalHold> GetObjectLegalHold(string bucketName,
string objectKey)
{
try
{
var request = new GetObjectLegalHoldRequest()
{
BucketName = bucketName,
Key = objectKey
};
var response = await _amazonS3.GetObjectLegalHoldAsync(request);
Console.WriteLine($"\tObject legal hold for {objectKey} in {bucketName}: " +
$"\n\tStatus: {response.LegalHold.Status}");
return response.LegalHold;
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"\tUnable to fetch legal hold: '{ex.Message}'");
return new ObjectLockLegalHold();
}
}
- CLI
-
- Amazon CLI
-
Retrieves the Legal Hold status of an object
The following get-object-legal-hold
example retrieves the Legal Hold status for the specified object.
aws s3api get-object-legal-hold \
--bucket my-bucket-with-object-lock \
--key doc1.rtf
Output:
{
"LegalHold": {
"Status": "ON"
}
}
- Java
-
- SDK for Java 2.x
-
// Get the legal hold details for an S3 object.
public ObjectLockLegalHold getObjectLegalHold(String bucketName, String objectKey) {
try {
GetObjectLegalHoldRequest legalHoldRequest = GetObjectLegalHoldRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build();
GetObjectLegalHoldResponse response = getClient().getObjectLegalHold(legalHoldRequest);
System.out.println("Object legal hold for " + objectKey + " in " + bucketName +
":\n\tStatus: " + response.legalHold().status());
return response.legalHold();
} catch (S3Exception ex) {
System.out.println("\tUnable to fetch legal hold: '" + ex.getMessage() + "'");
}
return null;
}
For a complete list of Amazon SDK developer guides and code examples, see
Using this service with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.