Skip to content

Commit 480c2ad

Browse files
committed
JavaScript (v3): S3 - Standardize PutObjectLegalHold example and flip ouput of unique name util.
1 parent 28da151 commit 480c2ad

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

javascriptv3/example_code/libs/utils/util-string.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { v4 as uuidv4 } from "uuid";
55
/**
66
* @param {string} name
77
*/
8-
export const getUniqueName = (name) => `${uuidv4()}-${name.toLowerCase()}`;
8+
export const getUniqueName = (name) => `${name.toLowerCase()}-${uuidv4()}`;
99

1010
/**
1111
* @param {int} length
Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,75 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// 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";
58

69
/**
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" }}
1012
*/
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({});
1221
const command = new PutObjectLegalHoldCommand({
1322
Bucket: bucketName,
1423
Key: objectKey,
1524
LegalHold: {
1625
// Set the status to 'ON' to place a legal hold on the object.
1726
// Set the status to 'OFF' to remove the legal hold.
18-
Status: "ON",
27+
Status: legalHoldStatus,
1928
},
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",
2629
});
2730

2831
try {
29-
const response = await client.send(command);
32+
await client.send(command);
3033
console.log(
31-
`Object legal hold status: ${response.$metadata.httpStatusCode}`,
34+
`Legal hold status set to "${legalHoldStatus}" for "${objectKey}" in "${bucketName}"`,
3235
);
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+
}
3551
}
3652
};
3753

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+
3958
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);
4175
}

javascriptv3/example_code/s3/tests/get-object-lock-configuration.integration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js";
1515
import { legallyEmptyAndDeleteBuckets } from "../libs/s3Utils.js";
1616

1717
const client = new S3Client({});
18-
const bucketName = getUniqueName("amzn-s3-demo-bucket");
18+
const bucketName = getUniqueName("code-example");
1919

2020
describe("get-object-lock-configuration.js Integration Test", () => {
2121
afterAll(async () => {

javascriptv3/example_code/s3/tests/put-object-legal-hold.integration.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { main as putObjectLegalHold } from "../actions/put-object-legal-hold.js"
1313
import { legallyEmptyAndDeleteBuckets } from "../libs/s3Utils.js";
1414

1515
const client = new S3Client({});
16-
const bucketName = getUniqueName("test-bucket");
17-
const objectKey = "test-object";
16+
const bucketName = getUniqueName("code-example");
17+
const objectKey = "file.txt";
1818

1919
describe("put-object-legal-hold.js Integration Test", () => {
2020
afterAll(async () => {
@@ -34,13 +34,13 @@ describe("put-object-legal-hold.js Integration Test", () => {
3434
new PutObjectCommand({
3535
Bucket: bucketName,
3636
Key: objectKey,
37-
Body: "test content",
37+
Body: "content",
3838
}),
3939
);
4040

4141
// Execute
4242
const spy = vi.spyOn(console, "error");
43-
await putObjectLegalHold(client, bucketName, objectKey);
43+
await putObjectLegalHold({ bucketName, objectKey, legalHoldStatus: "ON" });
4444
expect(spy).not.toHaveBeenCalled();
4545

4646
// Verify

javascriptv3/example_code/s3/tests/put-object-retention.integration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js";
1313
import { legallyEmptyAndDeleteBuckets } from "../libs/s3Utils.js";
1414

1515
const client = new S3Client({});
16-
const bucketName = getUniqueName("test-bucket");
16+
const bucketName = getUniqueName("code-example");
1717
const objectKey = "test-object";
1818

1919
describe("put-object-retention.js Integration Test", () => {

0 commit comments

Comments
 (0)