0

I have a file on an S3 bucket - I am able to query the bucket, so I know the credentials and parameters for the bucket are correct. I have the following code (from the AWS examples) :

      const run = async () => {
        try {
          // Get the object} from the Amazon S3 bucket. It is returned as a ReadableStream.
          const data = await s3Client.send(new GetObjectCommand(bucketParamsGet));
          // Convert the ReadableStream to a string.
          const fileInfo = await data.Body?.transformToString()
          // return await data.Body.transformToWebStream()
          // console.log(data)
          const filename = data.Body.req.path
          console.log('>>> ',filename)
          await fs.writeFile('./testfile', fileInfo, (err) => {
            if (err) 
              console.log(err)
            else {
              console.log('tada')
            }
          });
          return fileInfo
        } catch (err) {
          console.log("Error", err);
        }
      };
      
      run();

I added the writeFile to see if I could just simply write the file to disc, what I got was a file much larger than what was uploaded. I think my issue is around the fact that the file is being saved as a string - however, I can't see how to save this as a jpg (or whatever the original file was)

Joseph
  • 541
  • 1
  • 4
  • 31
  • You stated that you got this example from the AWS examples. Can you post URL. – smac2020 Oct 31 '22 at 13:25
  • You have a read stream so create a write stream using fs.createWriteStream and pipe the read stream to the write stream e.g. `readStream.pipe(writeStream)` – jarmod Oct 31 '22 at 13:25
  • Example is from: https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/s3/src/s3_getobject.js – Joseph Oct 31 '22 at 13:31
  • @jarmod - as my readStread is called data - do you mean do data.pipe(writestream) – Joseph Oct 31 '22 at 13:32

0 Answers0