-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathcreate-bucket.js
More file actions
77 lines (71 loc) · 2.22 KB
/
create-bucket.js
File metadata and controls
77 lines (71 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[s3.JavaScript.buckets.createBucketV3]
import {
BucketAlreadyExists,
BucketAlreadyOwnedByYou,
CreateBucketCommand,
S3Client,
waitUntilBucketExists,
} from "@aws-sdk/client-s3";
/**
* Create an Amazon S3 bucket.
* @param {{ bucketName: string }} config
*/
export const main = async ({ bucketName }) => {
const client = new S3Client({});
try {
const { Location } = await client.send(
new CreateBucketCommand({
// The name of the bucket. Bucket names are unique and have several other constraints.
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
Bucket: bucketName,
}),
);
await waitUntilBucketExists({ client }, { Bucket: bucketName });
console.log(`Bucket created with location ${Location}`);
} catch (caught) {
if (caught instanceof BucketAlreadyExists) {
console.error(
`The bucket "${bucketName}" already exists in another AWS account. Bucket names must be globally unique.`,
);
}
// WARNING: If you try to create a bucket in the North Virginia region,
// and you already own a bucket in that region with the same name, this
// error will not be thrown. Instead, the call will return successfully
// and the ACL on that bucket will be reset.
else if (caught instanceof BucketAlreadyOwnedByYou) {
console.error(
`The bucket "${bucketName}" already exists in this AWS account.`,
);
} else {
throw caught;
}
}
};
// snippet-end:[s3.JavaScript.buckets.createBucketV3]
// Call function if run directly
import { parseArgs } from "node:util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
required: true,
},
};
const results = parseArgs({ options });
const { errors } = validateArgs({ options }, results);
return { errors, results };
};
if (isMain(import.meta.url)) {
const { errors, results } = loadArgs();
if (!errors) {
main(results.values);
} else {
console.error(errors.join("\n"));
}
}