fix(medusa-file-s3, medusa): Add S3 export support (#2380)
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import aws from "aws-sdk"
|
import aws from "aws-sdk"
|
||||||
import { AbstractFileService } from '@medusajs/medusa'
|
import { AbstractFileService } from "@medusajs/medusa"
|
||||||
|
import stream from "stream"
|
||||||
|
|
||||||
class S3Service extends AbstractFileService {
|
class S3Service extends AbstractFileService {
|
||||||
|
// eslint-disable-next-line no-empty-pattern
|
||||||
constructor({}, options) {
|
constructor({}, options) {
|
||||||
super({}, options)
|
super({}, options)
|
||||||
|
|
||||||
@@ -15,16 +17,10 @@ class S3Service extends AbstractFileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
upload(file) {
|
upload(file) {
|
||||||
aws.config.setPromisesDependency(null)
|
this.updateAwsConfig()
|
||||||
aws.config.update({
|
|
||||||
accessKeyId: this.accessKeyId_,
|
|
||||||
secretAccessKey: this.secretAccessKey_,
|
|
||||||
region: this.region_,
|
|
||||||
endpoint: this.endpoint_,
|
|
||||||
}, true)
|
|
||||||
|
|
||||||
const s3 = new aws.S3()
|
const s3 = new aws.S3()
|
||||||
var params = {
|
const params = {
|
||||||
ACL: "public-read",
|
ACL: "public-read",
|
||||||
Bucket: this.bucket_,
|
Bucket: this.bucket_,
|
||||||
Body: fs.createReadStream(file.path),
|
Body: fs.createReadStream(file.path),
|
||||||
@@ -38,22 +34,16 @@ class S3Service extends AbstractFileService {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve({ url: data.Location })
|
resolve({ url: data.Location, key: data.Key })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(file) {
|
async delete(file) {
|
||||||
aws.config.setPromisesDependency(null)
|
this.updateAwsConfig()
|
||||||
aws.config.update({
|
|
||||||
accessKeyId: this.accessKeyId_,
|
|
||||||
secretAccessKey: this.secretAccessKey_,
|
|
||||||
region: this.region_,
|
|
||||||
endpoint: this.endpoint_,
|
|
||||||
}, true)
|
|
||||||
|
|
||||||
const s3 = new aws.S3()
|
const s3 = new aws.S3()
|
||||||
var params = {
|
const params = {
|
||||||
Bucket: this.bucket_,
|
Bucket: this.bucket_,
|
||||||
Key: `${file}`,
|
Key: `${file}`,
|
||||||
}
|
}
|
||||||
@@ -68,17 +58,70 @@ class S3Service extends AbstractFileService {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUploadStreamDescriptor(fileData) {
|
async getUploadStreamDescriptor(fileData) {
|
||||||
throw new Error("Method not implemented.")
|
this.updateAwsConfig()
|
||||||
|
|
||||||
|
const pass = new stream.PassThrough()
|
||||||
|
|
||||||
|
const fileKey = `${fileData.name}.${fileData.ext}`
|
||||||
|
const params = {
|
||||||
|
ACL: fileData.acl ?? "private",
|
||||||
|
Bucket: this.bucket_,
|
||||||
|
Body: pass,
|
||||||
|
Key: fileKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
const s3 = new aws.S3()
|
||||||
|
return {
|
||||||
|
writeStream: pass,
|
||||||
|
promise: s3.upload(params).promise(),
|
||||||
|
url: `${this.s3Url_}/${fileKey}`,
|
||||||
|
fileKey,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDownloadStream(fileData) {
|
async getDownloadStream(fileData) {
|
||||||
throw new Error("Method not implemented.")
|
this.updateAwsConfig()
|
||||||
|
|
||||||
|
const s3 = new aws.S3()
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
Bucket: this.bucket_,
|
||||||
|
Key: `${fileData.fileKey}`,
|
||||||
|
}
|
||||||
|
|
||||||
|
return s3.getObject(params).createReadStream()
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPresignedDownloadUrl(fileData) {
|
async getPresignedDownloadUrl(fileData) {
|
||||||
throw new Error("Method not implemented.")
|
this.updateAwsConfig({
|
||||||
|
signatureVersion: "v4",
|
||||||
|
})
|
||||||
|
|
||||||
|
const s3 = new aws.S3()
|
||||||
|
|
||||||
|
const params = {
|
||||||
|
Bucket: this.bucket_,
|
||||||
|
Key: `${fileData.fileKey}`,
|
||||||
|
Expires: this.downloadUrlDuration,
|
||||||
|
}
|
||||||
|
|
||||||
|
return await s3.getSignedUrlPromise("getObject", params)
|
||||||
|
}
|
||||||
|
|
||||||
|
updateAwsConfig(additionalConfiguration = {}) {
|
||||||
|
aws.config.setPromisesDependency(null)
|
||||||
|
aws.config.update(
|
||||||
|
{
|
||||||
|
accessKeyId: this.accessKeyId_,
|
||||||
|
secretAccessKey: this.secretAccessKey_,
|
||||||
|
region: this.region_,
|
||||||
|
endpoint: this.endpoint_,
|
||||||
|
...additionalConfiguration,
|
||||||
|
},
|
||||||
|
true
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -319,7 +319,10 @@ class OrderExportStrategy extends AbstractBatchJobStrategy {
|
|||||||
fieldName: "sales_channel",
|
fieldName: "sales_channel",
|
||||||
title: ["Sales channel name", "Sales channel description"].join(";"),
|
title: ["Sales channel name", "Sales channel description"].join(";"),
|
||||||
accessor: (order: Order): string =>
|
accessor: (order: Order): string =>
|
||||||
[order.sales_channel.name, order.sales_channel.description].join(";"),
|
[
|
||||||
|
order.sales_channel?.name || "",
|
||||||
|
order.sales_channel?.description || "",
|
||||||
|
].join(";"),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user