无法从容器中下载blob,如@ azure / storage-blob文档中所示

时间:2019-12-18 21:27:40

标签: node.js azure express azure-storage azure-storage-blobs

我无法从azure存储帐户下载blob。我完全按照那里官方的指示做 文档here

  

文档代码

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
  );
  

我的代码

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)
    }
}

我已经传递了Blob的名称以及保存任何Blob时发送回的参考号

1 个答案:

答案 0 :(得分:1)

根据我的理解,您想获取blob内容。请参考以下代码。此外,请注意,您需要提供正确的Blob名称。

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);
  });

enter image description here


更新

如果要下载图片,可以使用方法dowlodToFile。有关更多详细信息,请参阅https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/blobclient?view=azure-node-latest#downloadtofile-string--undefined---number--undefined---number--blobdownloadoptions-