I am fetching a base64 image from my s3 bucket using the following code.The first fetch method returns an s3 presgined url. The fetch the presigned url and convert response to blob. When I check the response type it is an octet-stream. My question is how can I convert it to base64 image and display it in the img tag
const profilePicFetchResult = await fetch('http://localhost/getUrl', {
method: "POST",
body: key,
headers: {
'Content-Type': 'application/json'
}
})
const result = await profilePicFetchResult.text()
const fetched = await fetch(result, {
method: "GET",
mode: "cors",
headers: {
'Access-Control-Allow-Origin': '*',
},
})
const imageBlob = await fetched.blob();
The imageBlob variable is equal
Blob {size: 66026, type: "binary/octet-stream"}
size: 66026
type: "binary/octet-stream"
__proto__: Blob
The img tag html
<img id="profile-image" src="https://trainerprofilepictures.s3.me-south-1.amazonaws.com/5e90603e7f0d251cab9253c6/1d70a21e-7c0e-4f12-92ae-84b3c85ecf1e" alt="">
The src is loading because the s3 bucket is private. I have to fetch it using presigned urls
The backend code is
function readObjectSignedUrl(req) {
return new Promise(function (resolve, reject) {
s3.getSignedUrl('getObject', {
Bucket: "pictures",
Key: req,
Expires: 6000
}, (err, url) => {
if (err) {
reject(err);
}
else {
resolve(url);
}
})
})
}
The router file
router.post('/getUrl',routerFunction.routerStatusFunction,trainerController.getObject)