import{
GetObjectLegalHoldCommand,
S3Client,
S3ServiceException,
} from"@aws-sdk/client-s3";
/**
* Get an object's current legal hold status.
* @param {{ bucketName: string, key: string }}
*/exportconst main = async ({ bucketName, key }) => {const client = new S3Client({});
try{const response = await client.send(
new GetObjectLegalHoldCommand({Bucket: bucketName,
Key: key,
// Optionally, you can provide additional parameters// ExpectedBucketOwner: "<account ID that is expected to own the bucket>",// VersionId: "<the specific version id of the object to check>",
}),
);
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.`,
);
} elseif (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;
}
}
};
// Call function if run directlyimport{ 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"));
}
}
defget_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,
)