From a88541614ae846aaca7d50a6512f8c98e5e9931f Mon Sep 17 00:00:00 2001 From: Brian Murray Date: Thu, 4 Feb 2021 14:02:56 +0000 Subject: [PATCH 1/2] updates to s3 upload examples --- ...and.ts => s3_create_and_upload_objects.ts} | 31 ++++++++++------ .../{s3_putobject.ts => s3_upload_object.ts} | 36 +++++++++---------- 2 files changed, 36 insertions(+), 31 deletions(-) rename javascriptv3/example_code/s3/src/{s3_upload_putcommand.ts => s3_create_and_upload_objects.ts} (56%) rename javascriptv3/example_code/s3/src/{s3_putobject.ts => s3_upload_object.ts} (56%) diff --git a/javascriptv3/example_code/s3/src/s3_upload_putcommand.ts b/javascriptv3/example_code/s3/src/s3_create_and_upload_objects.ts similarity index 56% rename from javascriptv3/example_code/s3/src/s3_upload_putcommand.ts rename to javascriptv3/example_code/s3/src/s3_create_and_upload_objects.ts index f47db597b8f..e7bf05280fe 100644 --- a/javascriptv3/example_code/s3/src/s3_upload_putcommand.ts +++ b/javascriptv3/example_code/s3/src/s3_create_and_upload_objects.ts @@ -2,16 +2,18 @@ SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at -https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-started-nodejs.html. +https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-example-creating-buckets.html. + Purpose: -s3_upload_putcommand.ts uploads a file to an S3 bucket. +s3_create_and_upload_objects.ts creates and uploads an object to an Amazon Simple Storage Solution (Amazon S3) bucket. Inputs (in the commandline input below): - REGION - BUCKET_NAME -- KEY The name of the file to upload. -- BODY (in the commandline input below): The contents of the uploaded file. Leave blank/remove to retain contents of original file. +- KEY: The name of the object to create and upload. +- BODY: The contents of the uploaded file. + Running the code: -ts-node s3_upload_putcommand.ts +ts-node s3_create_and_upload_object.ts */ // snippet-start:[s3.JavaScript.buckets.upload_putcommandV3] @@ -21,24 +23,31 @@ const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3"); // Set the AWS region const REGION = "REGION"; //e.g. "us-east-1" -// Set the parameters -const uploadParams = { Bucket: "BUCKET_NAME", Key: "KEY", Body: "BODY" }; //BUCKET_NAME, KEY (the name of the selected file), -// BODY (the contents of the uploaded file) +// Set the parameters. +const uploadParams = { + Bucket: "BUCKET_NAME", + // Specify the name of the new object. For example, 'index.html'. + // To create a directory for the object, use '/'. For example, 'myApp/package.json'. + Key: "OBJECT_NAME", + // Content of the new object. + Body: "BODY" +}; -// Create S3 service object +// Create Amazon S3 service client object. const s3 = new S3Client({ region: REGION }); -// call S3 to retrieve upload file to specified bucket +// Create and upload the object to the specified Amazon S3 bucket. const run = async () => { try { const data = await s3.send(new PutObjectCommand(uploadParams)); console.log( - "Successfully uploaded to " + uploadParams.Bucket + "/" + uploadParams.Key + "Successfully uploaded object: " + uploadParams.Bucket + "/" + uploadParams.Key ); } catch (err) { console.log("Error", err); } }; run(); + // snippet-end:[s3.JavaScript.buckets.upload_putcommandV3] diff --git a/javascriptv3/example_code/s3/src/s3_putobject.ts b/javascriptv3/example_code/s3/src/s3_upload_object.ts similarity index 56% rename from javascriptv3/example_code/s3/src/s3_putobject.ts rename to javascriptv3/example_code/s3/src/s3_upload_object.ts index a269ac54e13..c26a40ea4bb 100644 --- a/javascriptv3/example_code/s3/src/s3_putobject.ts +++ b/javascriptv3/example_code/s3/src/s3_upload_object.ts @@ -3,46 +3,41 @@ SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-example-creating-buckets.html. + Purpose: -s3_putobject.ts uploads a file to an S3 bucket. +s3_putobject.ts uploads an existing file to an Amazon Simple Storage Service (Amazon S3) bucket. + Inputs (replace in code): -- BUCKET_NAME -- KEY -- BODY -- FILE_NAME - REGION +- BUCKET_NAME +- OBJECT_PATH_AND_NAME: Relative path and name of object. For example '../myFiles/index.js'. + Running the code: ts-node s3_putobject.ts + [Outputs | Returns]: Uploads the specified file to the specified bucket. + */ // snippet-start:[s3.JavaScript.buckets.uploadV3] -// Import required AWS SDK clients and commands for Node.js +// Import required AWS SDK clients and commands for Node.js. const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3"); const path = require("path"); -const fs = require("fs"); -// Set the AWS region +// Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Set the parameters -const uploadParams = { Bucket: "BUCKET_NAME", Key: "KEY", Body: "BODY" }; //BUCKET_NAME, KEY (the name of the selected file), -// BODY (the contents of the uploaded file - leave blank/remove to retain contents of original file.) -const file = "FILE_NAME"; //FILE_NAME (the name of the file to upload (if you don't specify KEY)) +const uploadParams = { Bucket: "BUCKET_NAME" }; +const file = "OBJECT_PATH_AND_NAME"; // Path to and name of object. For example '../myFiles/index.js'. -// Create S3 service object +// Create an Amazon S3 service client object. const s3 = new S3Client({ region: REGION }); -// call S3 to retrieve upload file to specified bucket +// Upload file to specified bucket. const run = async () => { - // Configure the file stream and obtain the upload parameters - const fileStream = fs.createReadStream(file); - fileStream.on("error", function (err) { - console.log("File Error", err); - }); - uploadParams.Body = fileStream; + // Add the required 'Key' parameter using the 'path' module. uploadParams.Key = path.basename(file); - // call S3 to retrieve upload file to specified bucket try { const data = await s3.send(new PutObjectCommand(uploadParams)); console.log("Success", data); @@ -51,5 +46,6 @@ const run = async () => { } }; run(); + // snippet-end:[s3.JavaScript.buckets.uploadV3] From 33b0333a08655eaa16e8389de168f134b94ca26a Mon Sep 17 00:00:00 2001 From: Brian Murray Date: Thu, 4 Feb 2021 14:09:18 +0000 Subject: [PATCH 2/2] updates to s3 upload examples --- javascriptv3/example_code/s3/src/metadata.yaml | 4 ++-- .../tests/s3/s3_create_and_upload_object.test.js | 13 +++++++++++++ ...load_comand.test.js => s3_upload_object.test.js} | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 javascriptv3/example_code/tests/s3/s3_create_and_upload_object.test.js rename javascriptv3/example_code/tests/s3/{s3_upload_comand.test.js => s3_upload_object.test.js} (81%) diff --git a/javascriptv3/example_code/s3/src/metadata.yaml b/javascriptv3/example_code/s3/src/metadata.yaml index 3695845dde9..3493e53b933 100644 --- a/javascriptv3/example_code/s3/src/metadata.yaml +++ b/javascriptv3/example_code/s3/src/metadata.yaml @@ -52,10 +52,10 @@ files: - path: s3_list1000plusobjects.ts services: - s3 - - path: s3_putobject.ts + - path: s3_create_and_upload_object.ts services: - s3 - - path: s3_upload_putcommand.ts + - path: s3_upload_object.ts services: - s3 - path: s3_put_presignedURL.ts diff --git a/javascriptv3/example_code/tests/s3/s3_create_and_upload_object.test.js b/javascriptv3/example_code/tests/s3/s3_create_and_upload_object.test.js new file mode 100644 index 00000000000..7550edcba9d --- /dev/null +++ b/javascriptv3/example_code/tests/s3/s3_create_and_upload_object.test.js @@ -0,0 +1,13 @@ +const mockCreateAndUploadObject = jest.fn(); +jest.mock("@aws-sdk/client-s3/commands/PutObjectCommand", () => ({ + S3: function S3() { + this.PutObjectCommand = mockCreateAndUploadObject; + }, +})); +const { bucketParams, run } = require("../../s3/src/s3_create_and_upload_objects"); + +test("has to mock S3#createanduploadObject", async (done) => { + await run(); + expect(mockCreateAndUploadObject).toHaveBeenCalled; + done(); +}); diff --git a/javascriptv3/example_code/tests/s3/s3_upload_comand.test.js b/javascriptv3/example_code/tests/s3/s3_upload_object.test.js similarity index 81% rename from javascriptv3/example_code/tests/s3/s3_upload_comand.test.js rename to javascriptv3/example_code/tests/s3/s3_upload_object.test.js index 1b4f0c5f5fa..404a1210ce6 100644 --- a/javascriptv3/example_code/tests/s3/s3_upload_comand.test.js +++ b/javascriptv3/example_code/tests/s3/s3_upload_object.test.js @@ -4,7 +4,7 @@ jest.mock("@aws-sdk/client-s3/commands/PutObjectCommand", () => ({ this.PutObjectCommand = mockCreateBucket; }, })); -const { bucketParams, run } = require("../../s3/s3_setbucketpolicy"); +const { bucketParams, run } = require("../../s3/src/s3_upload_object"); test("has to mock S3#uploadObject", async (done) => { await run();