需要帮助使用NodeJS从Azure Blob下载图像 [英] Need help downloading image from Azure Blobs using NodeJS

查看:180
本文介绍了需要帮助使用NodeJS从Azure Blob下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从azure的NodeJS Blob快速入门中获得了大部分代码,我能够成功上传包括图像的文件,并且可以在azure存储仪表板中很好地看到它们.但是我似乎无法下载它们或获取它们的URL,并且我需要数据库的URL,以便可以查询它并使用URL来检索文件.

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.

快速入门中代码的下载部分对我来说并不那么清楚,因为上载也似乎是针对文本的.如果我进入Azure存储仪表板,则可以看到该容器,并且可以看到使用该图像创建的Blob,并且可以单击该图像并将其加载到另一页中.但是,如果我转到属性并选择Uri: https://facerstorage.blob .core.windows.net/a00008/sun.png 并将其粘贴到我的浏览器中,我得到了:

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 :

我还打印出从blockBlobURL返回的url,它与上面的blob仪表板中的Uri中的URL相同,尽管它在a0009和sun.png之间有一个'/./',并被删除.例如: https://facerstorage.blob.core.windows .net/a00009/./sun.png ",并且得到相同的错误.

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.

不确定这是怎么回事

我使用了来自nodejs blobs快速启动的代码,用于下载的代码如下:

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

我没有BlockBlobURL.download函数的代码,也不知道什么:

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

在做.

我认为,从上面的那些URL中,我应该已经能够从该URL访问图像,但是我得到了上面显示的错误.不确切知道这两个还有什么作用.

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.

感谢您的帮助.

推荐答案

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

It sounds like you want to download or show the images stored in Azure Blob Storage, but the container of these images is not public. It's very similar with the SO thread Azure file image path using java.

解决方案是生成带有SAS签名的blob URL,以供下载或直接在浏览器中显示,您可以参考官方文档 Manage anonymous read access to containers and blobs 来了解这个概念

The solution is to generate a blob url with SAS signature for downloading or directly showing in the browser, you can refer to the offical document Using shared access signatures (SAS) and Manage anonymous read access to containers and blobs to know the concept.

以下是Node中用于生成带有SAS签名的blob URL的示例代码,该代码来自

Here is the sample code in Node for generating a blob url with SAS signature, which comes from here.

要创建共享访问签名(SAS),请使用generateSharedAccessSignature方法.此外,您可以使用日期帮助器功能轻松创建相对于当前时间某个时刻过期的SAS.

To create a Shared Access Signature (SAS), use the generateSharedAccessSignature method. Additionally you can use the date helper functions to easily create a SAS that expires at some point relative to the current time.

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

注意:上面的代码基于通过npm install azure-storage的npm软件包azure-storage v2,而不是通过npm i @azure/storage-blob的预览版本v10-Preview或遵循官方文档使用Java中的java 的Azure文件映像路径,其中的API与Node的API类似.

Note: The code above is based on npm package azure-storage v2 via npm install azure-storage, not the preview version v10-Preview via npm i @azure/storage-blob or follow the offical document Quickstart: Upload, download, list, and delete blobs using Azure Storage v10 SDK for JavaScript (preview) to install. For using v10 APIs to generating the SAS url, you can refer to my code for SO tread Azure file image path using java in Java which APIs are similar with these for Node.

或者只是用令牌连接普通的Blob网址,以获取如下所示的SAS网址,因此您只需生成一个令牌即可在任何Blob网址的末尾应用.

Or just to concat a normal blob url with token to get a url with SAS like below, so you can just generate a token to apply at the end of any blob url.

var sasUrl = blobUrl + token;

然后,您可以使用带有SAS的url通过<img src="<sas url>">在浏览器中显示,或者直接使用Http客户端直接下载它,而无需进行任何其他身份验证,直到到期为止.

Then, you can use the url with SAS to show in browser via <img src="<sas url>"> or directly download it using Http client without any additional authentication until the expiry time.

这篇关于需要帮助使用NodeJS从Azure Blob下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