Skip to content

Commit

Permalink
Merge pull request awsdocs#1635 from awsdocs/putbuckeacl
Browse files Browse the repository at this point in the history
update s3 upload object examples
  • Loading branch information
brmur authored Feb 4, 2021
2 parents fbb568b + 33b0333 commit 4462a49
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
4 changes: 2 additions & 2 deletions javascriptv3/example_code/s3/src/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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]

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -51,5 +46,6 @@ const run = async () => {
}
};
run();

// snippet-end:[s3.JavaScript.buckets.uploadV3]

Original file line number Diff line number Diff line change
@@ -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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4462a49

Please sign in to comment.