Skip to content

Commit 28da151

Browse files
committed
JavaScript (v3): S3 - Standardize ListBuckets example.
1 parent 46dbd18 commit 28da151

File tree

2 files changed

+69
-21
lines changed

2 files changed

+69
-21
lines changed

javascriptv3/example_code/s3/actions/list-buckets.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,48 @@
44
import { fileURLToPath } from "url";
55

66
// snippet-start:[s3.JavaScript.buckets.listBucketsV3]
7-
import { ListBucketsCommand, S3Client } from "@aws-sdk/client-s3";
8-
9-
const client = new S3Client({});
7+
import {
8+
paginateListBuckets,
9+
S3Client,
10+
S3ServiceException,
11+
} from "@aws-sdk/client-s3";
1012

13+
/**
14+
* List the Amazon S3 buckets in your account.
15+
*/
1116
export const main = async () => {
12-
const command = new ListBucketsCommand({});
17+
const client = new S3Client({});
18+
/** @type {?import('@aws-sdk/client-s3').Owner} */
19+
let Owner = null;
20+
21+
/** @type {import('@aws-sdk/client-s3').Bucket[]} */
22+
const Buckets = [];
1323

1424
try {
15-
const { Owner, Buckets } = await client.send(command);
25+
const paginator = paginateListBuckets({ client }, {});
26+
27+
for await (const page of paginator) {
28+
if (!Owner) {
29+
Owner = page.Owner;
30+
}
31+
32+
Buckets.push(...page.Buckets);
33+
}
34+
1635
console.log(
1736
`${Owner.DisplayName} owns ${Buckets.length} bucket${
1837
Buckets.length === 1 ? "" : "s"
1938
}:`,
2039
);
2140
console.log(`${Buckets.map((b) => ` • ${b.Name}`).join("\n")}`);
22-
} catch (err) {
23-
console.error(err);
41+
} catch (caught) {
42+
if (caught instanceof S3ServiceException) {
43+
console.error(
44+
`Error from S3 while listing buckets. ${caught.name}: ${caught.message}`,
45+
);
46+
} else {
47+
throw caught;
48+
}
2449
}
2550
};
2651
// snippet-end:[s3.JavaScript.buckets.listBucketsV3]
Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
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

6-
const send = vi.fn();
7+
const paginateListBuckets = vi.fn().mockImplementation(async function* () {
8+
yield {
9+
Buckets: [{ Name: "amzn-s3-demo-bucket" }],
10+
Owner: { DisplayName: "bar" },
11+
};
12+
});
713

814
vi.doMock("@aws-sdk/client-s3", async () => {
915
const actual = await vi.importActual("@aws-sdk/client-s3");
1016
return {
1117
...actual,
12-
S3Client: class {
13-
send = send;
14-
},
18+
paginateListBuckets,
1519
};
1620
});
1721

1822
const { main } = await import("../actions/list-buckets.js");
1923

2024
describe("list-buckets", () => {
2125
it("should log the response from the service", async () => {
22-
send.mockResolvedValue({
23-
Buckets: [{ Name: "foo" }],
24-
Owner: { DisplayName: "bar" },
25-
});
26-
2726
const spy = vi.spyOn(console, "log");
2827

2928
await main();
3029

3130
expect(spy).toHaveBeenNthCalledWith(1, "bar owns 1 bucket:");
32-
expect(spy).toHaveBeenNthCalledWith(2, " • foo");
31+
expect(spy).toHaveBeenNthCalledWith(2, " • amzn-s3-demo-bucket");
3332
});
3433

35-
it("should log errors", async () => {
36-
send.mockRejectedValue("foo");
34+
it("should indicate a failure came from S3 when the error isn't generic", async () => {
35+
const error = new S3ServiceException("Some S3 service exception.");
36+
error.name = "ServiceException";
37+
const bucketName = "amzn-s3-demo-bucket";
38+
paginateListBuckets.mockImplementationOnce(
39+
// eslint-disable-next-line require-yield
40+
async function* () {
41+
throw error;
42+
},
43+
);
3744

3845
const spy = vi.spyOn(console, "error");
3946

40-
await main();
47+
await main({ bucketName, keys: ["foo"] });
48+
49+
expect(spy).toHaveBeenCalledWith(
50+
`Error from S3 while listing buckets. ${error.name}: ${error.message}`,
51+
);
52+
});
53+
54+
it("should throw errors that are not S3 specific", async () => {
55+
const bucketName = "amzn-s3-demo-bucket";
56+
paginateListBuckets.mockImplementationOnce(
57+
// eslint-disable-next-line require-yield
58+
async function* () {
59+
throw new Error();
60+
},
61+
);
4162

42-
expect(spy).toHaveBeenCalledWith("foo");
63+
await expect(() =>
64+
main({ bucketName, keys: ["foo"] }),
65+
).rejects.toBeTruthy();
4366
});
4467
});

0 commit comments

Comments
 (0)