Skip to content

Commit 4f0b29c

Browse files
committed
JavaScript (v3): S3 - Standardize GetBucketWebsite example.
1 parent e74f5c0 commit 4f0b29c

File tree

2 files changed

+82
-25
lines changed

2 files changed

+82
-25
lines changed
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { fileURLToPath } from "url";
5-
64
// snippet-start:[s3.JavaScript.website.getBucketWebsiteV3]
7-
import { GetBucketWebsiteCommand, S3Client } from "@aws-sdk/client-s3";
8-
9-
const client = new S3Client({});
5+
import {
6+
GetBucketWebsiteCommand,
7+
S3Client,
8+
S3ServiceException,
9+
} from "@aws-sdk/client-s3";
1010

11-
export const main = async () => {
12-
const command = new GetBucketWebsiteCommand({
13-
Bucket: "test-bucket",
14-
});
11+
/**
12+
* Log the website configuration for a bucket.
13+
* @param {{ bucketName }}
14+
*/
15+
export const main = async ({ bucketName }) => {
16+
const client = new S3Client({});
1517

1618
try {
17-
const { ErrorDocument, IndexDocument } = await client.send(command);
19+
const response = await client.send(
20+
new GetBucketWebsiteCommand({
21+
Bucket: bucketName,
22+
}),
23+
);
1824
console.log(
19-
`Your bucket is set up to host a website. It has an error document:`,
20-
`${ErrorDocument.Key}, and an index document: ${IndexDocument.Suffix}.`,
25+
`Your bucket is set up to host a website with the following configuration:\n${JSON.stringify(response, null, 2)}`,
2126
);
22-
} catch (err) {
23-
console.error(err);
27+
} catch (caught) {
28+
if (
29+
caught instanceof S3ServiceException &&
30+
caught.name === "NoSuchWebsiteConfiguration"
31+
) {
32+
console.error(
33+
`Error from S3 while getting website configuration for ${bucketName}. The bucket isn't configured as a website.`,
34+
);
35+
} else if (caught instanceof S3ServiceException) {
36+
console.error(
37+
`Error from S3 while getting website configuration for ${bucketName}. ${caught.name}: ${caught.message}`,
38+
);
39+
} else {
40+
throw caught;
41+
}
2442
}
2543
};
2644
// snippet-end:[s3.JavaScript.website.getBucketWebsiteV3]
2745

28-
// Invoke main function if this file was run directly.
46+
// Call function if run directly
47+
import { fileURLToPath } from "url";
48+
import { parseArgs } from "util";
49+
2950
if (process.argv[1] === fileURLToPath(import.meta.url)) {
30-
main();
51+
const options = {
52+
bucketName: {
53+
type: "string",
54+
default: "amzn-s3-demo-bucket",
55+
},
56+
};
57+
const { values } = parseArgs({ options });
58+
main(values);
3159
}

javascriptv3/example_code/s3/tests/get-bucket-website.unit.test.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import { S3ServiceException } from "@aws-sdk/client-s3";
45
import { describe, it, expect, vi } from "vitest";
56

67
const send = vi.fn();
@@ -19,28 +20,56 @@ const { main } = await import("../actions/get-bucket-website.js");
1920

2021
describe("get-bucket-website", () => {
2122
it("should log the response from the service", async () => {
22-
send.mockResolvedValue({
23+
const mockResponse = {
2324
IndexDocument: { Suffix: "foo" },
2425
ErrorDocument: { Key: "bar" },
25-
});
26+
};
27+
send.mockResolvedValue(mockResponse);
28+
const bucketName = "amzn-s3-demo-bucket";
2629

2730
const spy = vi.spyOn(console, "log");
2831

29-
await main();
32+
await main({ bucketName });
3033

3134
expect(spy).toHaveBeenCalledWith(
32-
"Your bucket is set up to host a website. It has an error document:",
33-
"bar, and an index document: foo.",
35+
`Your bucket is set up to host a website with the following configuration:\n${JSON.stringify(mockResponse, null, 2)}`,
3436
);
3537
});
3638

37-
it("should log errors", async () => {
38-
send.mockRejectedValue("foo");
39+
it("should log a relevant error when the bucket isn't configured as a website.", async () => {
40+
const error = new S3ServiceException("Not such website configuration.");
41+
error.name = "NoSuchWebsiteConfiguration";
42+
const bucketName = "amzn-s3-demo-bucket";
43+
send.mockRejectedValueOnce(error);
3944

4045
const spy = vi.spyOn(console, "error");
4146

42-
await main();
47+
await main({ bucketName });
4348

44-
expect(spy).toHaveBeenCalledWith("foo");
49+
expect(spy).toHaveBeenCalledWith(
50+
`Error from S3 while getting website configuration for ${bucketName}. The bucket isn't configured as a website.`,
51+
);
52+
});
53+
54+
it("should indicate a failure came from S3 when the error isn't generic", async () => {
55+
const error = new S3ServiceException("Some S3 service exception.");
56+
error.name = "ServiceException";
57+
const bucketName = "amzn-s3-demo-bucket";
58+
send.mockRejectedValueOnce(error);
59+
60+
const spy = vi.spyOn(console, "error");
61+
62+
await main({ bucketName });
63+
64+
expect(spy).toHaveBeenCalledWith(
65+
`Error from S3 while getting website configuration for ${bucketName}. ${error.name}: ${error.message}`,
66+
);
67+
});
68+
69+
it("should throw errors that are not S3 specific", async () => {
70+
const bucketName = "amzn-s3-demo-bucket";
71+
send.mockRejectedValueOnce(new Error());
72+
73+
await expect(() => main({ bucketName })).rejects.toBeTruthy();
4574
});
4675
});

0 commit comments

Comments
 (0)