import { S3Client } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';

const s3Client = new S3Client({
  endpoint: process.env.S3_ENDPOINT,
  region: process.env.S3_REGION || 'us-east-1',
  credentials: {
    accessKeyId: process.env.S3_ACCESS_KEY,
    secretAccessKey: process.env.S3_SECRET_KEY,
  },
  forcePathStyle: true,
});

/**
 * Create an upload stream for streaming data to S3
 * @param {string} bucket - S3 bucket name
 * @param {string} key - S3 object key
 * @param {Stream} body - ReadableStream to upload
 * @returns {Promise} Upload promise
 */
export function createUploadStream(bucket, key, body) {
  return new Upload({
    client: s3Client,
    params: {
      Bucket: bucket,
      Key: key,
      Body: body,
    },
  }).done();
}

export default s3Client;
