|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | | -import { fileURLToPath } from "url"; |
4 | | -import { PutObjectLegalHoldCommand, S3Client } from "@aws-sdk/client-s3"; |
| 3 | +import { |
| 4 | + PutObjectLegalHoldCommand, |
| 5 | + S3Client, |
| 6 | + S3ServiceException, |
| 7 | +} from "@aws-sdk/client-s3"; |
5 | 8 |
|
6 | 9 | /** |
7 | | - * @param {S3Client} client |
8 | | - * @param {string} bucketName |
9 | | - * @param {string} objectKey |
| 10 | + * Apply a legal hold configuration to the specified object. |
| 11 | + * @param {{ bucketName: string, objectKey: string, legalHoldStatus: "ON" | "OFF" }} |
10 | 12 | */ |
11 | | -export const main = async (client, bucketName, objectKey) => { |
| 13 | +export const main = async ({ bucketName, objectKey, legalHoldStatus }) => { |
| 14 | + if (!["OFF", "ON"].includes(legalHoldStatus.toUpperCase())) { |
| 15 | + throw new Error( |
| 16 | + "Invalid parameter. legalHoldStatus must be 'ON' or 'OFF'.", |
| 17 | + ); |
| 18 | + } |
| 19 | + |
| 20 | + const client = new S3Client({}); |
12 | 21 | const command = new PutObjectLegalHoldCommand({ |
13 | 22 | Bucket: bucketName, |
14 | 23 | Key: objectKey, |
15 | 24 | LegalHold: { |
16 | 25 | // Set the status to 'ON' to place a legal hold on the object. |
17 | 26 | // Set the status to 'OFF' to remove the legal hold. |
18 | | - Status: "ON", |
| 27 | + Status: legalHoldStatus, |
19 | 28 | }, |
20 | | - // Optionally, you can provide additional parameters |
21 | | - // ChecksumAlgorithm: "ALGORITHM", |
22 | | - // ContentMD5: "MD5_HASH", |
23 | | - // ExpectedBucketOwner: "ACCOUNT_ID", |
24 | | - // RequestPayer: "requester", |
25 | | - // VersionId: "OBJECT_VERSION_ID", |
26 | 29 | }); |
27 | 30 |
|
28 | 31 | try { |
29 | | - const response = await client.send(command); |
| 32 | + await client.send(command); |
30 | 33 | console.log( |
31 | | - `Object legal hold status: ${response.$metadata.httpStatusCode}`, |
| 34 | + `Legal hold status set to "${legalHoldStatus}" for "${objectKey}" in "${bucketName}"`, |
32 | 35 | ); |
33 | | - } catch (err) { |
34 | | - console.error(err); |
| 36 | + } catch (caught) { |
| 37 | + if ( |
| 38 | + caught instanceof S3ServiceException && |
| 39 | + caught.name === "NoSuchBucket" |
| 40 | + ) { |
| 41 | + console.error( |
| 42 | + `Error from S3 while modifying legal hold status for "${objectKey}" in "${bucketName}". The bucket doesn't exist.`, |
| 43 | + ); |
| 44 | + } else if (caught instanceof S3ServiceException) { |
| 45 | + console.error( |
| 46 | + `Error from S3 while modifying legal hold status for "${objectKey}" in "${bucketName}". ${caught.name}: ${caught.message}`, |
| 47 | + ); |
| 48 | + } else { |
| 49 | + throw caught; |
| 50 | + } |
35 | 51 | } |
36 | 52 | }; |
37 | 53 |
|
38 | | -// Invoke main function if this file was run directly. |
| 54 | +// Call function if run directly |
| 55 | +import { fileURLToPath } from "url"; |
| 56 | +import { parseArgs } from "util"; |
| 57 | + |
39 | 58 | if (process.argv[1] === fileURLToPath(import.meta.url)) { |
40 | | - main(new S3Client(), "BUCKET_NAME", "OBJECT_KEY"); |
| 59 | + const options = { |
| 60 | + bucketName: { |
| 61 | + type: "string", |
| 62 | + default: "amzn-s3-demo-bucket", |
| 63 | + }, |
| 64 | + objectKey: { |
| 65 | + type: "string", |
| 66 | + default: "file.txt", |
| 67 | + }, |
| 68 | + legalHoldStatus: { |
| 69 | + type: "string", |
| 70 | + default: "ON", |
| 71 | + }, |
| 72 | + }; |
| 73 | + const { values } = parseArgs({ options }); |
| 74 | + main(values); |
41 | 75 | } |
0 commit comments