74 lines
1.6 KiB
JavaScript
74 lines
1.6 KiB
JavaScript
import fs from "fs"
|
|
import aws from "aws-sdk"
|
|
import { FileService } from "medusa-interfaces"
|
|
|
|
class DigitalOceanService extends FileService {
|
|
constructor({}, options) {
|
|
super()
|
|
|
|
this.bucket_ = options.bucket
|
|
this.spacesUrl_ = options.spaces_url
|
|
this.accessKeyId_ = options.access_key_id
|
|
this.secretAccessKey_ = options.secret_access_key
|
|
this.region_ = options.region
|
|
this.endpoint_ = options.endpoint
|
|
}
|
|
|
|
upload(file) {
|
|
aws.config.setPromisesDependency()
|
|
aws.config.update({
|
|
accessKeyId: this.accessKeyId_,
|
|
secretAccessKey: this.secretAccessKey_,
|
|
region: this.region_,
|
|
endpoint: this.endpoint_,
|
|
})
|
|
|
|
const s3 = new aws.S3()
|
|
var params = {
|
|
ACL: "public-read",
|
|
Bucket: this.bucket_,
|
|
Body: fs.createReadStream(file.path),
|
|
Key: `${file.originalname}`,
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
s3.upload(params, (err, data) => {
|
|
if (err) {
|
|
reject(err)
|
|
return
|
|
}
|
|
|
|
resolve({ url: data.Location })
|
|
})
|
|
})
|
|
}
|
|
|
|
delete(file) {
|
|
aws.config.setPromisesDependency()
|
|
aws.config.update({
|
|
accessKeyId: this.accessKeyId_,
|
|
secretAccessKey: this.secretAccessKey_,
|
|
region: this.region_,
|
|
endpoint: this.endpoint_,
|
|
})
|
|
|
|
const s3 = new aws.S3()
|
|
var params = {
|
|
Bucket: this.bucket_,
|
|
Key: `${file}`,
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
s3.deleteObject(params, (err, data) => {
|
|
if (err) {
|
|
reject(err)
|
|
return
|
|
}
|
|
resolve(data)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
export default DigitalOceanService
|