To upload a file to Amazon S3 with Node.js, we call the putObject
method.
For instance, we write
const AWS = require("aws-sdk");
AWS.config.update({
accessKeyId: "accessKeyId",
secretAccessKey: "secretAccessKey",
region: "region",
});
const params = {
Bucket: "yourBucketName",
Key: "someUniqueKey",
Body: "someFile",
};
try {
const uploadPromise = await new AWS.S3().putObject(params).promise();
console.log("Successfully uploaded data to bucket");
} catch (e) {
console.log("Error uploading data: ", e);
}
to call update
to connect to AWS.
Then we call putObject
with an object with the Bucket
, Key
and Body
to start the upload.
And then we use await
to wait for the promise to finish running.