DEV Community

shivanimca10
shivanimca10

Posted on

Show Image from s3Bucket to html page

We are trying to display an image which exist in amazon s3 bucket in angular8. Our code is:-
In HTML:-

In ts:-

DownloadFromS3(FilePath, FileName) {
// Set up credentials
let AWSS3AccessKey = environment.AWSS3AccessKey
let AWSS3SecretKey = environment.AWSS3SecretKey
let S3Bucketname = environment.S3Bucketname
let S3BucketRootFolder = environment.S3BucketRootFolder

AWS.config.credentials = new AWS.Credentials({
  accessKeyId: AWSS3AccessKey, secretAccessKey: AWSS3SecretKey
});

const params = {
  Bucket: S3Bucketname,
  Key: S3BucketRootFolder + FilePath + "/" + FileName

};

let s3 = new AWS.S3();

let base64String = "";
Enter fullscreen mode Exit fullscreen mode

s3.getObject(params, function (err, data) {//unit8array
if (err) {
console.error(err); // an error occurred
}
else
{
//downloading the file inclient machine
const STRING_CHAR = String.fromCharCode.apply(null, data.Body);
base64String = btoa(STRING_CHAR);
return base64String;

  }
});
Enter fullscreen mode Exit fullscreen mode

}
//Calling Method
val= this.DownloadFromS3(this.imagePath, UploadedFileInfo[0].FileName);

But in s3.getObject function, "this" operator is not working like this.thumbnail is not defined here.

variable val is containing null even the following statement
return base64String;
containing the value.

Top comments (0)