Skip to content

Commit 3172564

Browse files
committed
Revise createFile logic to preserve key & location
- Add error handling for key generation - Standardize return object with location, url, and filename - Add support for optional config parameter - Return s3 response explicitly as a separate variable
1 parent e634835 commit 3172564

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,23 @@ class S3Adapter {
140140

141141
// For a given config object, filename, and data, store a file in S3
142142
// Returns a promise containing the S3 object creation response
143-
async createFile(filename, data, contentType, options = {}) {
143+
async createFile(filename, data, contentType, options = {}, config= {}) {
144+
145+
let key_without_prefix = filename;
146+
if (this._generateKey instanceof Function) {
147+
try {
148+
key_without_prefix = this._generateKey(filename);
149+
}catch(e){
150+
throw new Error(e); // throw error if generateKey function fails
151+
}
152+
}
153+
144154
const params = {
145155
Bucket: this._bucket,
146-
Key: this._bucketPrefix + filename,
156+
Key: this._bucketPrefix + key_without_prefix,
147157
Body: data,
148158
};
149-
150-
if (this._generateKey instanceof Function) {
151-
params.Key = this._bucketPrefix + this._generateKey(filename);
152-
}
159+
153160
if (this._fileAcl) {
154161
if (this._fileAcl === 'none') {
155162
delete params.ACL;
@@ -181,7 +188,14 @@ class S3Adapter {
181188
const endpoint = this._endpoint || `https://${this._bucket}.s3.${this._region}.amazonaws.com`;
182189
const location = `${endpoint}/${params.Key}`;
183190

184-
return Object.assign(response || {}, { Location: location });
191+
const url = await this.getFileLocation(config, key_without_prefix);
192+
193+
return {
194+
location: location, // actual upload location, used for tests
195+
url: url, // optionally signed url (can be returned to client)
196+
filename: key_without_prefix, // filename in storage
197+
s3_response: response // raw s3 response
198+
};
185199
}
186200

187201
async deleteFile(filename) {

0 commit comments

Comments
 (0)