Need help downloading image from Azure Blobs using NodeJS

Jac*_*ck 4 azure-storage azure-storage-blobs node.js azure-blob-storage

I got most of the code from the NodeJS Blob quickstart from azure, I am able to upload files including images successfully and I can see them in the azure storage dashboard just fine. But I can't seem to download them or get a URL to them and I need the url for my database so I can query it and use the url to retrieve the file.

The download part of the code in the quickstart isnt so clear to me, it seems to be made for text as the upload is as well. If I go into my azure storage dashboard I can see the container and I can see the blob created with the image and I can click on the image and it loads in another page. However if I go to properties and select the Uri: https://facerstorage.blob.core.windows.net/a00008/sun.png and paste it into my browser I get :
在此处输入图片说明

And I also print out the url returned from the blockBlobURL and it is the same as the one in the Uri in the blobs dashboard above, althought it has a '/./' between a0009 and sun.png which gets removed by the browser for example: https://facerstorage.blob.core.windows.net/a00009/./sun.png" its the same and I get the same error.

Not sure what is wrong here

I used the code from the nodejs blobs quickstart the code for the download is as follows:

const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, content);
console.log("The blobs URL is: " + JSON.stringify(blockBlobURL.url));
const downloadResponse = await blockBlobURL.download(aborter, 0);
downloadedContent = downloadResponse.readableStreamBody.read(content.length)//.toString();
console.log(`Downloaded blob content: "${downloadedContent}"`);
Run Code Online (Sandbox Code Playgroud)

I dont have the code for the BlockBlobURL.download function, nor do I understand what:

  const downloadResponse = await blockBlobURL.download(aborter, 0);
    downloadedContent = downloadResponse.readableStreamBody.read(content.length)//.toString();
Run Code Online (Sandbox Code Playgroud)

is doing.

I think that from the url above those I should already be able to access the image from that url but I get the errors as shown above. Don't know exactly what else these 2 do.

Thanks for any help.

Pet*_*Pan 5

听起来您想下载或显示存储在 Azure Blob 存储中的图像,但这些图像的容器不是公开的。它与使用 java的 SO 线程Azure 文件图像路径非常相似。

该解决方案是产生SAS签名BLOB网址下载或在浏览器中直接显示,你可以参考官方文档Using shared access signatures (SAS)Manage anonymous read access to containers and blobs了解的概念。

这是 Node 中生成带有 SAS 签名的 blob url 的示例代码,该代码来自此处

要创建共享访问签名 (SAS),请使用 generateSharedAccessSignature 方法。此外,您可以使用日期辅助函数轻松创建一个相对于当前时间在某个时间点到期的 SAS。

var azure = require('azure-storage');
var blobService = azure.createBlobService();

var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);

var sharedAccessPolicy = {
  AccessPolicy: {
    Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
    Start: startDate,
    Expiry: expiryDate
  }
};

var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
var sasUrl = blobService.getUrl(containerName, blobName, token);
Run Code Online (Sandbox Code Playgroud)

注意:以上代码是基于npm package azure-storagev2 via npm install azure-storage,而非预览版v10-Previewvianpm i @azure/storage-blob或按照官方文档Quickstart: Upload, download, list, and delete blobs using Azure Storage v10 SDK for JavaScript (preview)安装。对于使用v10API 生成 SAS url,您可以参考我的代码,使用 Java中的 Java执行 SO 步Azure 文件图像路径,这些 API 与 Node.js 的 API 类似。

或者只是将一个普通的 blob url 与令牌连接起来,以获得一个带有 SAS 的 url,如下所示,这样你就可以生成一个令牌以在任何 blob url 的末尾应用。

var sasUrl = blobUrl + token;
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用带有 SAS 的 url 通过浏览器显示<img src="<sas url>">或使用 Http 客户端直接下载它,而无需任何额外的身份验证,直到到期时间。