feat(medusa-file-s3): Added S3 directory config (#5291)
* Added S3 directory config * Rename option directory to prefix * Added changeset
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"medusa-file-s3": minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Added config to set S3 prefix
|
||||||
@@ -35,6 +35,7 @@ S3_BUCKET=<YOUR_BUCKET_NAME>
|
|||||||
S3_REGION=<YOUR_BUCKET_REGION>
|
S3_REGION=<YOUR_BUCKET_REGION>
|
||||||
S3_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
|
S3_ACCESS_KEY_ID=<YOUR_ACCESS_KEY_ID>
|
||||||
S3_SECRET_ACCESS_KEY=<YOUR_SECRET_ACCESS_KEY>
|
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:
|
3\. In `medusa-config.js` add the following at the end of the `plugins` array:
|
||||||
@@ -47,6 +48,7 @@ const plugins = [
|
|||||||
options: {
|
options: {
|
||||||
s3_url: process.env.S3_URL,
|
s3_url: process.env.S3_URL,
|
||||||
bucket: process.env.S3_BUCKET,
|
bucket: process.env.S3_BUCKET,
|
||||||
|
prefix: process.env.S3_PREFIX, // optional
|
||||||
region: process.env.S3_REGION,
|
region: process.env.S3_REGION,
|
||||||
access_key_id: process.env.S3_ACCESS_KEY_ID,
|
access_key_id: process.env.S3_ACCESS_KEY_ID,
|
||||||
secret_access_key: process.env.S3_SECRET_ACCESS_KEY,
|
secret_access_key: process.env.S3_SECRET_ACCESS_KEY,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
import stream from "stream"
|
import stream from "stream"
|
||||||
|
|
||||||
class S3Service extends AbstractFileService implements IFileService {
|
class S3Service extends AbstractFileService implements IFileService {
|
||||||
|
protected prefix_: string
|
||||||
protected bucket_: string
|
protected bucket_: string
|
||||||
protected s3Url_: string
|
protected s3Url_: string
|
||||||
protected accessKeyId_: string
|
protected accessKeyId_: string
|
||||||
@@ -34,6 +35,7 @@ class S3Service extends AbstractFileService implements IFileService {
|
|||||||
constructor({ logger }, options) {
|
constructor({ logger }, options) {
|
||||||
super({}, options)
|
super({}, options)
|
||||||
|
|
||||||
|
this.prefix_ = options.prefix ? `${options.prefix}/` : ''
|
||||||
this.bucket_ = options.bucket
|
this.bucket_ = options.bucket
|
||||||
this.s3Url_ = options.s3_url
|
this.s3Url_ = options.s3_url
|
||||||
this.accessKeyId_ = options.access_key_id
|
this.accessKeyId_ = options.access_key_id
|
||||||
@@ -79,7 +81,7 @@ class S3Service extends AbstractFileService implements IFileService {
|
|||||||
|
|
||||||
const parsedFilename = parse(file.originalname)
|
const parsedFilename = parse(file.originalname)
|
||||||
|
|
||||||
const fileKey = `${parsedFilename.name}-${Date.now()}${parsedFilename.ext}`
|
const fileKey = `${this.prefix_}${parsedFilename.name}-${Date.now()}${parsedFilename.ext}`
|
||||||
|
|
||||||
const command = new PutObjectCommand({
|
const command = new PutObjectCommand({
|
||||||
ACL: options.acl ?? (options.isProtected ? "private" : "public-read"),
|
ACL: options.acl ?? (options.isProtected ? "private" : "public-read"),
|
||||||
@@ -120,7 +122,7 @@ class S3Service extends AbstractFileService implements IFileService {
|
|||||||
|
|
||||||
const isPrivate = fileData.isPrivate ?? true // default to private
|
const isPrivate = fileData.isPrivate ?? true // default to private
|
||||||
|
|
||||||
const fileKey = `${fileData.name}.${fileData.ext}`
|
const fileKey = `${this.prefix_}${fileData.name}.${fileData.ext}`
|
||||||
const params: PutObjectCommandInput = {
|
const params: PutObjectCommandInput = {
|
||||||
ACL: isPrivate ? "private" : "public-read",
|
ACL: isPrivate ? "private" : "public-read",
|
||||||
Bucket: this.bucket_,
|
Bucket: this.bucket_,
|
||||||
|
|||||||
Reference in New Issue
Block a user