chore: Remove legacy files plugins (#7216)

**What**
rm legacy file plugins
This commit is contained in:
Adrien de Peretti
2024-05-03 10:01:40 +02:00
committed by GitHub
parent 5bc780a646
commit 46ded00c08
34 changed files with 11 additions and 3413 deletions

View File

@@ -1,16 +0,0 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock
/dist
/api
/services
/models
/subscribers
/__mocks__

View File

@@ -1,22 +0,0 @@
# @medusajs/file-local
## 1.0.3
### Patch Changes
- [#6100](https://github.com/medusajs/medusa/pull/6100) [`4792c5522`](https://github.com/medusajs/medusa/commit/4792c552269c147d3c07da49a175e9038f9260a8) Thanks [@shahednasser](https://github.com/shahednasser)! - chore(@medusajs/medusa): add missing constructor to some services
fix(@medusajs/file-local): Fix argument passed to the constructor
fix(medusa-file-minio): Fix argument passed to the constructor
fix(medusa-file-s3): Fix argument passed to the constructor
## 1.0.2
### Patch Changes
- [#4788](https://github.com/medusajs/medusa/pull/4788) [`d8a6e3e0d`](https://github.com/medusajs/medusa/commit/d8a6e3e0d8a86aba7209f4a767cd08ebe3e49c26) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa-file-local): local file service streaming methods
## 1.0.1
### Patch Changes
- [#4118](https://github.com/medusajs/medusa/pull/4118) [`c4aae6b97`](https://github.com/medusajs/medusa/commit/c4aae6b976d1983e89a3a133d0a73f073fa65cdb) Thanks [@olivermrbl](https://github.com/olivermrbl)! - feat(file-local): Add plugin for local file storage

View File

@@ -1,55 +0,0 @@
# Local file storage
Store uploaded files to your Medusa backend locally.
> Not suited for production environments
[Plugin Documentation](https://docs.medusajs.com/plugins/file-service/local) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa)
## Features
- Store product images locally
---
## Prerequisites
- [Medusa backend](https://docs.medusajs.com/development/backend/install)
---
## How to Install
1\. Run the following command in the directory of the Medusa backend:
```bash
npm install @medusajs/file-local
```
2 \. In `medusa-config.js` add the following at the end of the `plugins` array:
```js
const plugins = [
// ...
{
resolve: `@medusajs/file-local`,
options: {
upload_dir: 'uploads/images', // optional
backend_url: 'http://localhost:9000' // optional
}
},
]
```
---
## Test the Plugin
1\. Run the following command in the directory of the Medusa backend to run the backend:
```bash
npm run start
```
2\. Upload an image for a product using the admin dashboard or using [the Admin APIs](https://docs.medusajs.com/api/admin#tag/Upload).

View File

@@ -1,42 +0,0 @@
{
"name": "@medusajs/file-local",
"version": "1.0.3",
"description": "Local file plugin for Medusa",
"main": "dist/index.js",
"files": [
"dist"
],
"engines": {
"node": ">=16"
},
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-file-local"
},
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"@medusajs/medusa": "^1.20.1",
"@medusajs/types": "^1.11.11",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"rimraf": "^5.0.1",
"typescript": "^4.9.5"
},
"scripts": {
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"test": "jest --passWithNoTests src",
"build": "rimraf dist && tsc",
"watch": "tsc --watch"
},
"peerDependencies": {
"medusa-interfaces": "^1.3.7"
},
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808",
"keywords": [
"medusa-plugin",
"medusa-plugin-file",
"medusa-plugin-storage"
]
}

View File

@@ -1,11 +0,0 @@
import express from "express"
export default (rootDirectory, pluginOptions) => {
const app = express.Router()
const uploadDir = pluginOptions.upload_dir ?? "uploads/images"
app.use(`/${uploadDir}`, express.static(uploadDir))
return app
}

View File

@@ -1,122 +0,0 @@
import { AbstractFileService, IFileService } from "@medusajs/medusa"
import {
FileServiceGetUploadStreamResult,
FileServiceUploadResult,
UploadStreamDescriptorType,
} from "@medusajs/types"
import fs from "fs"
import path from "path"
import stream from "stream"
class LocalService extends AbstractFileService implements IFileService {
protected uploadDir_: string
protected backendUrl_: string
constructor({}, options) {
super(arguments[0], options)
this.uploadDir_ = options.upload_dir || "uploads"
this.backendUrl_ = options.backend_url || "http://localhost:9000"
}
async upload(file: Express.Multer.File): Promise<FileServiceUploadResult> {
return await this.uploadFile(file)
}
async uploadProtected(file: Express.Multer.File) {
return await this.uploadFile(file, {})
}
async uploadFile(
file: Express.Multer.File,
options = {}
): Promise<FileServiceUploadResult> {
const parsedFilename = path.parse(file.originalname)
if (parsedFilename.dir) {
this.ensureDirExists(parsedFilename.dir)
}
const fileKey = path.join(
parsedFilename.dir,
`${Date.now()}-${parsedFilename.base}`
)
return new Promise((resolve, reject) => {
fs.copyFile(file.path, `${this.uploadDir_}/${fileKey}`, (err) => {
if (err) {
reject(err)
throw err
}
const fileUrl = `${this.backendUrl_}/${this.uploadDir_}/${fileKey}`
resolve({ url: fileUrl, key: fileKey })
})
})
}
async delete(file): Promise<void> {
const filePath = `${this.uploadDir_}/${file.fileKey}`
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath)
}
}
async getUploadStreamDescriptor(
fileData: UploadStreamDescriptorType
): Promise<FileServiceGetUploadStreamResult> {
const parsedFilename = path.parse(
fileData.name + (fileData.ext ? `.${fileData.ext}` : "")
)
if (parsedFilename.dir) {
this.ensureDirExists(parsedFilename.dir)
}
const fileKey = path.join(
parsedFilename.dir,
`${Date.now()}-${parsedFilename.base}`
)
const fileUrl = `${this.backendUrl_}/${this.uploadDir_}/${fileKey}`
const pass = new stream.PassThrough()
const writeStream = fs.createWriteStream(`${this.uploadDir_}/${fileKey}`)
pass.pipe(writeStream) // for consistency with the IFileService
const promise = new Promise((res, rej) => {
writeStream.on("finish", res)
writeStream.on("error", rej)
})
return { url: fileUrl, fileKey, writeStream: pass, promise }
}
async getDownloadStream(fileData): Promise<NodeJS.ReadableStream> {
const filePath = `${this.uploadDir_}/${fileData.fileKey}`
return fs.createReadStream(filePath)
}
async getPresignedDownloadUrl(fileData): Promise<string> {
return `${this.backendUrl_}/${this.uploadDir_}/${fileData.fileKey}`
}
/**
* Ensure `uploadDir_` has nested directories provided as file path
*
* @param dirPath - file path relative to the base directory
* @private
*/
private ensureDirExists(dirPath: string) {
const relativePath = path.join(this.uploadDir_, dirPath)
if (!fs.existsSync(relativePath)) {
fs.mkdirSync(relativePath, { recursive: true })
}
}
}
export default LocalService

View File

@@ -1,30 +0,0 @@
{
"compilerOptions": {
"lib": ["es5", "es6", "es2019"],
"target": "es5",
"outDir": "./dist",
"rootDir": "src",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true // to use ES5 specific tooling
},
"include": ["src"],
"exclude": [
"dist",
"src/**/__tests__",
"src/**/__mocks__",
"src/**/__fixtures__",
"node_modules"
]
}

View File

@@ -1,13 +0,0 @@
{
"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"]
}
}
}

View File

@@ -1,16 +0,0 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock
/dist
/api
/services
/models
/subscribers
/__mocks__

View File

@@ -1,8 +0,0 @@
.DS_store
src
dist
yarn.lock
.babelrc
.turbo
.yarn

View File

