image-upload-example icon indicating copy to clipboard operation
image-upload-example copied to clipboard

Issue Integrating `@aws-sdk/client-s3`

Open salieflewis opened this issue 1 year ago • 0 comments

I'm trying to use this example to integrate with the AWS S3 SDK. I keep running into this error regardless of the command that I use.

TypeError: (0 , import_slurpFile.slurpFile) is not a function

Has anyone attempted this thus far? Here is a preview of what my route looks like.

import {
  PutObjectCommand,
  S3Client,
  S3ServiceException,
} from '@aws-sdk/client-s3';

const client = new S3Client({
  region: 'us-east-1',
  credentials: {
    accessKeyId: 'random-key',
    secretAccessKey: 'random-secret',
  },
});

export async function POST(req: Request) {
  const formData = await req.formData();

  for (const key of formData.keys()) {
    if (key.startsWith('photo')) {
      const file = formData.get(key);
      if (file && typeof file === 'object') {
        console.log('file', file.name, file.size);
        const buffer = Buffer.from(await file.arrayBuffer());

        console.log('buffer', buffer);

        const command = new PutObjectCommand({
          Bucket: 'development',
          Key: `media/${file.name ?? 'photo.png'}`, // Use the file name as the key
          Body: buffer,
        });

        try {
          console.log('getting ready to upload');
          const response = await client.send(command);
          console.log(response);
        } catch (caught) {
          if (caught instanceof S3ServiceException) {
            console.error(
              `Error from S3 while uploading object to development. ${caught.name}: ${caught.message}`,
            );
          } else {
            throw caught;
          }
        }
      }
    }
  }

  return Response.json({ success: true });
}

salieflewis avatar Nov 20 '24 19:11 salieflewis