forked from gordonball/pixly-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws.js
More file actions
151 lines (130 loc) · 3.85 KB
/
aws.js
File metadata and controls
151 lines (130 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
/**Class for accessing AWS S3 */
const AWS = require("aws-sdk");
const { v4: uuid } = require("uuid");
const fsP = require("fs/promises");
require("dotenv").config();
// create bucket
const pixlyBucket = new AWS.S3({
apiVersion: "2006-03-01",
});
pixlyBucket.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
pixlyBucket.config.region = process.env.BUCKET_REGION;
const bucketName = process.env.BUCKET_NAME;
const PIXLY_URL = `https://${bucketName}.s3.amazonaws.com`;
// make basic page with title/url submit for testing
// implement uuid to keyname?
// check against/sync database with bucket?
// put file in bucket, get etag and use as primary key
//
class PixlyAWS {
/**
* Creates a new bucket with a given name.
*/
static async createNewBucket(name) {
const result = pixlyBucket.createBucket({
Bucket: name,
});
// .promise(); definitely add to control order of events
const response = result.send();
console.log(response);
}
/**
* Read a local file from a given path
*/
static async readLocalFileFromPath(filePath) {
try {
// changes file to binary buffer
const result = await fsP.readFile("./hello.txt");
return result;
} catch (err) {
process.exit(1);
}
}
/**
* Put a new object into the basket
*
* Accepts fileData as binary string, fileName as string, fileType as string
*
* Returns the uploaded objects ETag
*/
static async putObjectInBasket(fileData, fileName) {
fileName = fileName.split(".");
fileName[0] = fileName[0] + uuid();
const keyVal = fileName.join(".");
let params = {
Body: fileData,
Bucket: bucketName,
Key: keyVal,
};
// puts object in bucket
await pixlyBucket
.putObject(params, function (err, fileData) {
console.log("PUT OBJECT!!!!");
if (err) console.log("DATA ERROR!!!!!", err);
else console.log("PUT DATA!!!!!!", fileData);
})
.promise();
return this.makeObjectLink(keyVal);
}
/**
* Gets a list of objects currently in the bucket
* Default amount is 100
*/
static async getObjectsInBucket(amount = 100) {
// List objects
let params = {
Bucket: bucketName,
MaxKeys: amount,
// Marker: start listing from ,
};
pixlyBucket.listObjects(params, function (err, data) {
console.log("LIST OBJECTS!!!!");
if (err) console.log("LIST ERROR!!!!", err, err.stack);
else console.log("LIST DATA!!!!!", data);
});
}
/**
* Gets a single item in the bucket by key value
*/
static async getSingleObjectInBucket(keyVal) {
//get params
let params = {
Bucket: bucketName,
Key: keyVal,
};
// get object from bucket
pixlyBucket.getObject(params, function (err, data) {
console.log("GET OBJECT!!!!");
if (err) console.log("GET ERROR!!!!", err, err.stack);
else console.log("GET DATA!!!!!", data); // data.Body is buffer data
});
}
/** Removes a single object from the bucket by key value. */
static async removeSingleObjectFromBucket(keyVal) {
let params = {
Bucket: bucketName,
Key: keyVal,
};
// delete object from bucket
await pixlyBucket
.deleteObject(params, function (err, data) {
console.log("!!!DELETE OBJECT");
if (err) console.log("DELETE ERROR!!!!!!", err, err.stack);
else console.log(`!!!Deleted ${keyVal} from bucket.`);
})
.promise();
}
static makeObjectLink(keyVal) {
return `${PIXLY_URL}/${keyVal}`;
}
}
module.exports = PixlyAWS;
// params = { Bucket: bucketName, Key: "auth-test-hello.txt" };
// pixlyBucket.getSignedUrl("getObject", params, function (err, data) {
// if (err) console.log("GET URL ERROR!!!!!!", err);
// else console.log("GET URL DATA!!!!!", data);
// });