Search code examples
amazon-web-servicesamazon-s3angular4-formss3-bucketuint8array

Get file from AWS-S3


I have files(PDF and Image) in S3 bucket, and I am trying to download using

bucket.getObject(params, function(err, data)

I am getting the data in Uint8Array, How can I get it as a file??

in AWS properties of that file:

Content-Disposition : inline
Content-Type : application/pdf

Code:

const params = {
  Bucket: 'mts-test-procedure-document',
  Key: 'Key'

};

bucket.getObject(params, function(err, data){
  if (err) {
    console.log('There was an error getting your file: ', err);
    return false;
  }

  console.log('Successfully got file.', data);
  // return true;
});  

Console:

Body: Uint8Array(94832) [37, 80, 68, 70, 45, 49, 46, 51, 13, 10, 37, 129, 150, 189, 221, 13, 10, 49, 32, 48, 32, 111, 98, 106, 13, 10, 60, 60, 32, 47, 84, 121, 112, 101, 32, 47, 67, 97, 116, 97, 108, 111, 103, 13, 10, 47, 80, 97, 103, 101, 115, 32, 50, 32, 48, 32, 82, 13, 10, 47, 79, 117, 116, 108, 105, 110, 101, 115, 32, 51, 32, 48, 32, 82, 13, 10, 62, 62, 13, 10, 101, 110, 100, 111, 98, 106, 13, 10, 52, 32, 48, 32, 111, 98, 106, 13, 10, 60, 60, 47, …]
ContentType: "application/pdf"
LastModified: Wed Sep 26 2018 15:34:59 GMT-0400 (Eastern Daylight Time) {}
Metadata: {}

Solution

  • You need to define while uploading your file . For image

        Content-Type: "image/jpeg"
    

    for jpeg images

    For pdf:

     Content-Type: "application/pdf"
     Content-Disposition: inline;
    

    More content -type can be found at https://s3browser.com/features-content-mime-types-editor.aspx