type here

In my application i've different end point where i get files from "form-data" with fastify multipart library. Then turn they into a buffer and upload in s3. All works fine except for the images. With images i get a larger corrupted file on s3.

This is what i do for now:

  • Get the file from the request
async upload(
    @Req() req: FastifyRequest,
    @Res() res: FastifyReply<never>,
    @Param() params: { entityId: string },
  ) {
    const image: MultipartFile = await req.file();
    await this.commandBus.execute(
      new UploadImageCommand(params.entityId, image),
    );
    return res
      .code(204)
      .send({ message: 'Image uploaded', filename: image.filename });
  }
  • Get a buffer stream from the file and upload to s3 using my service
const imageKey = this.createUniqueName(image.filename.replace(' ', '%'));
    const storageKey = `${entityId}/${imageKey}`;

    await this.storageService.uploadFile(
      await image.toBuffer(),
      storageKey,
      this.configService.get('IMG_BUCKET'),
      image.mimetype,
    );
async uploadFile(
    data: Buffer,
    storageKey: string,
    bucketName: string,
    type: string,
  ): Promise<void> {
    await this.s3Client.send(
      new PutObjectCommand({
        Bucket: bucketName,
        Key: storageKey,
        Body: data,
        ContentType: type,
      }),
    );
  }

I've already added the desired binaryMediaTypes to my ApiGateway.

Can someone help me with this?

I've already added correct binaryMediaTypes to my ApiGateway and i've already tried adding the ContentEncoding type like this:

async uploadFile(
    data: Buffer,
    storageKey: string,
    bucketName: string,
    type: string,
    encoding: string
  ): Promise<void> {
    await this.s3Client.send(
      new PutObjectCommand({
        Bucket: bucketName,
        Key: storageKey,
        Body: data,
        ContentType: type,
        ContentEncoding: encoding, //here i've tride with files encoded in 7bit and base64
      }),
    );
  }
0

There are 0 best solutions below