2

I have images stored in Amazon S3. Image types include jpg, png and svg. I have a typescript node backend where I should retrieve an image from S3 bucket to be rotated with Sharp library. I'm using aws-sdk to get the object, but I'm having problems understanding what I can do to the S3 object to get it to a form that can be used with Sharp.

const getRequest: S3.GetObjectRequest = {
    Bucket: config.aws.bucketName,
    Key: uuid
};
  
const response = await s3client.getObject(getRequest).promise();
const macigalForm = response.Body as ???; // From documentation: Body = Readable | ReadableStream | Blob

sharp(magicalForm); // Accepts: Buffer  | Uint8Array  | Uint8ClampedArray  | Int8Array  | 
// Uint16Array  | Int16Array  | Uint32Array  | Int32Array  | Float32Array  | Float64Array  | string 

I would need help with understanding what would be the best type for "magicalForm" to be fed to sharp input.

Namiskuukkwl
  • 165
  • 1
  • 6
  • see: https://stackoverflow.com/questions/36942442/how-to-get-response-from-s3-getobject-in-node-js – Lawrence Cherone Jul 02 '22 at 14:45
  • I've taken a look on that, and I've tried some things suggested there, but I haven't managed to make any working solution. I'm failing to understand all the different typings here, hence I'm not able to apply what I'm reading to my specific use case. – Namiskuukkwl Jul 02 '22 at 15:56
  • The s3 Body type is `Buffer|Uint8Array|Blob|string|Readable` and sharp requires `Buffer|ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|string`. You can make a new type consisting just of the overlap, or for a quick hack just assign it to one of the types that exist in both. `const macigalForm = response.Body as Buffer;` – Geraint Anderson Apr 05 '23 at 13:48

0 Answers0