feat(medusa-file-s3): S3 file service reusing a single AWS client (#3260)

This commit is contained in:
Pevey authored and GitHub committed 2023-02-19 19:17:35 +01:00
1 parent 7d585f5f84
commit 13c200ad2f
2 files changed
+12 -12

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"medusa-file-s3": patch
---
fix(medusa-file-s3): update s3 file service to reuse one s3 client
+7 -12
View File
@@ -15,6 +15,8 @@ class S3Service extends AbstractFileService {
this.region_ = options.region this.region_ = options.region
this.endpoint_ = options.endpoint this.endpoint_ = options.endpoint
this.awsConfigObject_ = options.aws_config_object this.awsConfigObject_ = options.aws_config_object
this.client_ = new aws.S3()
} }
upload(file) { upload(file) {
@@ -30,7 +32,6 @@ class S3Service extends AbstractFileService {
} }
uploadFile(file, options = { isProtected: false, acl: undefined }) { uploadFile(file, options = { isProtected: false, acl: undefined }) {
const s3 = new aws.S3()
const params = { const params = {
ACL: options.acl ?? (options.isProtected ? "private" : "public-read"), ACL: options.acl ?? (options.isProtected ? "private" : "public-read"),
Bucket: this.bucket_, Bucket: this.bucket_,
@@ -39,7 +40,7 @@ class S3Service extends AbstractFileService {
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
s3.upload(params, (err, data) => { this.client_.upload(params, (err, data) => {
if (err) { if (err) {
reject(err) reject(err)
return return
@@ -53,14 +54,13 @@ class S3Service extends AbstractFileService {
async delete(file) { async delete(file) {
this.updateAwsConfig() this.updateAwsConfig()
const s3 = new aws.S3()
const params = { const params = {
Bucket: this.bucket_, Bucket: this.bucket_,
Key: `${file}`, Key: `${file}`,
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
s3.deleteObject(params, (err, data) => { this.client_.deleteObject(params, (err, data) => {
if (err) { if (err) {
reject(err) reject(err)
return return
@@ -83,10 +83,9 @@ class S3Service extends AbstractFileService {
Key: fileKey, Key: fileKey,
} }
const s3 = new aws.S3()
return { return {
writeStream: pass, writeStream: pass,
promise: s3.upload(params).promise(), promise: this.client_.upload(params).promise(),
url: `${this.s3Url_}/${fileKey}`, url: `${this.s3Url_}/${fileKey}`,
fileKey, fileKey,
} }
@@ -95,14 +94,12 @@ class S3Service extends AbstractFileService {
async getDownloadStream(fileData) { async getDownloadStream(fileData) {
this.updateAwsConfig() this.updateAwsConfig()
const s3 = new aws.S3()
const params = { const params = {
Bucket: this.bucket_, Bucket: this.bucket_,
Key: `${fileData.fileKey}`, Key: `${fileData.fileKey}`,
} }
return s3.getObject(params).createReadStream() return this.client_.getObject(params).createReadStream()
} }
async getPresignedDownloadUrl(fileData) { async getPresignedDownloadUrl(fileData) {
@@ -110,15 +107,13 @@ class S3Service extends AbstractFileService {
signatureVersion: "v4", signatureVersion: "v4",
}) })
const s3 = new aws.S3()
const params = { const params = {
Bucket: this.bucket_, Bucket: this.bucket_,
Key: `${fileData.fileKey}`, Key: `${fileData.fileKey}`,
Expires: this.downloadUrlDuration, Expires: this.downloadUrlDuration,
} }
return await s3.getSignedUrlPromise("getObject", params) return await this.client_.getSignedUrlPromise("getObject", params)
} }
updateAwsConfig(additionalConfiguration = {}) { updateAwsConfig(additionalConfiguration = {}) {