feat: AWS S3 file service plugin (#376)
This commit is contained in:
1 parent
bf43896d19
commit
75b608330b
8 files changed
+174
No files matched your search
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"plugins": [
|
||||||
|
"@babel/plugin-proposal-class-properties",
|
||||||
|
"@babel/plugin-transform-instanceof",
|
||||||
|
"@babel/plugin-transform-classes"
|
||||||
|
],
|
||||||
|
"presets": ["@babel/preset-env"],
|
||||||
|
"env": {
|
||||||
|
"test": {
|
||||||
|
"plugins": ["@babel/plugin-transform-runtime"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"plugins": ["prettier"],
|
||||||
|
"extends": ["prettier"],
|
||||||
|
"rules": {
|
||||||
|
"prettier/prettier": "error",
|
||||||
|
"semi": "error",
|
||||||
|
"no-unused-expressions": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/lib
|
||||||
|
node_modules
|
||||||
|
.DS_store
|
||||||
|
.env*
|
||||||
|
/*.js
|
||||||
|
!index.js
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
|
/dist
|
||||||
|
|
||||||
|
/api
|
||||||
|
/services
|
||||||
|
/models
|
||||||
|
/subscribers
|
||||||
|
/__mocks__
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/lib
|
||||||
|
node_modules
|
||||||
|
.DS_store
|
||||||
|
.env*
|
||||||
|
/*.js
|
||||||
|
!index.js
|
||||||
|
yarn.lock
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"endOfLine": "lf",
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": false,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"trailingComma": "es5"
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// noop
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"name": "medusa-file-s3",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "AWS s3 file connector for Medusa",
|
||||||
|
"main": "index.js",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/medusajs/medusa",
|
||||||
|
"directory": "packages/medusa-file-s3"
|
||||||
|
},
|
||||||
|
"author": "Sebastian Mateos Nicolajsen",
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/cli": "^7.7.5",
|
||||||
|
"@babel/core": "^7.7.5",
|
||||||
|
"@babel/node": "^7.7.4",
|
||||||
|
"@babel/plugin-proposal-class-properties": "^7.7.4",
|
||||||
|
"@babel/plugin-transform-instanceof": "^7.8.3",
|
||||||
|
"@babel/plugin-transform-runtime": "^7.7.6",
|
||||||
|
"@babel/preset-env": "^7.7.5",
|
||||||
|
"@babel/register": "^7.7.4",
|
||||||
|
"@babel/runtime": "^7.9.6",
|
||||||
|
"client-sessions": "^0.8.0",
|
||||||
|
"cross-env": "^5.2.1",
|
||||||
|
"eslint": "^6.8.0",
|
||||||
|
"jest": "^25.5.2",
|
||||||
|
"medusa-test-utils": "^0.3.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "babel src -d .",
|
||||||
|
"prepare": "cross-env NODE_ENV=production npm run build",
|
||||||
|
"watch": "babel -w src --out-dir . --ignore **/__tests__",
|
||||||
|
"test": "jest"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"medusa-interfaces": "1.x"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/plugin-transform-classes": "^7.15.4",
|
||||||
|
"aws-sdk": "^2.983.0",
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
|
"express": "^4.17.1",
|
||||||
|
"medusa-core-utils": "^1.1.20",
|
||||||
|
"medusa-test-utils": "^1.1.23"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import fs from "fs"
|
||||||
|
import aws from "aws-sdk"
|
||||||
|
import { FileService } from "medusa-interfaces"
|
||||||
|
|
||||||
|
class S3Service extends FileService {
|
||||||
|
constructor({}, options) {
|
||||||
|
super()
|
||||||
|
|
||||||
|
this.bucket_ = options.bucket
|
||||||
|
this.s3Url_ = options.s3_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 S3Service
|
||||||
Reference in new issue
Block a user