@@ -1,176 +0,0 @@
# Change Log
## 1.3.1
### Patch Changes
- [#6100](https://github.com/medusajs/medusa/pull/6100) [`4792c5522`](https://github.com/medusajs/medusa/commit/4792c552269c147d3c07da49a175e9038f9260a8) Thanks [@shahednasser](https://github.com/shahednasser)! - chore(@medusajs/medusa): add missing constructor to some services
fix(@medusajs/file-local): Fix argument passed to the constructor
fix(medusa-file-minio): Fix argument passed to the constructor
fix(medusa-file-s3): Fix argument passed to the constructor
## 1.3.0
### Minor Changes
- [`e91bd9e1c`](https://github.com/medusajs/medusa/commit/e91bd9e1c1746ff2fe915d169077bf9bf2710dcf) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Minor-bumping file plugins
## 1.2.3
### Patch Changes
- [#4788](https://github.com/medusajs/medusa/pull/4788) [`d8a6e3e0d`](https://github.com/medusajs/medusa/commit/d8a6e3e0d8a86aba7209f4a767cd08ebe3e49c26) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa-file-local): local file service streaming methods
- [#4771](https://github.com/medusajs/medusa/pull/4771) [`edf9ed4e5`](https://github.com/medusajs/medusa/commit/edf9ed4e593063622aa39cdbebef4810bf2a5fb1) Thanks [@fPolic](https://github.com/fPolic)! - fix(medusa-interfaces, medusa-file-\*): add `ìsPrivate` flag to the streaming methods, fix minio default bucket
## 1.2.2
### Patch Changes
- [#4540](https://github.com/medusajs/medusa/pull/4540) [`950a58169`](https://github.com/medusajs/medusa/commit/950a5816909b2038a5bee5d8e6912290e5c7c53b) Thanks [@pKorsholm](https://github.com/pKorsholm)! - fix(medusa-file-minio): default getUploadDescriptor to private
## 1.2.1
### Patch Changes
- [#4276](https://github.com/medusajs/medusa/pull/4276) [`afd1b67f1`](https://github.com/medusajs/medusa/commit/afd1b67f1c7de8cf07fd9fcbdde599a37914e9b5) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Use caret range
## 1.2.0
### Minor Changes
- [#3740](https://github.com/medusajs/medusa/pull/3740) [`8b6464180`](https://github.com/medusajs/medusa/commit/8b6464180a82bcc41197f2a97e58b9555a7072cd) Thanks [@dwene](https://github.com/dwene)! - Migrate medusa-file-minio and medusa-file-s3 to typescript.
## 1.1.6
### Patch Changes
- [#3041](https://github.com/medusajs/medusa/pull/3041) [`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724) Thanks [@riqwan](https://github.com/riqwan)! - chore(medusa): Typeorm fixes / enhancements
- upgrade typeorm from 0.2.51 to 0.3.11
- Plugin repository loader to work with Typeorm update
- Updated dependencies [[`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`aa690beed`](https://github.com/medusajs/medusa/commit/aa690beed775646cbc86b445fb5dc90dcac087d5), [`54dcc1871`](https://github.com/medusajs/medusa/commit/54dcc1871c8f28bea962dbb9df6e79b038d56449), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73)]:
- medusa-core-utils@1.2.0
- medusa-interfaces@1.3.7
- medusa-test-utils@1.1.40
## 1.1.6-rc.0
### Patch Changes
- [#3041](https://github.com/medusajs/medusa/pull/3041) [`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724) Thanks [@riqwan](https://github.com/riqwan)! - chore(medusa): Typeorm fixes / enhancements
- upgrade typeorm from 0.2.51 to 0.3.11
- Plugin repository loader to work with Typeorm update
- Updated dependencies [[`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`aa690beed`](https://github.com/medusajs/medusa/commit/aa690beed775646cbc86b445fb5dc90dcac087d5), [`54dcc1871`](https://github.com/medusajs/medusa/commit/54dcc1871c8f28bea962dbb9df6e79b038d56449), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73)]:
- medusa-core-utils@1.2.0-rc.0
- medusa-interfaces@1.3.7-rc.0
- medusa-test-utils@1.1.40-rc.0
## 1.1.5
### Patch Changes
- [#3217](https://github.com/medusajs/medusa/pull/3217) [`8c5219a31`](https://github.com/medusajs/medusa/commit/8c5219a31ef76ee571fbce84d7d57a63abe56eb0) Thanks [@adrien2p](https://github.com/adrien2p)! - chore: Fix npm packages files included
- Updated dependencies [[`8c5219a31`](https://github.com/medusajs/medusa/commit/8c5219a31ef76ee571fbce84d7d57a63abe56eb0)]:
- medusa-core-utils@1.1.39
- medusa-interfaces@1.3.6
## 1.1.4
### Patch Changes
- [#3185](https://github.com/medusajs/medusa/pull/3185) [`08324355a`](https://github.com/medusajs/medusa/commit/08324355a4466b017a0bc7ab1d333ee3cd27b8c4) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Patches all dependencies + minor bumps `winston` to include a [fix for a significant memory leak](https://github.com/winstonjs/winston/pull/2057)
- Updated dependencies [[`08324355a`](https://github.com/medusajs/medusa/commit/08324355a4466b017a0bc7ab1d333ee3cd27b8c4)]:
- medusa-core-utils@1.1.38
- medusa-interfaces@1.3.5
## 1.1.3
### Patch Changes
- [#3025](https://github.com/medusajs/medusa/pull/3025) [`93d0dc1bd`](https://github.com/medusajs/medusa/commit/93d0dc1bdcb54cf6e87428a7bb9b0dac196b4de2) Thanks [@adrien2p](https://github.com/adrien2p)! - fix(medusa): test, build and watch scripts
- Updated dependencies [[`93d0dc1bd`](https://github.com/medusajs/medusa/commit/93d0dc1bdcb54cf6e87428a7bb9b0dac196b4de2)]:
- medusa-interfaces@1.3.4
## 1.1.2
### Patch Changes
- [#2808](https://github.com/medusajs/medusa/pull/2808) [`0a9c89185`](https://github.com/medusajs/medusa/commit/0a9c891853c4d16b553d38268a3408ca1daa71f0) Thanks [@patrick-medusajs](https://github.com/patrick-medusajs)! - chore: explicitly add devDependencies for monorepo peerDependencies
- Updated dependencies [[`7cced6006`](https://github.com/medusajs/medusa/commit/7cced6006a9a6f9108009e9f3e191e9f3ba1b168)]:
- medusa-core-utils@1.1.37
## 1.1.1
### Patch Changes
- [#2433](https://github.com/medusajs/medusa/pull/2433) [`3c5e31c64`](https://github.com/medusajs/medusa/commit/3c5e31c6455695f854e9df7a3592c12b899fa1e1) Thanks [@pKorsholm](https://github.com/pKorsholm)! - Add protected uploads to fileservices
## 1.1.0
### Minor Changes
- [#2159](https://github.com/medusajs/medusa/pull/2159) [`70cf3f1f2`](https://github.com/medusajs/medusa/commit/70cf3f1f2c314dff08dbd53bbe4e5d278958cf67) Thanks [@shahednasser](https://github.com/shahednasser)! - Version bump
## 1.0.10
### Patch Changes
- Updated dependencies [[`c97ccd3fb`](https://github.com/medusajs/medusa/commit/c97ccd3fb5dbe796b0e4fbf37def5bb6e8201557)]:
- medusa-interfaces@1.3.3
## 1.0.9
### Patch Changes
- [#1991](https://github.com/medusajs/medusa/pull/1991) [`3cde81748`](https://github.com/medusajs/medusa/commit/3cde817482df6c3cc8b931be30775fb34f85058a) Thanks [@pKorsholm](https://github.com/pKorsholm)! - Adds missing `path` import
## 1.0.8
### Patch Changes
- [#1914](https://github.com/medusajs/medusa/pull/1914) [`1dec44287`](https://github.com/medusajs/medusa/commit/1dec44287df5ac69b4c5769b59f9ebef58d3da68) Thanks [@fPolic](https://github.com/fPolic)! - Version bump due to missing changesets in merged PRs
- Updated dependencies [[`1dec44287`](https://github.com/medusajs/medusa/commit/1dec44287df5ac69b4c5769b59f9ebef58d3da68), [`b8ddb31f6`](https://github.com/medusajs/medusa/commit/b8ddb31f6fe296a11d2d988276ba8e991c37fa9b)]:
- medusa-interfaces@1.3.2
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.0.7](https://github.com/medusajs/medusa/compare/medusa-file-minio@1.0.6...medusa-file-minio@1.0.7) (2022-07-05)
### Bug Fixes
- **medusa-file-spaces,medusa-file-s3,medusa-file-minio:** Add options to super call in file plugins ([#1714](https://github.com/medusajs/medusa/issues/1714)) ([a5f717b](https://github.com/medusajs/medusa/commit/a5f717be5ae1954f3dbf1e7b2edb35d11088a8c8))
### Features
- **medusa:** Delete and download url endpoints ([#1705](https://github.com/medusajs/medusa/issues/1705)) ([cc29b64](https://github.com/medusajs/medusa/commit/cc29b641c9358415b46179371988e7ddc11d2664))
- **medusa:** Extend file-service interface + move to core ([#1577](https://github.com/medusajs/medusa/issues/1577)) ([8e42d37](https://github.com/medusajs/medusa/commit/8e42d37e84e80c003b9c0311117ab8a8871aa61b))
## [1.0.6](https://github.com/medusajs/medusa/compare/medusa-file-minio@1.0.4...medusa-file-minio@1.0.6) (2022-06-19)
**Note:** Version bump only for package medusa-file-minio
## [1.0.5](https://github.com/medusajs/medusa/compare/medusa-file-minio@1.0.4...medusa-file-minio@1.0.5) (2022-05-31)
**Note:** Version bump only for package medusa-file-minio
## [1.0.4](https://github.com/medusajs/medusa/compare/medusa-file-minio@1.0.3...medusa-file-minio@1.0.4) (2022-01-11)
**Note:** Version bump only for package medusa-file-minio
## [1.0.3](https://github.com/medusajs/medusa/compare/medusa-file-minio@1.0.2...medusa-file-minio@1.0.3) (2021-12-29)
**Note:** Version bump only for package medusa-file-minio
## [1.0.2](https://github.com/medusajs/medusa/compare/medusa-file-minio@1.0.1...medusa-file-minio@1.0.2) (2021-12-17)
**Note:** Version bump only for package medusa-file-minio
## 1.0.1 (2021-12-08)
**Note:** Version bump only for package medusa-file-minio

View File

@@ -1,72 +0,0 @@
# MinIO
Store uploaded files to your Medusa backend on MinIO.
[Plugin Documentation](https://docs.medusajs.com/plugins/file-service/minio) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa)
## Features
- Store product images on MinIO
- Support for importing and exporting data through CSV files, such as Products or Prices.
- Support for both private and public buckets.
---
## Prerequisites
- [Medusa backend](https://docs.medusajs.com/development/backend/install)
- [MinIO](https://docs.min.io/minio/baremetal/quickstart/quickstart.html)
---
## How to Install
1\. Run the following command in the directory of the Medusa backend:
```bash
npm install medusa-file-minio
```
2\. Set the following environment variables in `.env`:
```bash
MINIO_ENDPOINT=<ENDPOINT>
MINIO_BUCKET=<BUCKET>
MINIO_ACCESS_KEY=<ACCESS_KEY>
MINIO_SECRET_KEY=<SECRET_KEY>
```
3\. In `medusa-config.js` add the following at the end of the `plugins` array:
```js
const plugins = [
// ...
{
resolve: `medusa-file-minio`,
options: {
endpoint: process.env.MINIO_ENDPOINT,
bucket: process.env.MINIO_BUCKET,
access_key_id: process.env.MINIO_ACCESS_KEY,
secret_access_key: process.env.MINIO_SECRET_KEY,
},
},
]
```
---
## Test the Plugin
1\. Run the following command in the directory of the Medusa backend to run the backend:
```bash
npm run start
```
2\. Upload an image for a product using the admin dashboard or using [the Admin APIs](https://docs.medusajs.com/api/admin#tag/Upload).
---
## Additional Resources
- [MinIO Plugin Documentation](https://docs.medusajs.com/plugins/file-service/minio)

View File

@@ -1 +0,0 @@
// noop

View File

@@ -1,48 +0,0 @@
{
"name": "medusa-file-minio",
"version": "1.3.1",
"description": "MinIO server file connector for Medusa",
"main": "dist/index.js",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-file-minio"
},
"engines": {
"node": ">=16"
},
"author": "Edin Skeja",
"license": "MIT",
"devDependencies": {
"@medusajs/medusa": "^1.20.1",
"@medusajs/types": "^1.11.11",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"rimraf": "^5.0.1",
"typescript": "^4.9.5"
},
"scripts": {
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"test": "jest --passWithNoTests src",
"build": "rimraf dist && tsc",
"watch": "tsc --watch"
},
"peerDependencies": {
"@medusajs/medusa": "^1.12.0"
},
"dependencies": {
"aws-sdk": "^2.983.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"medusa-core-utils": "^1.2.0",
"medusa-test-utils": "^1.1.40"
},
"gitHead": "3bbd1e8507e00bc471de6ae3c30207999a4a4011",
"keywords": [
"medusa-plugin",
"medusa-plugin-file"
]
}

View File

@@ -1,212 +0,0 @@
import { AbstractFileService, IFileService } from "@medusajs/medusa"
import {
DeleteFileType,
FileServiceUploadResult,
GetUploadedFileType,
UploadStreamDescriptorType,
} from "@medusajs/types"
import { ClientConfiguration, PutObjectRequest } from "aws-sdk/clients/s3"
import { MedusaError } from "medusa-core-utils"
import aws from "aws-sdk"
import fs from "fs"
import { parse } from "path"
import stream from "stream"
class MinioService extends AbstractFileService implements IFileService {
protected bucket_: string
protected accessKeyId_: string
protected secretAccessKey_: string
protected private_bucket_: string
protected private_access_key_id_: string
protected private_secret_access_key_: string
protected endpoint_: string
protected s3ForcePathStyle_: boolean
protected signatureVersion_: string
protected downloadUrlDuration: string | number
constructor({}, options) {
super(arguments[0], options)
this.bucket_ = options.bucket
this.accessKeyId_ = options.access_key_id
this.secretAccessKey_ = options.secret_access_key
this.private_bucket_ = options.private_bucket
this.private_access_key_id_ =
options.private_access_key_id ?? this.accessKeyId_
this.private_secret_access_key_ =
options.private_secret_access_key ?? this.secretAccessKey_
this.endpoint_ = options.endpoint
this.s3ForcePathStyle_ = true
this.signatureVersion_ = "v4"
this.downloadUrlDuration = options.download_url_duration ?? 60 // 60 seconds
}
protected buildUrl(bucket: string, key: string) {
return `${this.endpoint_}/${bucket}/${key}`
}
async upload(file: Express.Multer.File): Promise<FileServiceUploadResult> {
return await this.uploadFile(file)
}
async uploadProtected(
file: Express.Multer.File
): Promise<FileServiceUploadResult> {
this.validatePrivateBucketConfiguration_(true)
return await this.uploadFile(file, { isProtected: true })
}
protected async uploadFile(
file: Express.Multer.File,
options: { isProtected: boolean } = { isProtected: false }
) {
const parsedFilename = parse(file.originalname)
const fileKey = `${parsedFilename.name}-${Date.now()}${parsedFilename.ext}`
const client = this.getClient(options.isProtected)
const params = {
ACL: options.isProtected ? "private" : "public-read",
Bucket: options.isProtected ? this.private_bucket_ : this.bucket_,
Body: fs.createReadStream(file.path),
Key: fileKey,
ContentType: file.mimetype,
}
const result = await client.upload(params).promise()
return { url: result.Location, key: result.Key }
}
async delete(file: DeleteFileType): Promise<void> {
const privateClient = this.getClient(false)
const publicClient = this.getClient(true)
const params = {
Bucket: this.bucket_,
Key: `${file.fileKey}`,
}
await Promise.all([
new Promise((resolve, reject) =>
publicClient.deleteObject(
{ ...params, Bucket: this.bucket_ },
(err, data) => {
if (err) {
reject(err)
return
}
resolve(data)
}
)
),
new Promise((resolve, reject) =>
privateClient.deleteObject(
{ ...params, Bucket: this.private_bucket_ },
(err, data) => {
if (err) {
reject(err)
return
}
resolve(data)
}
)
),
])
}
async getUploadStreamDescriptor(
fileData: UploadStreamDescriptorType & {
contentType?: string
}
) {
const usePrivateBucket = fileData.isPrivate ?? true
this.validatePrivateBucketConfiguration_(usePrivateBucket)
const client = this.getClient(usePrivateBucket)
const pass = new stream.PassThrough()
const fileKey = `${fileData.name}.${fileData.ext}`
const params: PutObjectRequest = {
Bucket: usePrivateBucket ? this.private_bucket_ : this.bucket_,
Body: pass,
Key: fileKey,
ContentType: fileData.contentType,
}
return {
writeStream: pass,
promise: client.upload(params).promise(),
url: this.buildUrl(params.Bucket, fileKey),
fileKey,
}
}
async getDownloadStream(fileData: GetUploadedFileType) {
const usePrivateBucket = fileData.isPrivate ?? true
this.validatePrivateBucketConfiguration_(usePrivateBucket)
const client = this.getClient(usePrivateBucket)
const params = {
Bucket: usePrivateBucket ? this.private_bucket_ : this.bucket_,
Key: `${fileData.fileKey}`,
}
return client.getObject(params).createReadStream()
}
async getPresignedDownloadUrl({
isPrivate = true,
...fileData
}: GetUploadedFileType) {
this.validatePrivateBucketConfiguration_(isPrivate)
const client = this.getClient(isPrivate, {
signatureVersion: "v4",
})
const params = {
Bucket: isPrivate ? this.private_bucket_ : this.bucket_,
Key: `${fileData.fileKey}`,
Expires: this.downloadUrlDuration,
}
return await client.getSignedUrlPromise("getObject", params)
}
validatePrivateBucketConfiguration_(usePrivateBucket: boolean) {
if (
usePrivateBucket &&
(!this.private_access_key_id_ || !this.private_bucket_)
) {
throw new MedusaError(
MedusaError.Types.UNEXPECTED_STATE,
"Private bucket is not configured"
)
}
}
protected getClient(
usePrivateBucket = false,
additionalConfiguration: Partial<ClientConfiguration> = {}
) {
return new aws.S3({
accessKeyId: usePrivateBucket
? this.private_access_key_id_
: this.accessKeyId_,
secretAccessKey: usePrivateBucket
? this.private_secret_access_key_
: this.secretAccessKey_,
endpoint: this.endpoint_,
s3ForcePathStyle: this.s3ForcePathStyle_,
signatureVersion: this.signatureVersion_,
...additionalConfiguration,
})
}
}
export default MinioService

View File

@@ -1,30 +0,0 @@
{
"compilerOptions": {
"lib": ["es5", "es6", "es2019"],
"target": "es5",
"outDir": "./dist",
"rootDir": "src",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true // to use ES5 specific tooling
},
"include": ["src"],
"exclude": [
"dist",
"src/**/__tests__",
"src/**/__mocks__",
"src/**/__fixtures__",
"node_modules"
]
}

View File

@@ -1,13 +0,0 @@
{
"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"]
}
}
}

View File

@@ -1,16 +0,0 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock
/dist
/api
/services
/models
/subscribers
/__mocks__

View File

@@ -1,8 +0,0 @@
.DS_store
src
dist
yarn.lock
.babelrc
.turbo
.yarn

View File

@@ -1,218 +0,0 @@
# Change Log
## 1.4.1
### Patch Changes
- [#6100](https://github.com/medusajs/medusa/pull/6100) [`4792c5522`](https://github.com/medusajs/medusa/commit/4792c552269c147d3c07da49a175e9038f9260a8) Thanks [@shahednasser](https://github.com/shahednasser)! - chore(@medusajs/medusa): add missing constructor to some services
fix(@medusajs/file-local): Fix argument passed to the constructor
fix(medusa-file-minio): Fix argument passed to the constructor
fix(medusa-file-s3): Fix argument passed to the constructor
## 1.4.0
### Minor Changes
- [#5291](https://github.com/medusajs/medusa/pull/5291) [`bbd9dd408`](https://github.com/medusajs/medusa/commit/bbd9dd408f04dc95eb7d2d57984fb61e5c015bbc) Thanks [@pepijn-vanvlaanderen](https://github.com/pepijn-vanvlaanderen)! - Added config to set S3 prefix
## 1.3.1
### Patch Changes
- [#4884](https://github.com/medusajs/medusa/pull/4884) [`046b0dcfa`](https://github.com/medusajs/medusa/commit/046b0dcfa5acfdbf98f5a5593b42673c7567430d) Thanks [@pevey](https://github.com/pevey)! - Chore(medusa-file-s3): Add cache-control option, fix delete function, update to sdk v3
## 1.3.0
### Minor Changes
- [`e91bd9e1c`](https://github.com/medusajs/medusa/commit/e91bd9e1c1746ff2fe915d169077bf9bf2710dcf) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Minor-bumping file plugins
## 1.2.2
### Patch Changes
- [#4788](https://github.com/medusajs/medusa/pull/4788) [`d8a6e3e0d`](https://github.com/medusajs/medusa/commit/d8a6e3e0d8a86aba7209f4a767cd08ebe3e49c26) Thanks [@fPolic](https://github.com/fPolic)! - feat(medusa-file-local): local file service streaming methods
- [#4771](https://github.com/medusajs/medusa/pull/4771) [`edf9ed4e5`](https://github.com/medusajs/medusa/commit/edf9ed4e593063622aa39cdbebef4810bf2a5fb1) Thanks [@fPolic](https://github.com/fPolic)! - fix(medusa-interfaces, medusa-file-\*): add `ìsPrivate` flag to the streaming methods, fix minio default bucket
## 1.2.1
### Patch Changes
- [#4276](https://github.com/medusajs/medusa/pull/4276) [`afd1b67f1`](https://github.com/medusajs/medusa/commit/afd1b67f1c7de8cf07fd9fcbdde599a37914e9b5) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Use caret range
## 1.2.0
### Minor Changes
- [#3740](https://github.com/medusajs/medusa/pull/3740) [`8b6464180`](https://github.com/medusajs/medusa/commit/8b6464180a82bcc41197f2a97e58b9555a7072cd) Thanks [@dwene](https://github.com/dwene)! - Migrate medusa-file-minio and medusa-file-s3 to typescript.
## 1.1.12
### Patch Changes
- [#3260](https://github.com/medusajs/medusa/pull/3260) [`13c200ad2`](https://github.com/medusajs/medusa/commit/13c200ad2f394734a126ea8920840a70e61401f7) Thanks [@pevey](https://github.com/pevey)! - fix(medusa-file-s3): update s3 file service to reuse one s3 client
- Updated dependencies [[`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`aa690beed`](https://github.com/medusajs/medusa/commit/aa690beed775646cbc86b445fb5dc90dcac087d5), [`54dcc1871`](https://github.com/medusajs/medusa/commit/54dcc1871c8f28bea962dbb9df6e79b038d56449), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73)]:
- medusa-core-utils@1.2.0
- medusa-interfaces@1.3.7
- medusa-test-utils@1.1.40
## 1.1.12-rc.0
### Patch Changes
- [#3260](https://github.com/medusajs/medusa/pull/3260) [`13c200ad2`](https://github.com/medusajs/medusa/commit/13c200ad2f394734a126ea8920840a70e61401f7) Thanks [@pevey](https://github.com/pevey)! - fix(medusa-file-s3): update s3 file service to reuse one s3 client
- Updated dependencies [[`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`aa690beed`](https://github.com/medusajs/medusa/commit/aa690beed775646cbc86b445fb5dc90dcac087d5), [`54dcc1871`](https://github.com/medusajs/medusa/commit/54dcc1871c8f28bea962dbb9df6e79b038d56449), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73)]:
- medusa-core-utils@1.2.0-rc.0
- medusa-interfaces@1.3.7-rc.0
- medusa-test-utils@1.1.40-rc.0
## 1.1.11
### Patch Changes
- [#3217](https://github.com/medusajs/medusa/pull/3217) [`8c5219a31`](https://github.com/medusajs/medusa/commit/8c5219a31ef76ee571fbce84d7d57a63abe56eb0) Thanks [@adrien2p](https://github.com/adrien2p)! - chore: Fix npm packages files included
- Updated dependencies [[`8c5219a31`](https://github.com/medusajs/medusa/commit/8c5219a31ef76ee571fbce84d7d57a63abe56eb0)]:
- medusa-core-utils@1.1.39
- medusa-interfaces@1.3.6
## 1.1.10
### Patch Changes
- [#3185](https://github.com/medusajs/medusa/pull/3185) [`08324355a`](https://github.com/medusajs/medusa/commit/08324355a4466b017a0bc7ab1d333ee3cd27b8c4) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Patches all dependencies + minor bumps `winston` to include a [fix for a significant memory leak](https://github.com/winstonjs/winston/pull/2057)
- Updated dependencies [[`08324355a`](https://github.com/medusajs/medusa/commit/08324355a4466b017a0bc7ab1d333ee3cd27b8c4)]:
- medusa-core-utils@1.1.38
- medusa-interfaces@1.3.5
## 1.1.9
### Patch Changes
- [#3025](https://github.com/medusajs/medusa/pull/3025) [`93d0dc1bd`](https://github.com/medusajs/medusa/commit/93d0dc1bdcb54cf6e87428a7bb9b0dac196b4de2) Thanks [@adrien2p](https://github.com/adrien2p)! - fix(medusa): test, build and watch scripts
- Updated dependencies [[`93d0dc1bd`](https://github.com/medusajs/medusa/commit/93d0dc1bdcb54cf6e87428a7bb9b0dac196b4de2)]:
- medusa-interfaces@1.3.4
## 1.1.8
### Patch Changes
- [#2808](https://github.com/medusajs/medusa/pull/2808) [`0a9c89185`](https://github.com/medusajs/medusa/commit/0a9c891853c4d16b553d38268a3408ca1daa71f0) Thanks [@patrick-medusajs](https://github.com/patrick-medusajs)! - chore: explicitly add devDependencies for monorepo peerDependencies
- [#2809](https://github.com/medusajs/medusa/pull/2809) [`79cddc23d`](https://github.com/medusajs/medusa/commit/79cddc23da66d5cc47fa0aeba81d80cab867d6ad) Thanks [@pKorsholm](https://github.com/pKorsholm)! - Add options for s3 configuration for increased flexibility in configuration
- Updated dependencies [[`7cced6006`](https://github.com/medusajs/medusa/commit/7cced6006a9a6f9108009e9f3e191e9f3ba1b168)]:
- medusa-core-utils@1.1.37
## 1.1.7
### Patch Changes
- [#2433](https://github.com/medusajs/medusa/pull/2433) [`3c5e31c64`](https://github.com/medusajs/medusa/commit/3c5e31c6455695f854e9df7a3592c12b899fa1e1) Thanks [@pKorsholm](https://github.com/pKorsholm)! - Add protected uploads to fileservices
## 1.1.6
### Patch Changes
- [`8f4135fd5`](https://github.com/medusajs/medusa/commit/8f4135fd5ff6d6d24f395d705fe4e45ca9769415) Thanks [@olivermrbl](https://github.com/olivermrbl)! - Add AWS S3 export support
- Updated dependencies [[`7dc8d3a0c`](https://github.com/medusajs/medusa/commit/7dc8d3a0c90ce06e3f11a6a46dec1f9ec3f26e81)]:
- medusa-core-utils@1.1.32
## 1.1.5
### Patch Changes
- Updated dependencies [[`c97ccd3fb`](https://github.com/medusajs/medusa/commit/c97ccd3fb5dbe796b0e4fbf37def5bb6e8201557)]:
- medusa-interfaces@1.3.3
## 1.1.4
### Patch Changes
- [#1914](https://github.com/medusajs/medusa/pull/1914) [`1dec44287`](https://github.com/medusajs/medusa/commit/1dec44287df5ac69b4c5769b59f9ebef58d3da68) Thanks [@fPolic](https://github.com/fPolic)! - Version bump due to missing changesets in merged PRs
- Updated dependencies [[`1dec44287`](https://github.com/medusajs/medusa/commit/1dec44287df5ac69b4c5769b59f9ebef58d3da68), [`b8ddb31f6`](https://github.com/medusajs/medusa/commit/b8ddb31f6fe296a11d2d988276ba8e991c37fa9b)]:
- medusa-interfaces@1.3.2
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.1.3](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.1.2...medusa-file-s3@1.1.3) (2022-07-05)
### Bug Fixes
- **medusa-file-spaces,medusa-file-s3,medusa-file-minio:** Add options to super call in file plugins ([#1714](https://github.com/medusajs/medusa/issues/1714)) ([a5f717b](https://github.com/medusajs/medusa/commit/a5f717be5ae1954f3dbf1e7b2edb35d11088a8c8))
### Features
- **medusa:** Extend file-service interface + move to core ([#1577](https://github.com/medusajs/medusa/issues/1577)) ([8e42d37](https://github.com/medusajs/medusa/commit/8e42d37e84e80c003b9c0311117ab8a8871aa61b))
## [1.1.2](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.1.0...medusa-file-s3@1.1.2) (2022-06-19)
**Note:** Version bump only for package medusa-file-s3
## [1.1.1](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.1.0...medusa-file-s3@1.1.1) (2022-05-31)
**Note:** Version bump only for package medusa-file-s3
# [1.1.0](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.12...medusa-file-s3@1.1.0) (2022-05-01)
**Note:** Version bump only for package medusa-file-s3
## [1.0.12](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.11...medusa-file-s3@1.0.12) (2022-01-11)
**Note:** Version bump only for package medusa-file-s3
## [1.0.11](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.10...medusa-file-s3@1.0.11) (2021-12-29)
**Note:** Version bump only for package medusa-file-s3
## [1.0.10](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.9...medusa-file-s3@1.0.10) (2021-12-17)
**Note:** Version bump only for package medusa-file-s3
## [1.0.9](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.8...medusa-file-s3@1.0.9) (2021-12-08)
**Note:** Version bump only for package medusa-file-s3
## [1.0.8](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.7...medusa-file-s3@1.0.8) (2021-11-23)
**Note:** Version bump only for package medusa-file-s3
## [1.0.7](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.6...medusa-file-s3@1.0.7) (2021-11-22)
**Note:** Version bump only for package medusa-file-s3
## [1.0.6](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.5...medusa-file-s3@1.0.6) (2021-11-19)
**Note:** Version bump only for package medusa-file-s3
## [1.0.5](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.4...medusa-file-s3@1.0.5) (2021-11-19)
**Note:** Version bump only for package medusa-file-s3
## [1.0.4](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.3...medusa-file-s3@1.0.4) (2021-10-18)
**Note:** Version bump only for package medusa-file-s3
## [1.0.3](https://github.com/medusajs/medusa/compare/medusa-file-s3@1.0.2...medusa-file-s3@1.0.3) (2021-10-18)
**Note:** Version bump only for package medusa-file-s3
## 1.0.2 (2021-10-18)
### Features
- AWS S3 file service plugin ([#376](https://github.com/medusajs/medusa/issues/376)) ([75b6083](https://github.com/medusajs/medusa/commit/75b608330b51a2c4ac22e7e63766346d17dda9a7))
## 1.0.1 (2021-10-18)
### Features
- AWS S3 file service plugin ([#376](https://github.com/medusajs/medusa/issues/376)) ([75b6083](https://github.com/medusajs/medusa/commit/75b608330b51a2c4ac22e7e63766346d17dda9a7))

View File

@@ -1,77 +0,0 @@
# S3
Store uploaded files to your Medusa backend on S3.
[Plugin Documentation](https://docs.medusajs.com/plugins/file-service/s3) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa)
## Features
- Store product images on S3
- Support for importing and exporting data through CSV files, such as Products or Prices.
- Support for Bucket Policies and User Permissions.
---
## Prerequisites
- [Medusa backend](https://docs.medusajs.com/development/backend/install)
- [S3](https://aws.amazon.com/s3)
---
## How to Install
1\. Run the following command in the directory of the Medusa backend:
```bash
npm install medusa-file-s3
```
2\. Set the following environment variables in `.env`:
```bash
S3_URL=<YOUR_BUCKET_URL>
S3_BUCKET=<YOUR_BUCKET_NAME>
S3_REGION=<YOUR_BUCKET_REGION>
S3_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
S3_SECRET_ACCESS_KEY=<YOUR_SECRET_ACCESS_KEY>
S3_PREFIX=<YOUR_BUCKET_PREFIX> (optional)
```
3\. In `medusa-config.js` add the following at the end of the `plugins` array:
```js
const plugins = [
// ...
{
resolve: `medusa-file-s3`,
options: {
s3_url: process.env.S3_URL,
bucket: process.env.S3_BUCKET,
prefix: process.env.S3_PREFIX, // optional
region: process.env.S3_REGION,
access_key_id: process.env.S3_ACCESS_KEY_ID,
secret_access_key: process.env.S3_SECRET_ACCESS_KEY,
aws_config_object: {},
},
},
]
```
---
## Test the Plugin
1\. Run the following command in the directory of the Medusa backend to run the backend:
```bash
npm run start
```
2\. Upload an image for a product using the admin dashboard or using [the Admin APIs](https://docs.medusajs.com/api/admin#tag/Upload).
---
## Additional Resources
- [S3 Plugin Documentation](https://docs.medusajs.com/plugins/file-service/s3)

View File

@@ -1 +0,0 @@
// noop

View File

@@ -1,51 +0,0 @@
{
"name": "medusa-file-s3",
"version": "1.4.1",
"description": "AWS s3 file connector for Medusa",
"main": "dist/index.js",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-file-s3"
},
"engines": {
"node": ">=16"
},
"author": "Sebastian Mateos Nicolajsen",
"license": "MIT",
"devDependencies": {
"@medusajs/medusa": "^1.20.1",
"@medusajs/types": "^1.11.11",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"medusa-test-utils": "^1.1.40",
"rimraf": "^5.0.1",
"typescript": "^4.9.5"
},
"scripts": {
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"test": "jest --passWithNoTests src",
"build": "rimraf dist && tsc",
"watch": "tsc --watch"
},
"peerDependencies": {
"@medusajs/medusa": "^1.12.0"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.400.0",
"@aws-sdk/lib-storage": "^3.400.0",
"@aws-sdk/s3-request-presigner": "^3.400.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"medusa-core-utils": "^1.2.0",
"medusa-test-utils": "^1.1.40"
},
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808",
"keywords": [
"medusa-plugin",
"medusa-plugin-file"
]
}

View File

@@ -1,172 +0,0 @@
import fs from "fs"
import type { S3ClientConfigType, PutObjectCommandInput, GetObjectCommandOutput } from "@aws-sdk/client-s3"
import { Upload } from "@aws-sdk/lib-storage"
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"
import {
S3Client,
PutObjectCommand,
DeleteObjectCommand,
GetObjectCommand
} from "@aws-sdk/client-s3"
import { parse } from "path"
import { AbstractFileService, IFileService } from "@medusajs/medusa"
import {
DeleteFileType,
FileServiceUploadResult,
GetUploadedFileType,
UploadStreamDescriptorType,
Logger
} from "@medusajs/types"
import stream from "stream"
class S3Service extends AbstractFileService implements IFileService {
protected prefix_: string
protected bucket_: string
protected s3Url_: string
protected accessKeyId_: string
protected secretAccessKey_: string
protected region_: string
protected awsConfigObject_: any
protected downloadFileDuration_: number
protected cacheControl_: string
protected logger_: Logger
protected client_: S3Client
constructor({ logger }, options) {
super(arguments[0], options)
this.prefix_ = options.prefix ? `${options.prefix}/` : ''
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.downloadFileDuration_ = options.download_file_duration
this.awsConfigObject_ = options.aws_config_object ?? {}
this.cacheControl_ = options.cache_control ?? "max-age=31536000"
this.logger_ = logger
this.client_ = this.getClient()
}
protected getClient(overwriteConfig: Partial<S3ClientConfigType> = {}) {
const config: S3ClientConfigType = {
credentials: {
accessKeyId: this.accessKeyId_,
secretAccessKey: this.secretAccessKey_,
},
region: this.region_,
...this.awsConfigObject_,
signatureVersion: 'v4',
...overwriteConfig,
}
return new S3Client(config)
}
async upload(file: Express.Multer.File): Promise<FileServiceUploadResult> {
return await this.uploadFile(file)
}
async uploadProtected(file: Express.Multer.File) {
return await this.uploadFile(file, { acl: "private" })
}
async uploadFile(
file: Express.Multer.File,
options: { isProtected?: boolean; acl?: string } = {
isProtected: false,
acl: undefined,
}
) {
const parsedFilename = parse(file.originalname)
const fileKey = `${this.prefix_}${parsedFilename.name}-${Date.now()}${parsedFilename.ext}`
const command = new PutObjectCommand({
ACL: options.acl ?? (options.isProtected ? "private" : "public-read"),
Bucket: this.bucket_,
Body: fs.createReadStream(file.path),
Key: fileKey,
ContentType: file.mimetype,
CacheControl: this.cacheControl_
})
try {
await this.client_.send(command)
return {
url: `${this.s3Url_}/${fileKey}`,
key: fileKey,
}
} catch (e) {
this.logger_.error(e)
throw e
}
}
async delete(file: DeleteFileType): Promise<void> {
const command = new DeleteObjectCommand({
Bucket: this.bucket_,
Key: `${file.file_key}`,
})
try {
await this.client_.send(command)
} catch (e) {
this.logger_.error(e)
}
}
async getUploadStreamDescriptor(fileData: UploadStreamDescriptorType) {
const pass = new stream.PassThrough()
const isPrivate = fileData.isPrivate ?? true // default to private
const fileKey = `${this.prefix_}${fileData.name}.${fileData.ext}`
const params: PutObjectCommandInput = {
ACL: isPrivate ? "private" : "public-read",
Bucket: this.bucket_,
Body: pass,
Key: fileKey,
ContentType: fileData.contentType as string,
}
const uploadJob = new Upload({
client: this.client_,
params
})
return {
writeStream: pass,
promise: uploadJob.done(),
url: `${this.s3Url_}/${fileKey}`,
fileKey,
}
}
async getDownloadStream(
fileData: GetUploadedFileType
): Promise<NodeJS.ReadableStream> {
const command = new GetObjectCommand({
Bucket: this.bucket_,
Key: `${fileData.fileKey}`,
})
const response: GetObjectCommandOutput = await this.client_.send(command)
return response.Body as NodeJS.ReadableStream
}
async getPresignedDownloadUrl(
fileData: GetUploadedFileType
): Promise<string> {
const command = new GetObjectCommand({
Bucket: this.bucket_,
Key: `${fileData.fileKey}`,
})
return await getSignedUrl(this.client_, command, { expiresIn: this.downloadFileDuration_ })
}
}
export default S3Service

View File

@@ -1,30 +0,0 @@
{
"compilerOptions": {
"lib": ["es5", "es6", "es2019"],
"target": "es5",
"outDir": "./dist",
"rootDir": "src",
"esModuleInterop": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true
},
"include": ["src"],
"exclude": [
"dist",
"src/**/__tests__",
"src/**/__mocks__",
"src/**/__fixtures__",
"node_modules"
]
}

View File

@@ -1,13 +0,0 @@
{
"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"]
}
}
}

View File

@@ -1,16 +0,0 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock
/dist
/api
/services
/models
/subscribers
/__mocks__

View File

@@ -1,8 +0,0 @@
.DS_store
src
dist
yarn.lock
.babelrc
.turbo
.yarn

View File

@@ -1,337 +0,0 @@
# Change Log
## 1.4.0
### Minor Changes
- [`e91bd9e1c`](https://github.com/medusajs/medusa/commit/e91bd9e1c1746ff2fe915d169077bf9bf2710dcf) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Minor-bumping file plugins
## 1.3.8
### Patch Changes
- [#4771](https://github.com/medusajs/medusa/pull/4771) [`edf9ed4e5`](https://github.com/medusajs/medusa/commit/edf9ed4e593063622aa39cdbebef4810bf2a5fb1) Thanks [@fPolic](https://github.com/fPolic)! - fix(medusa-interfaces, medusa-file-\*): add `ìsPrivate` flag to the streaming methods, fix minio default bucket
## 1.3.7
### Patch Changes
- [#4276](https://github.com/medusajs/medusa/pull/4276) [`afd1b67f1`](https://github.com/medusajs/medusa/commit/afd1b67f1c7de8cf07fd9fcbdde599a37914e9b5) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Use caret range
## 1.3.6
### Patch Changes
- Updated dependencies [[`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`aa690beed`](https://github.com/medusajs/medusa/commit/aa690beed775646cbc86b445fb5dc90dcac087d5), [`54dcc1871`](https://github.com/medusajs/medusa/commit/54dcc1871c8f28bea962dbb9df6e79b038d56449), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73)]:
- medusa-core-utils@1.2.0
- medusa-interfaces@1.3.7
- medusa-test-utils@1.1.40
## 1.3.6-rc.0
### Patch Changes
- Updated dependencies [[`121b42acf`](https://github.com/medusajs/medusa/commit/121b42acfe98c12dd593f9b1f2072ff0f3b61724), [`aa690beed`](https://github.com/medusajs/medusa/commit/aa690beed775646cbc86b445fb5dc90dcac087d5), [`54dcc1871`](https://github.com/medusajs/medusa/commit/54dcc1871c8f28bea962dbb9df6e79b038d56449), [`77d46220c`](https://github.com/medusajs/medusa/commit/77d46220c23bfe19e575cbc445874eb6c22f3c73)]:
- medusa-core-utils@1.2.0-rc.0
- medusa-interfaces@1.3.7-rc.0
- medusa-test-utils@1.1.40-rc.0
## 1.3.5
### Patch Changes
- [#3217](https://github.com/medusajs/medusa/pull/3217) [`8c5219a31`](https://github.com/medusajs/medusa/commit/8c5219a31ef76ee571fbce84d7d57a63abe56eb0) Thanks [@adrien2p](https://github.com/adrien2p)! - chore: Fix npm packages files included
- Updated dependencies [[`8c5219a31`](https://github.com/medusajs/medusa/commit/8c5219a31ef76ee571fbce84d7d57a63abe56eb0)]:
- medusa-core-utils@1.1.39
- medusa-interfaces@1.3.6
## 1.3.4
### Patch Changes
- [#3185](https://github.com/medusajs/medusa/pull/3185) [`08324355a`](https://github.com/medusajs/medusa/commit/08324355a4466b017a0bc7ab1d333ee3cd27b8c4) Thanks [@olivermrbl](https://github.com/olivermrbl)! - chore: Patches all dependencies + minor bumps `winston` to include a [fix for a significant memory leak](https://github.com/winstonjs/winston/pull/2057)
- Updated dependencies [[`08324355a`](https://github.com/medusajs/medusa/commit/08324355a4466b017a0bc7ab1d333ee3cd27b8c4)]:
- medusa-core-utils@1.1.38
- medusa-interfaces@1.3.5
## 1.3.3
### Patch Changes
- [#3025](https://github.com/medusajs/medusa/pull/3025) [`93d0dc1bd`](https://github.com/medusajs/medusa/commit/93d0dc1bdcb54cf6e87428a7bb9b0dac196b4de2) Thanks [@adrien2p](https://github.com/adrien2p)! - fix(medusa): test, build and watch scripts
- Updated dependencies [[`93d0dc1bd`](https://github.com/medusajs/medusa/commit/93d0dc1bdcb54cf6e87428a7bb9b0dac196b4de2)]:
- medusa-interfaces@1.3.4
## 1.3.2
### Patch Changes
- [#2808](https://github.com/medusajs/medusa/pull/2808) [`0a9c89185`](https://github.com/medusajs/medusa/commit/0a9c891853c4d16b553d38268a3408ca1daa71f0) Thanks [@patrick-medusajs](https://github.com/patrick-medusajs)! - chore: explicitly add devDependencies for monorepo peerDependencies
- Updated dependencies [[`7cced6006`](https://github.com/medusajs/medusa/commit/7cced6006a9a6f9108009e9f3e191e9f3ba1b168)]:
- medusa-core-utils@1.1.37
## 1.3.1
### Patch Changes
- [#2433](https://github.com/medusajs/medusa/pull/2433) [`3c5e31c64`](https://github.com/medusajs/medusa/commit/3c5e31c6455695f854e9df7a3592c12b899fa1e1) Thanks [@pKorsholm](https://github.com/pKorsholm)! - Add protected uploads to fileservices
## 1.3.0
### Minor Changes
- [#2171](https://github.com/medusajs/medusa/pull/2171) [`ee8fe3a88`](https://github.com/medusajs/medusa/commit/ee8fe3a88bb1af20ed8976bd5cf0146da140e29f) Thanks [@fPolic](https://github.com/fPolic)! - Add return `fileKey` for Spaces upload
## 1.2.5
### Patch Changes
- Updated dependencies [[`c97ccd3fb`](https://github.com/medusajs/medusa/commit/c97ccd3fb5dbe796b0e4fbf37def5bb6e8201557)]:
- medusa-interfaces@1.3.3
## 1.2.4
### Patch Changes
- [#1914](https://github.com/medusajs/medusa/pull/1914) [`1dec44287`](https://github.com/medusajs/medusa/commit/1dec44287df5ac69b4c5769b59f9ebef58d3da68) Thanks [@fPolic](https://github.com/fPolic)! - Version bump due to missing changesets in merged PRs
- Updated dependencies [[`1dec44287`](https://github.com/medusajs/medusa/commit/1dec44287df5ac69b4c5769b59f9ebef58d3da68), [`b8ddb31f6`](https://github.com/medusajs/medusa/commit/b8ddb31f6fe296a11d2d988276ba8e991c37fa9b)]:
- medusa-interfaces@1.3.2
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.2.3](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.2.2...medusa-file-spaces@1.2.3) (2022-07-05)
### Bug Fixes
- **medusa-file-spaces,medusa-file-s3,medusa-file-minio:** Add options to super call in file plugins ([#1714](https://github.com/medusajs/medusa/issues/1714)) ([a5f717b](https://github.com/medusajs/medusa/commit/a5f717be5ae1954f3dbf1e7b2edb35d11088a8c8))
### Features
- **medusa:** Add batch strategy for order exports ([#1603](https://github.com/medusajs/medusa/issues/1603)) ([bf47d1a](https://github.com/medusajs/medusa/commit/bf47d1aecd74f4489667609444a8b09393e894d3))
- **medusa:** Extend file-service interface + move to core ([#1577](https://github.com/medusajs/medusa/issues/1577)) ([8e42d37](https://github.com/medusajs/medusa/commit/8e42d37e84e80c003b9c0311117ab8a8871aa61b))
- **medusa-file-spaces:** DigitalOcean fileservice streaming ([#1585](https://github.com/medusajs/medusa/issues/1585)) ([abaf10b](https://github.com/medusajs/medusa/commit/abaf10b31d1e9a60710da87cac5c9c869195660d)), closes [#1583](https://github.com/medusajs/medusa/issues/1583) [#1580](https://github.com/medusajs/medusa/issues/1580) [#1582](https://github.com/medusajs/medusa/issues/1582) [#1583](https://github.com/medusajs/medusa/issues/1583) [#1580](https://github.com/medusajs/medusa/issues/1580) [#1582](https://github.com/medusajs/medusa/issues/1582)
## [1.2.2](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.2.0...medusa-file-spaces@1.2.2) (2022-06-19)
### Bug Fixes
- **medusa-file-spaces:** Allow duplicate filenames ([#1474](https://github.com/medusajs/medusa/issues/1474)) ([525910f](https://github.com/medusajs/medusa/commit/525910f72aa76355c29dd153f28ea08221956f3e))
## [1.2.1](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.2.0...medusa-file-spaces@1.2.1) (2022-05-31)
### Bug Fixes
- **medusa-file-spaces:** Allow duplicate filenames ([#1474](https://github.com/medusajs/medusa/issues/1474)) ([525910f](https://github.com/medusajs/medusa/commit/525910f72aa76355c29dd153f28ea08221956f3e))
# [1.2.0](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.37...medusa-file-spaces@1.2.0) (2022-05-01)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.37](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.36...medusa-file-spaces@1.1.37) (2022-01-11)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.36](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.35...medusa-file-spaces@1.1.36) (2021-12-29)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.35](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.34...medusa-file-spaces@1.1.35) (2021-12-17)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.34](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.33...medusa-file-spaces@1.1.34) (2021-12-08)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.33](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.32...medusa-file-spaces@1.1.33) (2021-11-23)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.32](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.31...medusa-file-spaces@1.1.32) (2021-11-22)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.31](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.30...medusa-file-spaces@1.1.31) (2021-11-19)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.30](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.29...medusa-file-spaces@1.1.30) (2021-11-19)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.29](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.28...medusa-file-spaces@1.1.29) (2021-10-18)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.28](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.27...medusa-file-spaces@1.1.28) (2021-10-18)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.27](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.25...medusa-file-spaces@1.1.27) (2021-10-18)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.26](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.25...medusa-file-spaces@1.1.26) (2021-10-18)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.25](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.24...medusa-file-spaces@1.1.25) (2021-09-15)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.24](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.23...medusa-file-spaces@1.1.24) (2021-09-14)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.23](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.22...medusa-file-spaces@1.1.23) (2021-08-05)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.22](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.21...medusa-file-spaces@1.1.22) (2021-07-26)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.21](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.19...medusa-file-spaces@1.1.21) (2021-07-15)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.20](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.19...medusa-file-spaces@1.1.20) (2021-07-15)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.19](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.18...medusa-file-spaces@1.1.19) (2021-07-02)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.18](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.17...medusa-file-spaces@1.1.18) (2021-06-22)
### Bug Fixes
- release assist ([668e8a7](https://github.com/medusajs/medusa/commit/668e8a740200847fc2a41c91d2979097f1392532))
## [1.1.17](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.16...medusa-file-spaces@1.1.17) (2021-06-09)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.16](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.15...medusa-file-spaces@1.1.16) (2021-06-09)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.15](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.14...medusa-file-spaces@1.1.15) (2021-06-09)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.14](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.13...medusa-file-spaces@1.1.14) (2021-06-09)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.13](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.12...medusa-file-spaces@1.1.13) (2021-06-08)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.12](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.9...medusa-file-spaces@1.1.12) (2021-04-28)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.11](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.10...medusa-file-spaces@1.1.11) (2021-04-20)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.10](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.9...medusa-file-spaces@1.1.10) (2021-04-20)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.9](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.8...medusa-file-spaces@1.1.9) (2021-04-13)
### Bug Fixes
- merge develop ([2982a8e](https://github.com/medusajs/medusa/commit/2982a8e682e90beb4549d969d9d3b04d78a46a2d))
- merge develop ([a468c45](https://github.com/medusajs/medusa/commit/a468c451e82c68f41b5005a2e480057f6124aaa6))
## [1.1.8](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.7...medusa-file-spaces@1.1.8) (2021-04-13)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.7](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.6...medusa-file-spaces@1.1.7) (2021-03-30)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.6](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.5...medusa-file-spaces@1.1.6) (2021-03-17)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.5](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.3...medusa-file-spaces@1.1.5) (2021-03-17)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.4](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.3...medusa-file-spaces@1.1.4) (2021-03-17)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.3](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.2...medusa-file-spaces@1.1.3) (2021-02-17)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.2](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.1...medusa-file-spaces@1.1.2) (2021-02-03)
**Note:** Version bump only for package medusa-file-spaces
## [1.1.1](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.1.0...medusa-file-spaces@1.1.1) (2021-01-27)
**Note:** Version bump only for package medusa-file-spaces
# [1.1.0](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.0.13...medusa-file-spaces@1.1.0) (2021-01-26)
**Note:** Version bump only for package medusa-file-spaces
## [1.0.13](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.0.12...medusa-file-spaces@1.0.13) (2020-12-17)
**Note:** Version bump only for package medusa-file-spaces
## [1.0.12](https://github.com/medusajs/medusa/compare/medusa-file-spaces@1.0.11...medusa-file-spaces@1.0.12) (2020-11-24)
**Note:** Version bump only for package medusa-file-spaces
## 1.0.11 (2020-10-19)
## 1.0.10 (2020-09-09)
### Bug Fixes
- updates license ([db519fb](https://github.com/medusajs/medusa/commit/db519fbaa6f8ad02c19cbecba5d4f28ba1ee81aa))
## 1.0.7 (2020-09-07)
## 1.0.1 (2020-09-05)
## 1.0.1-beta.0 (2020-09-04)
# 1.0.0 (2020-09-03)
# 1.0.0-alpha.30 (2020-08-28)
# 1.0.0-alpha.27 (2020-08-27)
# 1.0.0-alpha.26 (2020-08-27)
# 1.0.0-alpha.24 (2020-08-27)
# 1.0.0-alpha.3 (2020-08-20)
# 1.0.0-alpha.2 (2020-08-20)
# 1.0.0-alpha.1 (2020-08-20)
# 1.0.0-alpha.0 (2020-08-20)
## [1.0.10](https://github.com/medusajs/medusa/compare/v1.0.9...v1.0.10) (2020-09-09)
### Bug Fixes
- updates license ([db519fb](https://github.com/medusajs/medusa/commit/db519fbaa6f8ad02c19cbecba5d4f28ba1ee81aa))

View File

@@ -1,73 +0,0 @@
# DigitalOcean Spaces
Store uploaded files to your Medusa backend on Spaces.
[Plugin Documentation](https://docs.medusajs.com/plugins/file-service/spaces) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa)
## Features
- Store product images on DigitalOcean Spaces
- Support for importing and exporting data through CSV files, such as Products or Prices.
---
## Prerequisites
- [Medusa backend](https://docs.medusajs.com/development/backend/install)
- [DigitalOcean Spaces](https://www.digitalocean.com/products/spaces)
---
## How to Install
1\. Run the following command in the directory of the Medusa backend:
```bash
npm install medusa-file-spaces
```
2\. Set the following environment variables in `.env`:
```bash
SPACE_URL=<YOUR_SPACE_URL>
SPACE_BUCKET=<YOUR_SPACE_NAME>
SPACE_ENDPOINT=<YOUR_SPACE_ENDPOINT>
SPACE_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
SPACE_SECRET_ACCESS_KEY=<YOUR_SECRET_ACCESS_KEY>
```
3\. In `medusa-config.js` add the following at the end of the `plugins` array:
```js
const plugins = [
// ...
{
resolve: `medusa-file-spaces`,
options: {
spaces_url: process.env.SPACE_URL,
bucket: process.env.SPACE_BUCKET,
endpoint: process.env.SPACE_ENDPOINT,
access_key_id: process.env.SPACE_ACCESS_KEY_ID,
secret_access_key: process.env.SPACE_SECRET_ACCESS_KEY,
},
},
]
```
---
## Test the Plugin
1\. Run the following command in the directory of the Medusa backend to run the backend:
```bash
npm run start
```
2\. Upload an image for a product using the admin dashboard or using [the Admin APIs](https://docs.medusajs.com/api/admin#tag/Upload).
---
## Additional Resources
- [Spaces Plugin Documentation](https://docs.medusajs.com/plugins/file-service/spaces)

View File

@@ -1 +0,0 @@
// noop

View File

@@ -1,57 +0,0 @@
{
"name": "medusa-file-spaces",
"version": "1.4.0",
"description": "Digital Ocean Spaces file connector for Medusa",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-file-spaces"
},
"engines": {
"node": ">=16"
},
"author": "Sebastian Rindom",
"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",
"@medusajs/medusa": "^1.15.0",
"client-sessions": "^0.8.0",
"cross-env": "^5.2.1",
"jest": "^25.5.4",
"medusa-interfaces": "^1.3.7",
"medusa-test-utils": "^1.1.40"
},
"scripts": {
"prepare": "cross-env NODE_ENV=production yarn run build",
"test": "jest --passWithNoTests src",
"build": "babel src --out-dir . --ignore '**/__tests__','**/__mocks__'",
"watch": "babel -w src --out-dir . --ignore '**/__tests__','**/__mocks__'"
},
"peerDependencies": {
"@medusajs/medusa": "^1.12.0",
"medusa-interfaces": "^1.3.7"
},
"dependencies": {
"@babel/plugin-transform-classes": "^7.9.5",
"aws-sdk": "^2.710.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"medusa-core-utils": "^1.2.0",
"medusa-test-utils": "^1.1.40",
"stripe": "^8.50.0"
},
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808",
"keywords": [
"medusa-plugin",
"medusa-plugin-file"
]
}

View File

@@ -1,150 +0,0 @@
import { AbstractFileService } from "@medusajs/medusa"
import aws from "aws-sdk"
import fs from "fs"
import { parse } from "path"
import stream from "stream"
class DigitalOceanService extends AbstractFileService {
constructor({}, options) {
super({}, options)
this.bucket_ = options.bucket
this.spacesUrl_ = options.spaces_url?.replace(/\/$/, "")
this.accessKeyId_ = options.access_key_id
this.secretAccessKey_ = options.secret_access_key
this.region_ = options.region
this.endpoint_ = options.endpoint
this.downloadUrlDuration = options.download_url_duration ?? 60 // 60 seconds
}
upload(file) {
this.updateAwsConfig()
return this.uploadFile(file)
}
uploadProtected(file) {
this.updateAwsConfig()
return this.uploadFile(file, { acl: "private" })
}
uploadFile(file, options = { isProtected: false, acl: undefined }) {
const parsedFilename = parse(file.originalname)
const fileKey = `${parsedFilename.name}-${Date.now()}${parsedFilename.ext}`
const s3 = new aws.S3()
const params = {
ACL: options.acl ?? (options.isProtected ? "private" : "public-read"),
Bucket: this.bucket_,
Body: fs.createReadStream(file.path),
Key: fileKey,
}
return new Promise((resolve, reject) => {
s3.upload(params, (err, data) => {
if (err) {
reject(err)
return
}
if (this.spacesUrl_) {
resolve({ url: `${this.spacesUrl_}/${data.Key}`, key: data.Key })
}
resolve({ url: data.Location, key: data.Key })
})
})
}
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) => {
if (err) {
reject(err)
return
}
resolve(data)
})
})
}
async getUploadStreamDescriptor(fileData) {
this.updateAwsConfig()
const pass = new stream.PassThrough()
// default to private
const isPrivate =
typeof fileData.isPrivate === "undefined" ? true : fileData.isPrivate
const fileKey = `${fileData.name}.${fileData.ext}`
const params = {
ACL: isPrivate ? "private" : "public-read",
Bucket: this.bucket_,
Body: pass,
Key: fileKey,
}
const s3 = new aws.S3()
return {
writeStream: pass,
promise: s3.upload(params).promise(),
url: `${this.spacesUrl_}/${fileKey}`,
fileKey,
}
}
async getDownloadStream(fileData) {
this.updateAwsConfig()
const s3 = new aws.S3()
const params = {
Bucket: this.bucket_,
Key: `${fileData.fileKey}`,
}
return s3.getObject(params).createReadStream()
}
async getPresignedDownloadUrl(fileData) {
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
)
}
}
export default DigitalOceanService

1309
yarn.lock

File diff suppressed because it is too large Load Diff