Use GetObjectLegalHold
with an AWS 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 example:
- .NET
-
- AWS SDK for .NET
-
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
-
- AWS 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 amzn-s3-demo-bucket-with-object-lock
\
--key doc1.rtf
Output:
{
"LegalHold": {
"Status": "ON"
}
}
- Go
-
- SDK for Go V2
-
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
)
type S3Actions struct {
S3Client *s3.Client
S3Manager *manager.Uploader
}
func (actor S3Actions) GetObjectLegalHold(ctx context.Context, bucket string, key string, versionId string) (*types.ObjectLockLegalHoldStatus, error) {
var status *types.ObjectLockLegalHoldStatus
input := &s3.GetObjectLegalHoldInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
VersionId: aws.String(versionId),
}
output, err := actor.S3Client.GetObjectLegalHold(ctx, input)
if err != nil {
var noSuchKeyErr *types.NoSuchKey
var apiErr *smithy.GenericAPIError
if errors.As(err, &noSuchKeyErr) {
log.Printf("Object %s does not exist in bucket %s.\n", key, bucket)
err = noSuchKeyErr
} else if errors.As(err, &apiErr) {
switch apiErr.ErrorCode() {
case "NoSuchObjectLockConfiguration":
log.Printf("Object %s does not have an object lock configuration.\n", key)
err = nil
case "InvalidRequest":
log.Printf("Bucket %s does not have an object lock configuration.\n", bucket)
err = nil
}
}
} else {
status = &output.LegalHold.Status
}
return status, err
}
- Java
-
- SDK for Java 2.x
-
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;
}
- JavaScript
-
- SDK for JavaScript (v3)
-
import {
GetObjectLegalHoldCommand,
S3Client,
S3ServiceException,
} from "@aws-sdk/client-s3";
export const main = async ({ bucketName, key }) => {
const client = new S3Client({});
try {
const response = await client.send(
new GetObjectLegalHoldCommand({
Bucket: bucketName,
Key: key,
}),
);
console.log(`Legal Hold Status: ${response.LegalHold.Status}`);
} catch (caught) {
if (
caught instanceof S3ServiceException &&
caught.name === "NoSuchBucket"
) {
console.error(
`Error from S3 while getting legal hold status for ${key} in ${bucketName}. The bucket doesn't exist.`,
);
} else if (caught instanceof S3ServiceException) {
console.error(
`Error from S3 while getting legal hold status for ${key} in ${bucketName} from ${bucketName}. ${caught.name}: ${caught.message}`,
);
} else {
throw caught;
}
}
};
import { parseArgs } from "node:util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
required: true,
},
key: {
type: "string",
required: true,
},
};
const results = parseArgs({ options });
const { errors } = validateArgs({ options }, results);
return { errors, results };
};
if (isMain(import.meta.url)) {
const { errors, results } = loadArgs();
if (!errors) {
main(results.values);
} else {
console.error(errors.join("\n"));
}
}
- Python
-
- SDK for Python (Boto3)
-
Put an object legal hold.
def get_legal_hold(s3_client, bucket: str, key: str) -> None:
"""
Get the legal hold status of a specific file in a bucket.
Args:
s3_client: Boto3 S3 client.
bucket: The name of the bucket containing the file.
key: The key of the file to get the legal hold status of.
"""
print()
logger.info("Getting legal hold status of file [%s] in bucket [%s]", key, bucket)
try:
response = s3_client.get_object_legal_hold(Bucket=bucket, Key=key)
legal_hold_status = response["LegalHold"]["Status"]
logger.debug(
"Legal hold status of file [%s] in bucket [%s] is [%s]",
key,
bucket,
legal_hold_status,
)
except Exception as e:
logger.error(
"Failed to get legal hold status of file [%s] in bucket [%s]: %s",
key,
bucket,
e,
)
For a complete list of AWS SDK developer guides and code examples, see
Developing with Amazon S3 using the AWS SDKs.
This topic also includes information about getting started and details about previous SDK versions.