s3: made S3 upload API coherent
const AWS = require('aws-sdk')
-export function configureClient() {
- // Configure client for use with Spaces
- const spacesEndpoint = new AWS.Endpoint('nyc3.digitaloceanspaces.com');
- return new AWS.S3({
- endpoint: spacesEndpoint,
+class S3 {
+ constructor() {
+ this.endpoint = new AWS.Endpoint("");
+ this.accessKeyId = "";
+ this.secretAccesskey = "";
+
+ this.s3 = null;
+ }
+
+ setCredentials(endpoint, accessKeyId, secretAccessKey) {
+ this.endpoint = new AWS.Endpoint(endpoint);
+ this.accessKeyId = accessKeyId;
+ this.secretAccessKey = secretAccessKey;
+
+ this.s3 =
+ new AWS.S3({
+ endpoint: this.endpoint,
credentials: new AWS.Credentials({
- accessKeyId: 'ACCESS_KEY',
- secretAccessKey: 'SECRET_KEY'
+ accessKeyId: this.accessKeyId,
+ secretAccessKey: this.secretAccessKey
})
});
}
-export function createBucket() {
- // Create a new Space
- var params = {
- Bucket: "my-new-space-with-a-unique-name"
+ createBucket(name) {
+ let params = {
+ Bucket: name,
+ ACL: "public-read"
};
- s3.createBucket(params, function(err, data) {
- if (err) console.log(err, err.stack);
- else console.log(data);
+ return new Promise((resolve, reject) => {
+ if (!this.s3) {
+ reject({ error: 'S3 not initialized!' });
+ return;
+ }
+ this.s3.createBucket(params, (error, data) => {
+ if (error) {
+ reject({ error });
+ } else {
+ resolve({ data });
+ }
+ });
});
}
-export function listBuckets() {
- // List all Spaces in the region
- s3.listBuckets({}, function(err, data) {
- if (err) console.log(err, err.stack);
- else {
- data['Buckets'].forEach(function(space) {
- console.log(space['Name']);
- })};
+ listBuckets() {
+ return new Promise((resolve, reject) => {
+ if (!this.s3) {
+ reject({ error: 'S3 not initialized!' });
+ return;
+ }
+ this.s3.listBuckets({}, (error, data) => {
+ if (error) {
+ reject({ error });
+ } else {
+ resolve({ data });
+ }
+ });
});
}
-export function putObject(body, bucket, key) {
- // Add a file to a Space
- var params = {
- Body: "The contents of the file",
- Bucket: "my-new-space-with-a-unique-name",
- Key: "file.ext",
+ putObject(body, bucket, filename) {
+ let params = {
+ Body: body,
+ Bucket: bucket,
+ Key: filename,
};
- s3.putObject(params, function(err, data) {
- if (err) console.log(err, err.stack);
- else console.log(data);
+ return new Promise((resolve, reject) => {
+ if (!this.s3) {
+ reject({ error: 'S3 not initialized!' });
+ return;
+ }
+ this.s3.putObject(params, (error, data) => {
+ if (error) {
+ reject({ error });
+ } else {
+ resolve({ data });
+ }
});
+ });
+
}
+}
+
+export let s3 = new S3();
