Skip to content

Commit ec13f56

Browse files
committed
JavaScript (v3): S3 - Standardize PutObjectLockConfiguration example.
1 parent 480c2ad commit ec13f56

File tree

5 files changed

+65
-20
lines changed

5 files changed

+65
-20
lines changed

javascriptv3/example_code/libs/tests/util-string.unit.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ describe("util-string", () => {
1616
const u2 = getUniqueName(value);
1717
expect(u1).not.toEqual(u2);
1818
});
19+
20+
it("should return undefined if a falsy value is passed in", () => {
21+
expect(getUniqueName()).toBeUndefined();
22+
expect(getUniqueName("")).toBeUndefined();
23+
expect(getUniqueName(0)).toBeUndefined();
24+
});
1925
});
2026

2127
describe("postfix", () => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3+
import { fileURLToPath } from "url";
34

45
export const getEnv = (/** @type {string} */ key) => process.env[key];
56
export const setEnv = (/** @type {string} */ key, value) => {
67
process.env[key] = value;
78
};
9+
10+
/**
11+
* Check if the running file was run directly.
12+
* @param {string | URL} fileUrl
13+
*/
14+
export const isMain = (fileUrl) => process.argv[1] === fileURLToPath(fileUrl);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { v4 as uuidv4 } from "uuid";
55
/**
66
* @param {string} name
77
*/
8-
export const getUniqueName = (name) => `${name.toLowerCase()}-${uuidv4()}`;
8+
export const getUniqueName = (name) => {
9+
if (!name) {
10+
return;
11+
}
12+
13+
return `${name.toLowerCase()}-${uuidv4()}`;
14+
};
915

1016
/**
1117
* @param {int} length
Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { fileURLToPath } from "url";
43
import {
54
PutObjectLockConfigurationCommand,
65
S3Client,
6+
S3ServiceException,
77
} from "@aws-sdk/client-s3";
88

99
/**
10-
* @param {S3Client} client
11-
* @param {string} bucketName
10+
* Enable S3 Object Lock for an Amazon S3 bucket.
11+
* After you enable Object Lock on a bucket, you can't
12+
* disable Object Lock or suspend versioning for that bucket.
13+
* @param {{ bucketName: string, enabled: boolean }}
1214
*/
13-
export const main = async (client, bucketName) => {
15+
export const main = async ({ bucketName }) => {
16+
const client = new S3Client({});
1417
const command = new PutObjectLockConfigurationCommand({
1518
Bucket: bucketName,
1619
// The Object Lock configuration that you want to apply to the specified bucket.
1720
ObjectLockConfiguration: {
1821
ObjectLockEnabled: "Enabled",
1922
},
20-
// Optionally, you can provide additional parameters
21-
// ExpectedBucketOwner: "ACCOUNT_ID",
22-
// RequestPayer: "requester",
23-
// Token: "OPTIONAL_TOKEN",
2423
});
2524

2625
try {
27-
const response = await client.send(command);
28-
console.log(
29-
`Object Lock Configuration updated: ${response.$metadata.httpStatusCode}`,
30-
);
31-
} catch (err) {
32-
console.error(err);
26+
await client.send(command);
27+
console.log(`Object Lock for "${bucketName}" enabled.`);
28+
} catch (caught) {
29+
if (
30+
caught instanceof S3ServiceException &&
31+
caught.name === "NoSuchBucket"
32+
) {
33+
console.error(
34+
`Error from S3 while modifying the object lock configuration for the bucket "${bucketName}". The bucket doesn't exist.`,
35+
);
36+
} else if (caught instanceof S3ServiceException) {
37+
console.error(
38+
`Error from S3 while modifying the object lock configuration for the bucket "${bucketName}". ${caught.name}: ${caught.message}`,
39+
);
40+
} else {
41+
throw caught;
42+
}
3343
}
3444
};
3545

36-
// Invoke main function if this file was run directly.
37-
if (process.argv[1] === fileURLToPath(import.meta.url)) {
38-
main(new S3Client(), "BUCKET_NAME");
46+
// Call function if run directly
47+
import { parseArgs } from "util";
48+
import { isMain } from "@aws-doc-sdk-examples/lib/utils/util-node.js";
49+
50+
const loadArgs = () => {
51+
const options = {
52+
bucketName: {
53+
type: "string",
54+
default: process.env["S3_BUCKET_NAME"] ?? "",
55+
},
56+
};
57+
return parseArgs({ options });
58+
};
59+
60+
if (isMain(import.meta.url)) {
61+
const { values } = loadArgs();
62+
main(values);
3963
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ 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("test-bucket");
18+
const bucketName = getUniqueName(
19+
process.env["S3_BUCKET_NAME"] || "object-lock-integ",
20+
);
1921

2022
describe("put-object-lock-configuration.js Integration Test", () => {
2123
afterAll(async () => {
@@ -38,7 +40,7 @@ describe("put-object-lock-configuration.js Integration Test", () => {
3840

3941
// Execute
4042
const spy = vi.spyOn(console, "error");
41-
await putObjectLockConfiguration(client, bucketName);
43+
await putObjectLockConfiguration({ bucketName });
4244
expect(spy).not.toHaveBeenCalled();
4345

4446
// Verify

0 commit comments

Comments
 (0)