forked from parse-community/parse-server-s3-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (156 loc) · 5.35 KB
/
index.js
File metadata and controls
169 lines (156 loc) · 5.35 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
// S3Adapter
//
// Stores Parse files in AWS S3.
var AWS = require('aws-sdk');
const DEFAULT_S3_REGION = "us-east-1";
function requiredOrFromEnvironment(options, key, env) {
options[key] = options[key] || process.env[env];
if (!options[key]) {
throw `S3Adapter requires option '${key}' or env. variable ${env}`;
}
return options;
}
function fromEnvironmentOrDefault(options, key, env, defaultValue) {
options[key] = options[key] || process.env[env] || defaultValue;
return options;
}
function optionsFromArguments(args) {
let options = {};
let accessKeyOrOptions = args[0];
if (typeof accessKeyOrOptions == 'string') {
options.accessKey = accessKeyOrOptions;
options.secretKey = args[1];
options.bucket = args[2];
let otherOptions = args[3];
if (otherOptions) {
options.bucketPrefix = otherOptions.bucketPrefix;
options.directAccess = otherOptions.directAccess;
options.baseUrl = otherOptions.baseUrl;
options.baseUrlDirect = otherOptions.baseUrlDirect;
options.signatureVersion = otherOptions.signatureVersion;
}
} else {
options = accessKeyOrOptions || {};
}
options = requiredOrFromEnvironment(options, 'accessKey', 'S3_ACCESS_KEY');
options = requiredOrFromEnvironment(options, 'secretKey', 'S3_SECRET_KEY');
options = requiredOrFromEnvironment(options, 'bucket', 'S3_BUCKET');
options = fromEnvironmentOrDefault(options, 'bucketPrefix', 'S3_BUCKET_PREFIX', '');
options = fromEnvironmentOrDefault(options, 'region', 'S3_REGION', DEFAULT_S3_REGION);
options = fromEnvironmentOrDefault(options, 'directAccess', 'S3_DIRECT_ACCESS', false);
options = fromEnvironmentOrDefault(options, 'baseUrl', 'S3_BASE_URL', null);
options = fromEnvironmentOrDefault(options, 'baseUrlDirect', 'S3_BASE_URL_DIRECT', false);
options = fromEnvironmentOrDefault(options, 'signatureVersion', 'S3_SIGNATURE_VERSION', 'v4');
return options;
}
// Creates an S3 session.
// Providing AWS access, secret keys and bucket are mandatory
// Region will use sane defaults if omitted
function S3Adapter() {
var options = optionsFromArguments(arguments);
this._region = options.region;
this._bucket = options.bucket;
this._bucketPrefix = options.bucketPrefix;
this._directAccess = options.directAccess;
this._baseUrl = options.baseUrl;
this._baseUrlDirect = options.baseUrlDirect;
this._signatureVersion = options.signatureVersion;
let s3Options = {
accessKeyId: options.accessKey,
secretAccessKey: options.secretKey,
params: { Bucket: this._bucket },
region: this._region,
signatureVersion: this._signatureVersion
};
this._s3Client = new AWS.S3(s3Options);
this._hasBucket = false;
}
S3Adapter.prototype.createBucket = function() {
var promise;
if (this._hasBucket) {
promise = Promise.resolve();
} else {
promise = new Promise((resolve, reject) => {
this._s3Client.createBucket(() => {
this._hasBucket = true;
resolve();
});
});
}
return promise;
}
// For a given config object, filename, and data, store a file in S3
// Returns a promise containing the S3 object creation response
S3Adapter.prototype.createFile = function(filename, data, contentType) {
let params = {
Key: this._bucketPrefix + filename,
Body: data
};
if (this._directAccess) {
params.ACL = "public-read"
}
if (contentType) {
params.ContentType = contentType;
}
return this.createBucket().then(() => {
return new Promise((resolve, reject) => {
this._s3Client.upload(params, (err, data) => {
if (err !== null) {
return reject(err);
}
resolve(data);
});
});
});
}
S3Adapter.prototype.deleteFile = function(filename) {
return this.createBucket().then(() => {
return new Promise((resolve, reject) => {
let params = {
Key: this._bucketPrefix + filename
};
this._s3Client.deleteObject(params, (err, data) =>{
if(err !== null) {
return reject(err);
}
resolve(data);
});
});
});
}
// Search for and return a file if found by filename
// Returns a promise that succeeds with the buffer result from S3
S3Adapter.prototype.getFileData = function(filename) {
let params = {Key: this._bucketPrefix + filename};
return this.createBucket().then(() => {
return new Promise((resolve, reject) => {
this._s3Client.getObject(params, (err, data) => {
if (err !== null) {
return reject(err);
}
// Something happend here...
if (data && !data.Body) {
return reject(data);
}
resolve(data.Body);
});
});
});
}
// Generates and returns the location of a file stored in S3 for the given request and filename
// The location is the direct S3 link if the option is set, otherwise we serve the file through parse-server
S3Adapter.prototype.getFileLocation = function(config, filename) {
if (this._directAccess) {
if (this._baseUrl && this._baseUrlDirect) {
return `${this._baseUrl}/${filename}`;
} else if (this._baseUrl) {
return `${this._baseUrl}/${this._bucketPrefix + filename}`;
} else {
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + filename}`;
}
}
return (config.mount + '/files/' + config.applicationId + '/' + encodeURIComponent(filename));
}
module.exports = S3Adapter;
module.exports.default = S3Adapter;