Skip to content

Issue Integrating @aws-sdk/client-s3 #21

Open
@salieflewis

Description

@salieflewis

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 });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions