Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
salieflewis opened this issue Nov 20, 2024 · 0 comments
Open

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

salieflewis opened this issue Nov 20, 2024 · 0 comments

Comments

@salieflewis
Copy link

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 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant