【问题标题】:Unable to download blob from my container as shown in the @azure/storage-blob documentation无法从我的容器中下载 blob,如 @azure/storage-blob 文档中所示
【发布时间】:2020-04-11 11:45:20
【问题描述】:

我无法从 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 时它发回的参考号

【问题讨论】:

  • 请编辑您的问题并包含您收到的任何错误消息。
  • 能否请您检查 ref 是否是您的 blob 名称并提供错误信息?
  • @JimXu UnhandledPromiseRejectionWarning:错误:意外状态代码:404 at new RestError (D:\wd\javascript\Projects\blue - secure\node_modules\@azure\core-http\dist\coreHttp.node .js:1716:28) 在 D:\wd\javascript\Projects\blue - secure\node_modules\@azure\core-http\dist\coreHttp.node.js:3416:37 在 process._tickCallback (internal/process/ next_tick.js:68:7) (node:9396) UnhandledPromiseRejectionWarning: 未处理的承诺拒绝。
  • @AmaanItiyaz 根据错误,您的 blob 可能不存在。请检查您是否提供了正确的 blob 名称。
  • @Jim Xu ,我现在正在获取流,但它是在 readableStreamBody 中加密的。有什么方法可以解密吗

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


【解决方案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);
  });


更新

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

【讨论】:

    猜你喜欢
    • 2018-04-13
    • 2022-11-02
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2016-09-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多