Skip to content

Commit

Permalink
Uses AWS S3 presigned url together with baseUrl
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel San <[email protected]>
  • Loading branch information
danielsanfr committed Nov 19, 2020
1 parent 6495047 commit 3fb8650
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,41 @@ class S3Adapter {
getFileLocation(config, filename) {
const fileName = filename.split('/').map(encodeURIComponent).join('/');
if (this._directAccess) {
let presignedUrl = '';
if (this._presignedUrl) {
const params = { Bucket: this._bucket, Key: fileName, Expires: this._presignedUrlExpires };
return this._s3Client.getSignedUrl('getObject', params);
const params = {
Bucket: this._bucket,
Key: this._bucketPrefix + fileName,
Expires: this._presignedUrlExpires,
};
presignedUrl = this._s3Client.getSignedUrl('getObject', params);
}
if (this._baseUrl) {
let directAccessUrl;

if (typeof this._baseUrl === 'function') {
if (this._baseUrlDirect) {
return `${this._baseUrl(config, filename)}/${fileName}`;
directAccessUrl = `${this._baseUrl(config, filename)}/${fileName}`;
} else {
directAccessUrl = `${this._baseUrl(config, filename)}/${this._bucketPrefix + fileName}`;
}
return `${this._baseUrl(config, filename)}/${this._bucketPrefix + fileName}`;
} else if (this._baseUrlDirect) {
directAccessUrl = `${this._baseUrl}/${fileName}`;
} else {
directAccessUrl = `${this._baseUrl}/${this._bucketPrefix + fileName}`;
}
if (this._baseUrlDirect) {
return `${this._baseUrl}/${fileName}`;

if (this._presignedUrl) {
directAccessUrl += presignedUrl.substring(presignedUrl.indexOf('?') + 1);
}
return `${this._baseUrl}/${this._bucketPrefix + fileName}`;
return directAccessUrl;
}
if (this._presignedUrl) {
return presignedUrl;
}
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + fileName}`;
}
return (`${config.mount}/files/${config.applicationId}/${fileName}`);
return `${config.mount}/files/${config.applicationId}/${fileName}`;
}

handleFileStream(filename, req, res) {
Expand Down

0 comments on commit 3fb8650

Please sign in to comment.