Skip to content

Commit

Permalink
feat(medusa-file-s3): Added S3 directory config (#5291)
Browse files Browse the repository at this point in the history
* Added S3 directory config

* Rename option directory to prefix

* Added changeset
  • Loading branch information
pepijn-vanvlaanderen authored Oct 11, 2023
1 parent 2c94186 commit bbd9dd4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-rockets-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"medusa-file-s3": minor
---

Added config to set S3 prefix
2 changes: 2 additions & 0 deletions packages/medusa-file-s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ 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:
Expand All @@ -47,6 +48,7 @@ const plugins = [
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,
Expand Down
22 changes: 12 additions & 10 deletions packages/medusa-file-s3/src/services/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ 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
import {
S3Client,
PutObjectCommand,
DeleteObjectCommand,
GetObjectCommand
} from "@aws-sdk/client-s3"
import { parse } from "path"
import { AbstractFileService, IFileService } from "@medusajs/medusa"
Expand All @@ -20,6 +20,7 @@ import {
import stream from "stream"

class S3Service extends AbstractFileService implements IFileService {
protected prefix_: string
protected bucket_: string
protected s3Url_: string
protected accessKeyId_: string
Expand All @@ -34,6 +35,7 @@ class S3Service extends AbstractFileService implements IFileService {
constructor({ logger }, options) {
super({}, options)

this.prefix_ = options.prefix ? `${options.prefix}/` : ''
this.bucket_ = options.bucket
this.s3Url_ = options.s3_url
this.accessKeyId_ = options.access_key_id
Expand All @@ -49,8 +51,8 @@ class S3Service extends AbstractFileService implements IFileService {
protected getClient(overwriteConfig: Partial<S3ClientConfigType> = {}) {
const config: S3ClientConfigType = {
credentials: {
accessKeyId: this.accessKeyId_,
secretAccessKey: this.secretAccessKey_,
accessKeyId: this.accessKeyId_,
secretAccessKey: this.secretAccessKey_,
},
region: this.region_,
...this.awsConfigObject_,
Expand Down Expand Up @@ -79,7 +81,7 @@ class S3Service extends AbstractFileService implements IFileService {

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({
ACL: options.acl ?? (options.isProtected ? "private" : "public-read"),
Expand All @@ -103,7 +105,7 @@ class S3Service extends AbstractFileService implements IFileService {
}

async delete(file: DeleteFileType): Promise<void> {
const command = new DeleteObjectCommand({
const command = new DeleteObjectCommand({
Bucket: this.bucket_,
Key: `${file.file_key}`,
})
Expand All @@ -120,7 +122,7 @@ class S3Service extends AbstractFileService implements IFileService {

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 = {
ACL: isPrivate ? "private" : "public-read",
Bucket: this.bucket_,
Expand Down

0 comments on commit bbd9dd4

Please sign in to comment.