I want to fetch image from azure blob storage and send it over to client without saving it locally. I am able to get image from blob storage and save it to local file but struggling to send it to client without saving locally. Please find code below
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlobClient(blobName);
const downloadBlockBlobResponse = await blockBlobClient.download(0);
console.log('\nDownloaded blob content...');
let f = await streamToString(downloadBlockBlobResponse.readableStreamBody)
reply.type('image/jpg').send(f)
streamToString function is as follows
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}
I get blank screen in browser when I run this code
If you want to fetch an image from azure blob storage and send it over to the client without saving it locally, the Node server sending a SAS token to the client and the client get this image from Azure storage directly would be a better solution I think. It also eases the pressure of the Node server: generate a SAS token and send it to the client is ok, no need to read data from Storage.
Try the code below to generate a SAS token:
Result:
Client-side could use this image URL to access this image directly from storage:
Try this to convert imageURL to base64 image content so that you can save/show images directly based on their content:
Result:
Details of tag: