From bbd9dd408f04dc95eb7d2d57984fb61e5c015bbc Mon Sep 17 00:00:00 2001 From: pepijn-vanvlaanderen Date: Wed, 11 Oct 2023 19:32:35 +0200 Subject: [PATCH] feat(medusa-file-s3): Added S3 directory config (#5291) * Added S3 directory config * Rename option directory to prefix * Added changeset --- .changeset/sour-rockets-bathe.md | 5 +++++ packages/medusa-file-s3/README.md | 2 ++ packages/medusa-file-s3/src/services/s3.ts | 22 ++++++++++++---------- 3 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 .changeset/sour-rockets-bathe.md diff --git a/.changeset/sour-rockets-bathe.md b/.changeset/sour-rockets-bathe.md new file mode 100644 index 0000000000000..5f3d907d1f88d --- /dev/null +++ b/.changeset/sour-rockets-bathe.md @@ -0,0 +1,5 @@ +--- +"medusa-file-s3": minor +--- + +Added config to set S3 prefix diff --git a/packages/medusa-file-s3/README.md b/packages/medusa-file-s3/README.md index a5b13ea12f8d8..1c6aa9666a904 100644 --- a/packages/medusa-file-s3/README.md +++ b/packages/medusa-file-s3/README.md @@ -35,6 +35,7 @@ S3_BUCKET= S3_REGION= S3_ACCESS_KEY_ID= S3_SECRET_ACCESS_KEY= +S3_PREFIX= (optional) ``` 3\. In `medusa-config.js` add the following at the end of the `plugins` array: @@ -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, diff --git a/packages/medusa-file-s3/src/services/s3.ts b/packages/medusa-file-s3/src/services/s3.ts index 053cbb3e41196..0ac60d5facdd3 100644 --- a/packages/medusa-file-s3/src/services/s3.ts +++ b/packages/medusa-file-s3/src/services/s3.ts @@ -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" @@ -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 @@ -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 @@ -49,8 +51,8 @@ class S3Service extends AbstractFileService implements IFileService { protected getClient(overwriteConfig: Partial = {}) { const config: S3ClientConfigType = { credentials: { - accessKeyId: this.accessKeyId_, - secretAccessKey: this.secretAccessKey_, + accessKeyId: this.accessKeyId_, + secretAccessKey: this.secretAccessKey_, }, region: this.region_, ...this.awsConfigObject_, @@ -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"), @@ -103,7 +105,7 @@ class S3Service extends AbstractFileService implements IFileService { } async delete(file: DeleteFileType): Promise { - const command = new DeleteObjectCommand({ + const command = new DeleteObjectCommand({ Bucket: this.bucket_, Key: `${file.file_key}`, }) @@ -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_,