Phase 2: services/mam-api/src/s3/client.js

This commit is contained in:
Zac Gaetano 2026-04-07 22:05:40 -04:00
parent cc06166e00
commit a2c233aed3

View file

@ -2,6 +2,8 @@ import { S3Client, GetObjectCommand, DeleteObjectCommand } from '@aws-sdk/client
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { Upload } from '@aws-sdk/lib-storage'; import { Upload } from '@aws-sdk/lib-storage';
export const S3_BUCKET = process.env.S3_BUCKET || 'mam';
const s3Client = new S3Client({ const s3Client = new S3Client({
region: process.env.S3_REGION || 'us-east-1', region: process.env.S3_REGION || 'us-east-1',
endpoint: process.env.S3_ENDPOINT, endpoint: process.env.S3_ENDPOINT,
@ -9,6 +11,7 @@ const s3Client = new S3Client({
accessKeyId: process.env.S3_ACCESS_KEY, accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY, secretAccessKey: process.env.S3_SECRET_KEY,
}, },
forcePathStyle: true,
}); });
export { s3Client }; export { s3Client };
@ -16,7 +19,7 @@ export { s3Client };
export const getSignedUrlForObject = async (key, expiresIn = 3600) => { export const getSignedUrlForObject = async (key, expiresIn = 3600) => {
try { try {
const command = new GetObjectCommand({ const command = new GetObjectCommand({
Bucket: process.env.S3_BUCKET, Bucket: S3_BUCKET,
Key: key, Key: key,
}); });
const url = await getSignedUrl(s3Client, command, { expiresIn }); const url = await getSignedUrl(s3Client, command, { expiresIn });
@ -32,7 +35,7 @@ export const uploadStream = async (key, stream, contentType) => {
const upload = new Upload({ const upload = new Upload({
client: s3Client, client: s3Client,
params: { params: {
Bucket: process.env.S3_BUCKET, Bucket: S3_BUCKET,
Key: key, Key: key,
Body: stream, Body: stream,
ContentType: contentType, ContentType: contentType,
@ -50,7 +53,7 @@ export const uploadStream = async (key, stream, contentType) => {
export const deleteObject = async (key) => { export const deleteObject = async (key) => {
try { try {
const command = new DeleteObjectCommand({ const command = new DeleteObjectCommand({
Bucket: process.env.S3_BUCKET, Bucket: S3_BUCKET,
Key: key, Key: key,
}); });
const result = await s3Client.send(command); const result = await s3Client.send(command);