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