AWS SDK Nodejs

1. Install or include the Amazon S3 SDK

e.g. with npm

npm install --save aws-sdk
npm install --save aws-sdk

2. Import the S3 client

import S3 from "aws-sdk/clients/s3";
import S3 from "aws-sdk/clients/s3";

3. Create client object with MT credentials

const accessKeyId = "access key here";
const secretAccessKey = "secret access key here";
const endpoint = "https://gateway.storjshare.io";

const s3 = new S3({
accessKeyId,
secretAccessKey,
endpoint,
s3ForcePathStyle: true,
signatureVersion: "v4",
connectTimeout: 0,
httpOptions: { timeout: 0 }
});
const accessKeyId = "access key here";
const secretAccessKey = "secret access key here";
const endpoint = "https://gateway.storjshare.io";

const s3 = new S3({
accessKeyId,
secretAccessKey,
endpoint,
s3ForcePathStyle: true,
signatureVersion: "v4",
connectTimeout: 0,
httpOptions: { timeout: 0 }
});

4. List objects and log to console

(async () => {

const { Buckets } = await s3.listBuckets({}).promise();

console.log(Buckets);

})();
(async () => {

const { Buckets } = await s3.listBuckets({}).promise();

console.log(Buckets);

})();

5. Upload an object

(async () => {

// `file` can be a readable stream in node or a `Blob` in the browser

const params = {
Bucket: "my-bucket",
Key: "my-object",
Body: file
};

await s3.upload(params, {
partSize: 64 * 1024 * 1024
}).promise();

})();
(async () => {

// `file` can be a readable stream in node or a `Blob` in the browser

const params = {
Bucket: "my-bucket",
Key: "my-object",
Body: file
};

await s3.upload(params, {
partSize: 64 * 1024 * 1024
}).promise();

})();

6. Get URL that points to an object

The getSignedUrl function creates a cryptographically signed url. No contact with the gateway is needed here; this happens instantaneously.

const params = {
Bucket: "my-bucket",
Key: "my-object"
}

const url = s3.getSignedUrl("getObject", params);

// e.g. create an <img> where src points to url
const params = {
Bucket: "my-bucket",
Key: "my-object"
}

const url = s3.getSignedUrl("getObject", params);

// e.g. create an <img> where src points to url
Previous
AWS CLI Endpoint