Skip to content

Commit d5471a6

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 30b1f75 commit d5471a6

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
@@ -139,16 +139,23 @@ class S3Adapter {
139139

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

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

186200
async deleteFile(filename) {

0 commit comments

Comments
 (0)