oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.82k stars 2.56k forks source link

Error while uploading image to aws s3 #9452

Open FaizanAshiq opened 3 months ago

FaizanAshiq commented 3 months ago

What version of Bun is running?

1.0.31

What platform is your computer?

Linux 6.5.0-1014-aws x86_64 x86_64

What steps can reproduce the bug?

first of all my code for aws image upload is:

// -----------------------------------------------------------
//               imports
// -----------------------------------------------------------

const { promisify } = require('node:util');
const {
  S3Client,
  PutObjectCommand,
  DeleteObjectCommand,
} = require('@aws-sdk/client-s3');
const fs = require('node:fs');
const AppError = require('../utils/appError');
const readFile = promisify(fs.readFile);

// -----------------------------------------------------------
//               configurations
// -----------------------------------------------------------

const s3 = new S3Client({
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  },
  region: process.env.AWS_REGION,
});

// -----------------------------------------------------------
//               exports
// -----------------------------------------------------------

exports.uploadImage = async (path, name, next) => {
  const fileContent = await readFile(path);
  const params = {
    Body: fileContent,
    Bucket: process.env.AWS_BUCKET_NAME,
    ContentType: 'image/jpeg',
    Key: name,
  };

  // Uploading files to the bucket
  const command = new PutObjectCommand(params);
  console.log(command);
  const result = await s3.send(command);
  console.log('done');
  if (result.$metadata.httpStatusCode !== 200) {
    return next(new AppError('Image update failed', 500));
  }
  const url = `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${name}`;

  return url;
};

exports.deleteImage = async (Key) => {
  const params = {
    Bucket: process.env.AWS_BUCKET_NAME,
    Key,
  };

  const command = new DeleteObjectCommand(params);
  const result = await s3.send(command);
  return result;
};

I am running the server using two commands for testing. first command: node server.js second command: bun server.js with node server.js everything works fine. but when I run it with bun server.js it got stuck on line const result = await s3.send(command); when I send a request from Postman. I can see the command log but cannot see the done log.

moreover the logs for both cases are here.

logs when I run it with bun

_PutObjectCommand {
  middlewareStack: {
    add: [Function: add],
    addRelativeTo: [Function: addRelativeTo],
    clone: [Function: clone],
    use: [Function: use],
    remove: [Function: remove],
    removeByTag: [Function: removeByTag],
    concat: [Function: concat],
    applyToStack: [Function],
    identify: [Function: identify],
    identifyOnResolve: [Function: identifyOnResolve],
    resolve: [Function: resolve],
  },
  serialize: [Function: AsyncFunction],
  deserialize: [Function: AsyncFunction],
  input: {
    Body: Buffer(54815) [ 255, 216, 255, 219, 0, 67, 0, 6, 4, 5, 6, 5, 4, 6, 6, 5, 6, 7, 7, 6, 8, 10, 16, 10, 10, 9, 9, 10, 20, 14, 15, 12, 16, 23, 20, 24, 24, 23, 20, 22, 22, 26, 29, 37, 31, 26, 27, 35, 28, 22, 22, 32, 44, 32, 35, 38, 39, 41, 42, 41, 25, 31, 45, 48, 45, 40, 48, 37, 40, 41, 40, 255, 219, 0, 67, 1, 7, 7, 7, 10, 8, 10, 19, 10, 10, 19, 40, 26, 22, 26, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 255, 192, 0, 17, 8, 1, 244, 1, 202, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 29, 0, 0, 0, 7, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 1, 8, 9, 255, 196, 0, 74, 16, 0, 1, 3, 3, 3, 2, 4, 4, 3, 6, 2, 8, 4, 5, 4, 3, 1, 2, 3, 4, 0, 5, 17, 6, 18, 33, 49, 65, 19, 34, 81, 97, 7, 20, 113, 129, 50, 145, 161, 21, 35, 66, 82, 177, 193, 22, 209, 8, 36, 51, 98, 114, 130, 225, 240, 67, 146, 162, 241, 37, 38, 52, 68, 83, 23, 178, 194, 210, 54, 99, 115, 255, 196, 0, 27, 1, 0, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 1, 4, 5, 6, 7, 255, 196, 0, 59, 17, 0, 1, 4, 0, 4, 3, 6, 5, 2, 6, 1, 4, 3, 1, 0, 0, 1, 0, 2, 3, 17, 4, 18, 33, 49, 5, 65, 81, 19, 34, 97, 113, 145, 161, 50, 129, 177, 193, 240, 20, 209, 6, 35, 51, 66, 225, 241, 52, 82, 98, 146, 178, 36, 114, 130, 194, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 245, 5, 10, 20, 41, 138, 208, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 66, 133, 69, 16, 161, 92, 36, 14, 164, 14, 213, 218, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, 133, 10, 138, 33, 66, ... 54302 more ],
    Bucket: "muuve",
    ContentType: "image/jpeg",
    Key: "user-65f50764fdb6ce27458ea6fd.jpg",
  },
  resolveMiddleware: [Function: resolveMiddleware],
  resolveMiddlewareWithContext: [Function: resolveMiddlewareWithContext],
}

