Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions javascriptv3/example_code/libs/utils/util-log.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import readline from "readline";

import { parseString } from "@aws-doc-sdk-examples/lib/utils/util-string.js";

const log = (str) => {
export const log = (str) => {
const parsed = parseString(str);
console.log(parsed);
return parsed;
};

export { log };
export class ProgressBar {
/**
* Create a progress bar that will display in the console.
* @param {Object} config
* @param {string} config.description - The text that will display next to the progress bar.
* @param {number} config.barLength - The length, in characters, of the progress bar.
*/
constructor({ description, barLength }) {
this._currentProgress = 0;
this._barLength = barLength;
this._description = description;
}

/**
* @param {{ current: number, total: number }} event
*/
update({ current, total }) {
this._currentProgress = current / total;
this._render();
}

_render() {
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 0);

const filledLength = Math.round(this._barLength * this._currentProgress);
const bar =
"█".repeat(filledLength) + " ".repeat(this._barLength - filledLength);

process.stdout.write(
`${this._description} [${bar}] ${this._currentProgress * 100}%`,
);

if (this._currentProgress === 1) {
process.stdout.write("\n");
}
}
}
6 changes: 3 additions & 3 deletions javascriptv3/example_code/libs/utils/util-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export const validateArgs = (config, results) => {

if (optionRequired && !optionPresent) {
errors = errors ?? [];
errors.push(`Missing required argument "${option}".`);
errors.push(`Missing required argument "--${option}".`);
}

return { errors };
}

return { errors };
};
2 changes: 1 addition & 1 deletion javascriptv3/example_code/s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Code excerpts that show you how to call individual service functions.

- [CopyObject](actions/copy-object.js#L4)
- [CreateBucket](actions/create-bucket.js#L4)
- [DeleteBucket](actions/delete-bucket.js#L6)
- [DeleteBucket](actions/delete-bucket.js#L4)
- [DeleteBucketPolicy](actions/delete-bucket-policy.js#L4)
- [DeleteBucketWebsite](actions/delete-bucket-website.js#L4)
- [DeleteObject](actions/delete-object.js#L4)
Expand Down
31 changes: 22 additions & 9 deletions javascriptv3/example_code/s3/actions/copy-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,41 @@ export const main = async ({
// snippet-end:[s3.JavaScript.buckets.copyObjectV3]

// Call function if run directly
import { fileURLToPath } from "url";
import { parseArgs } from "util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
const loadArgs = () => {
const options = {
sourceBucket: {
sourceBucketName: {
type: "string",
default: "source-bucket",
required: true,
},
sourceKey: {
type: "string",
default: "todo.txt",
required: true,
},
destinationBucket: {
type: "string",
default: "destination-bucket",
required: true,
},
destinationKey: {
type: "string",
default: "todo.txt",
required: true,
},
};
const { values } = parseArgs({ options });
main(values);
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"));
}
}
23 changes: 18 additions & 5 deletions javascriptv3/example_code/s3/actions/create-bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,29 @@ export const main = async ({ bucketName }) => {
// snippet-end:[s3.JavaScript.buckets.createBucketV3]

// Call function if run directly
import { fileURLToPath } from "url";
import { parseArgs } from "util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
default: "bucket-name",
required: true,
},
};
const { values } = parseArgs({ options });
main(values);
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"));
}
}
23 changes: 18 additions & 5 deletions javascriptv3/example_code/s3/actions/delete-bucket-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,29 @@ export const main = async ({ bucketName }) => {
// snippet-end:[s3.JavaScript.policy.deleteBucketPolicyV3]

// Call function if run directly
import { fileURLToPath } from "url";
import { parseArgs } from "util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
default: "amzn-s3-demo-bucket",
required: true,
},
};
const { values } = parseArgs({ options });
main(values);
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"));
}
}
23 changes: 18 additions & 5 deletions javascriptv3/example_code/s3/actions/delete-bucket-website.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,29 @@ export const main = async ({ bucketName }) => {
// snippet-end:[s3.JavaScript.website.deleteBucketWebsiteV3]

// Call function if run directly
import { fileURLToPath } from "url";
import { parseArgs } from "util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
default: "amzn-s3-demo-bucket",
required: true,
},
};
const { values } = parseArgs({ options });
main(values);
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"));
}
}
70 changes: 55 additions & 15 deletions javascriptv3/example_code/s3/actions/delete-bucket.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,69 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { fileURLToPath } from "url";

// snippet-start:[s3.JavaScript.buckets.deleteBucketV3]
import { DeleteBucketCommand, S3Client } from "@aws-sdk/client-s3";

const client = new S3Client({});
import {
DeleteBucketCommand,
S3Client,
S3ServiceException,
} from "@aws-sdk/client-s3";

// Delete a bucket.
export const main = async () => {
/**
* Delete an Amazon S3 bucket.
* @param {{ bucketName: string }}
*/
export const main = async ({ bucketName }) => {
const client = new S3Client({});
const command = new DeleteBucketCommand({
Bucket: "test-bucket",
Bucket: bucketName,
});

try {
const response = await client.send(command);
console.log(response);
} catch (err) {
console.error(err);
await client.send(command);
console.log(`Bucket was deleted.`);
} catch (caught) {
if (
caught instanceof S3ServiceException &&
caught.name === "NoSuchBucket"
) {
console.error(
`Error from S3 while deleting bucket. The bucket doesn't exist.`,
);
} else if (caught instanceof S3ServiceException) {
console.error(
`Error from S3 while deleting the bucket. ${caught.name}: ${caught.message}`,
);
} else {
throw caught;
}
}
};
// snippet-end:[s3.JavaScript.buckets.deleteBucketV3]

// Invoke main function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
main();
// Call function if run directly
import { parseArgs } from "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"));
}
}
25 changes: 19 additions & 6 deletions javascriptv3/example_code/s3/actions/delete-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,33 @@ export const main = async ({ bucketName, key }) => {
// snippet-end:[s3.JavaScript.buckets.deleteobjectV3]

// Call function if run directly
import { fileURLToPath } from "url";
import { parseArgs } from "util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
default: "amzn-s3-demo-bucket",
required: true,
},
key: {
type: "string",
default: "todo.txt",
required: true,
},
};
const { values } = parseArgs({ options });
main(values);
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"));
}
}
26 changes: 18 additions & 8 deletions javascriptv3/example_code/s3/actions/delete-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,29 @@ export const main = async ({ bucketName, keys }) => {
Example usage:
node delete-objects.js --bucketName amzn-s3-demo-bucket obj1.txt obj2.txt
*/
import { fileURLToPath } from "url";
import { parseArgs } from "util";
import {
isMain,
validateArgs,
} from "@aws-doc-sdk-examples/lib/utils/util-node.js";

if (process.argv[1] === fileURLToPath(import.meta.url)) {
const loadArgs = () => {
const options = {
bucketName: {
type: "string",
default: "amzn-s3-demo-bucket",
required: true,
},
};
const { values, positionals } = parseArgs({
options,
allowPositionals: true,
});
main({ ...values, keys: positionals });
const results = parseArgs({ options, allowPositionals: true });
const { errors } = validateArgs({ options }, results);
return { errors, results };
};

if (isMain(import.meta.url)) {
const { errors, results } = loadArgs();
if (!errors) {
main({ ...results.values, keys: results.positionals });
} else {
console.error(errors.join("\n"));
}
}
Loading
Loading