Amazon Web Services S3 support for Node.js
Support for AWS S3.
Links
Link Type | Methods from SDK V2 s3client
|
Methods from SDK V3 s3client
|
Commands from SDK V3 imported from '@aws-sdk/client-s3' |
---|---|---|---|
No Link |
|
|
|
callLink |
|
|
|
useInsertLink |
|
|
|
useDeleteLink |
|
|
|
useSelectLink |
|
|
|
useUpdateLink |
|
|
|
Examples
This code will create a S3 Bucket named “BucketTest1” on an AWS server:
//import { SNSClient } from "@aws-sdk/client-sns";
// ES5 example
const {S3Client} = require("@aws-sdk/client-s3");
// Set the AWS Region.
const REGION = "us-east-1";
// Create an Amazon S3 service client object.
const s3Client = new S3Client({ region: REGION });
export { s3Client };
const {CreateBucketCommand} = require("@aws-sdk/client-s3");
const {PutObjectCommand} = require("@aws-sdk/client-s3");
const {DeleteBucketCommand} = require("@aws-sdk/client-s3");
import {path} from "path";
import {fs} from "fs";
const file = "OBJECT_PATH_AND_NAME"; // Path to and name of object. For example '../myFiles/index.js'.
const fileStream = fs.createReadStream(file);
export const bucket = {
Bucket: "BucketTest1",
ACL : "public-read'"
};
// Create the Amazon S3 bucket.
export const runTest = async () => {
try {
const data = await s3Client.send(new CreateBucketCommand(bucket));
console.log("Success", data);
return data; // For unit tests.
} catch (err) {
console.log("Error", err);
}
};
runTest();
export const uploadParams = {
Bucket: "BucketTest1",
// Add the required 'Key' parameter using the 'path' module.
Key: path.basename(file),
// Add the required 'Body' parameter
Body: fileStream,
};
// Upload file to specified bucket.
export const runTestPut = async () => {
try {
const data = await s3Client.send(new PutObjectCommand(uploadParams));
console.log("Success", data);
return data; // For unit tests.
} catch (err) {
console.log("Error", err);
}
};
runTestPut();
// Upload file to specified bucket.
export const runTestDelete = async () => {
try {
const data = await s3Client.send(new DeleteBucketCommand(bucket));
console.log("Success", data);
return data; // For unit tests.
} catch (err) {
console.log("Error", err);
}
};
runTestDelete();
The code listed above will produce the following results:
Limitations
- Use of access points is not supported.