-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: Revise createFile logic to return modified filenames and location #242
base: master
Are you sure you want to change the base?
Changes from all commits
d5471a6
a9edee2
2592fa8
5abd18a
537a5ce
61ae6b0
87573b9
bcb71b6
429c9f1
816c780
8c15366
de090aa
bbfcbf5
b9df8b6
d70e749
4c987ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,16 +139,23 @@ class S3Adapter { | |
|
||
// For a given config object, filename, and data, store a file in S3 | ||
// Returns a promise containing the S3 object creation response | ||
async createFile(filename, data, contentType, options = {}) { | ||
async createFile(filename, data, contentType, options = {}, config = {}) { | ||
|
||
let key_without_prefix = filename; | ||
if (this._generateKey instanceof Function) { | ||
try { | ||
key_without_prefix = this._generateKey(filename, contentType, options); | ||
}catch(e){ | ||
throw new Error(e); // throw error if generateKey function fails | ||
} | ||
} | ||
|
||
const params = { | ||
Bucket: this._bucket, | ||
Key: this._bucketPrefix + filename, | ||
Key: this._bucketPrefix + key_without_prefix, | ||
Body: data, | ||
}; | ||
|
||
if (this._generateKey instanceof Function) { | ||
params.Key = this._bucketPrefix + this._generateKey(filename); | ||
} | ||
if (this._fileAcl) { | ||
if (this._fileAcl === 'none') { | ||
delete params.ACL; | ||
|
@@ -180,7 +187,17 @@ class S3Adapter { | |
const endpoint = this._endpoint || `https://${this._bucket}.s3.${this._region}.amazonaws.com`; | ||
const location = `${endpoint}/${params.Key}`; | ||
|
||
return Object.assign(response || {}, { Location: location }); | ||
let url; | ||
if (Object.keys(config).length != 0) { // if config is passed, we can generate a presigned url here | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of checking only for the presence of keys, let's ensure we are checking for the correct keys. Maybe change to: |
||
url = await this.getFileLocation(config, key_without_prefix); | ||
} | ||
|
||
return { | ||
location: location, // actual upload location, used for tests | ||
name: key_without_prefix, // filename in storage, consistent with other adapters | ||
s3_response: response, // raw s3 response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need the s3_response key and we can remove it. |
||
...url ? { url: url } : {} // url (optionally presigned) or non-direct access url | ||
}; | ||
} | ||
|
||
async deleteFile(filename) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe removing the try block would achieve the same result while preserving the original error trace and improving readability.