logs when I run it with node.

PutObjectCommand {
  middlewareStack: {
    add: [Function: add],
    addRelativeTo: [Function: addRelativeTo],
    clone: [Function: clone],
    use: [Function: use],
    remove: [Function: remove],
    removeByTag: [Function: removeByTag],
    concat: [Function: concat],
    applyToStack: [Function: cloneTo],
    identify: [Function: identify],
    identifyOnResolve: [Function: identifyOnResolve],
    resolve: [Function: resolve]
  },
  serialize: [AsyncFunction: se_PutObjectCommand],
  deserialize: [AsyncFunction: de_PutObjectCommand],
  input: {
    Body: <Buffer ff d8 ff db 00 43 00 06 04 05 06 05 04 06 06 05 06 07 07 06 08 0a 10 0a 0a 09 09 0a 14 0e 0f 0c 10 17 14 18 18 17 14 16 16 1a 1d 25 1f 1a 1b 23 1c 16 ... 54765 more bytes>,
    Bucket: 'muuve',
    ContentType: 'image/jpeg',
    Key: 'user-65f50764fdb6ce27458ea6fd.jpg'
  }
}

Additional information

You can see commands are different here. and the buffer is also different.

Jarred-Sumner commented 3 months ago

with node server.js everything works fine. but when I run it with bun server.js it got stuck on line const result = await s3.send(command); when I send a request from Postman. I can see the command log but cannot see the done log.

Is the uploadImage function being await'?

Otherwise, this mostly appears to be a formatting difference between Bun and Node for Buffer objects and potentially classes as well

Via @mangs in the discord: https://discord.com/channels/876711213126520882/1149339379446325248/1218435079802716221

mangs — Today at 10:46 PM those outputs are the same, convert decimal to hex and you get the same values https://www.binaryhexconverter.com/decimal-to-hex-converter

In the future please use code blocks on GitHub to format your code, it's difficult to see what you mean exactly

FaizanAshiq commented 3 months ago

Yes, I am awaiting the uploadImage function. the same code is working when I run with node but stuck on const result = await s3.send(command); when I run this using Bun here is my code.

// -----------------------------------------------------------
//               imports
// -----------------------------------------------------------

const sharp = require('sharp');
const {
  S3Client,
  PutObjectCommand,
  DeleteObjectCommand,
} = require('@aws-sdk/client-s3');
const AppError = require('../utils/appError');

// -----------------------------------------------------------
//               configurations
// -----------------------------------------------------------

const s3 = new S3Client({
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  },
  region: process.env.AWS_REGION,
});

// -----------------------------------------------------------
//               exports
// -----------------------------------------------------------

exports.uploadImage = async (path, name, next) => {
  const fileBuffer = await sharp(path).toBuffer();
  const params = {
    Body: fileBuffer,
    Bucket: process.env.AWS_BUCKET_NAME,
    ContentType: 'image/jpeg',
    Key: name,
  };

  // Uploading files to the bucket
  const command = new PutObjectCommand(params);
  const result = await s3.send(command);
  if (result.$metadata.httpStatusCode !== 200) {
    return next(new AppError('Image update failed', 500));
  }
  const url = `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${name}`;

  return url;
};

exports.deleteImage = async (Key) => {
  const params = {
    Bucket: process.env.AWS_BUCKET_NAME,
    Key,
  };

  const command = new DeleteObjectCommand(params);
  const result = await s3.send(command);
  return result;
};
FaizanAshiq commented 3 months ago

@Jarred-Sumner I am still struggling to figure out what's wrong. The same code works for node but not for bun.

a few screenshots to understand

code

postman

node

phongsathornpt commented 2 months ago

@Jarred-Sumner I am still struggling to figure out what's wrong. The same code works for node but not for bun.

a few screenshots to understand

code

postman

node

same problem

davidtranjs commented 1 month ago

Same issue +1