Reputation: 293
I am unable to download blob from azure storage account. I have done exactly as shown in there official documentation here.
DOCUMENTATION CODE
const { BlobServiceClient } = require("@azure/storage-blob");
const account = "<account name>";
const sas = "<service Shared Access Signature Token>";
const containerName = "<container name>";
const blobName = "<blob name>"
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net${sas}`
);
async function main() {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
// Get blob content from position 0 to the end
// In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
const downloadBlockBlobResponse = await blobClient.download();
const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
console.log(
"Downloaded blob content",
downloaded
);
My Code
exports.downloadBlob = async (req, res, next)=>{
const ref = req.params.id;
try{
const containerClient = blobServiceClient.getContainerClient('xxxxxxxxx');
const blobClient = containerClient.getBlobClient(ref);
// Get blob content from position 0 to the end
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
const downloadBlockBlobResponse = await blobClient.download();
const downloaded = await streamToString(downloadBlockBlobResponse.readableStreamBody);
console.log("Downloaded blob content:", downloaded);
return res.send(downloaded)
}
catch(err){
return res.send(err)
}
}
I have passed the name of the blob as well the reference number it sends back when we save any blob
Upvotes: 0
Views: 4094
Reputation: 23161
According to my understanding, you want to get the blob content. Please refer to the following code. Besides,please note that you need to provide the right blob name.
var storage = require("@azure/storage-blob")
async function readBlob() {
const accountname ="";
const key = "";
const cerds = new storage.StorageSharedKeyCredential(accountname,key);
const blobServiceClient =new storage.BlobServiceClient( `https://${accountname}.blob.core.windows.net`,cerds)
const containerName="test";
var client =blobServiceClient.getContainerClient(containerName)
// download blob(read)
const blobClient = client.getBlobClient("test.json");
const downloadBlockBlobResponse = await blobClient.download(0);
console.log(
"Downloaded blob content",
await streamToString(downloadBlockBlobResponse.readableStreamBody)
);
}
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);
});
}
readBlob()
.then(() => {
console.log("Successfully executed sample.");
})
.catch((err) => {
console.log(err.message);
});
If you want to download the picture, you can use the method dowlodToFile
. For more details, please refer to https://learn.microsoft.com/en-us/javascript/api/@azure/storage-blob/blobclient?view=azure-node-latest#downloadtofile-string--undefined---number--undefined---number--blobdownloadoptions-.
Upvotes: 1