Skip to content

Commit 3fb8650

Browse files
committed
Uses AWS S3 presigned url together with baseUrl
Signed-off-by: Daniel San <[email protected]>
1 parent 6495047 commit 3fb8650

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

index.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,41 @@ class S3Adapter {
161161
getFileLocation(config, filename) {
162162
const fileName = filename.split('/').map(encodeURIComponent).join('/');
163163
if (this._directAccess) {
164+
let presignedUrl = '';
164165
if (this._presignedUrl) {
165-
const params = { Bucket: this._bucket, Key: fileName, Expires: this._presignedUrlExpires };
166-
return this._s3Client.getSignedUrl('getObject', params);
166+
const params = {
167+
Bucket: this._bucket,
168+
Key: this._bucketPrefix + fileName,
169+
Expires: this._presignedUrlExpires,
170+
};
171+
presignedUrl = this._s3Client.getSignedUrl('getObject', params);
167172
}
168173
if (this._baseUrl) {
174+
let directAccessUrl;
175+
169176
if (typeof this._baseUrl === 'function') {
170177
if (this._baseUrlDirect) {
171-
return `${this._baseUrl(config, filename)}/${fileName}`;
178+
directAccessUrl = `${this._baseUrl(config, filename)}/${fileName}`;
179+
} else {
180+
directAccessUrl = `${this._baseUrl(config, filename)}/${this._bucketPrefix + fileName}`;
172181
}
173-
return `${this._baseUrl(config, filename)}/${this._bucketPrefix + fileName}`;
182+
} else if (this._baseUrlDirect) {
183+
directAccessUrl = `${this._baseUrl}/${fileName}`;
184+
} else {
185+
directAccessUrl = `${this._baseUrl}/${this._bucketPrefix + fileName}`;
174186
}
175-
if (this._baseUrlDirect) {
176-
return `${this._baseUrl}/${fileName}`;
187+
188+
if (this._presignedUrl) {
189+
directAccessUrl += presignedUrl.substring(presignedUrl.indexOf('?') + 1);
177190
}
178-
return `${this._baseUrl}/${this._bucketPrefix + fileName}`;
191+
return directAccessUrl;
192+
}
193+
if (this._presignedUrl) {
194+
return presignedUrl;
179195
}
180196
return `https://${this._bucket}.s3.amazonaws.com/${this._bucketPrefix + fileName}`;
181197
}
182-
return (`${config.mount}/files/${config.applicationId}/${fileName}`);
198+
return `${config.mount}/files/${config.applicationId}/${fileName}`;
183199
}
184200

185201
handleFileStream(filename, req, res) {

0 commit comments

Comments
 (0)