在外部系统+ REST API中显示来自Azure的文件服务映像 [英] Displaying Images of File Service from Azure in external system + REST API

查看:36
本文介绍了在外部系统+ REST API中显示来自Azure的文件服务映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用azure的GETFILE()服务创建了一个方法.参考: https://docs.microsoft.com/zh-我们/rest/api/storageservices/获取文件

I have created a method using GETFILE() service of azure. Reference: https://docs.microsoft.com/en-us/rest/api/storageservices/get-file

  public void getImage(){
        string storageKey = 'xxxxStorageKeyxxx';
        string storageName = '<storageName>';
        Datetime dt = Datetime.now();
        string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss')+ ' GMT';
        string CanonicalizedHeaders = 'x-ms-date:'+formattedDate+'\nx-ms-version:2016-05-31';
        string CanonicalizedResource = '/' + storageName + '/<shareName>/<dirName>/<File Name>\ntimeout:20';
        string StringToSign = 'GET\n\n\n\n\napplication/octet-stream\n\n\n\n\n\n\n' + CanonicalizedHeaders+'\n'+CanonicalizedResource;

        Blob temp = EncodingUtil.base64Decode(storageKey);
        Blob hmac = Crypto.generateMac('HmacSHA256',Blob.valueOf(StringToSign),temp ); //StringToSign
        system.debug('oo-'+EncodingUtil.base64Encode(hmac));
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setHeader('x-ms-version','2016-05-31' );
        req.setHeader('x-ms-date', formattedDate);
        req.setHeader('content-type','application/octet-stream');
        string signature = EncodingUtil.base64Encode(hmac);
        string authHeader =  'SharedKey <storageName>'+':'+signature;

        req.setHeader('Authorization',authHeader);
        req.setEndpoint('https://<storageName>.file.core.windows.net/<shareName>/<dirName>/<file Name>?timeout=20');

        Http http = new Http();
        HTTPResponse res;
        res = http.send(req);                
    }

上述工作正常,并给出200作为响应代码.但是,我的主要目标是显示/下载我通过REST API检索到的图像.我该如何实现?

The above was working fine and giving the 200 as response code. But, my main goal is to display/download the respective image which i retrieved through REST API. How can i achieve that?

推荐答案

在回答您的问题之前有几件事:

So a few things before I answer your question:

  • 文件存储并不真正适合您要完成的任务(尽管有可能).
  • 您应该查看Blob存储,因为Blob存储更适合这种情况.

假设您使用Blob存储,可以做一些事情:

Assuming you go with Blob storage, there are a few things you could do:

  • 如果Blob容器(相当于文件存储中的共享)的ACL为 Blob Container (即容器中的Blob是公开可用的),则可以只需在响应中返回Blob的URL(与上面代码中的请求URL相同),然后在应用程序中创建一个将href设置为该URL的链接.
  • 如果Blob容器的ACL为 Private (即Blob不公开),则需要在该ACL上创建一个 Shared Access Signature(SAS)令牌具有至少 Read 权限的Blob,然后创建SAS URL.SAS URL只是 blob URL + SAS令牌,然后在响应中返回此SAS URL,然后在应用程序中创建一个将href设置为该URL的链接.
  • If the blob container (equivalent to a share in file storage) has an ACL is Blob or Container (i.e. blobs in a container are publicly available), you could simply return the blob's URL (Same is your request URL in code above) in your response and then create a link in your application with href set to this URL.
  • If the blob container has an ACL as Private (i.e. blobs are not publicly available), you would need to create a Shared Access Signature (SAS) token on that blob with at least Read permission and then create a SAS URL. A SAS URL is simply blob URL + SAS token and return this SAS URL in your response and then create a link in your application with href set to this URL.

由于Azure文件共享始终是私有的,因此,如果要使用Azure文件服务来提供文件,则将执行与上面列出的第二个选项相同的操作.您将至少具有 Read 权限在文件上创建SAS令牌,然后在响应中返回SAS URL,然后在应用程序中创建一个将href设置为该URL的链接.

Since an Azure File Share is always private, if you were to use Azure File service to serve a file, you would do the same thing as 2nd option I listed above. You will create a SAS token on the file with at least Read permission and then return the SAS URL in the response and then create a link in your application with href set to this URL.

要了解有关共享访问签名的信息,您可能会发现此链接很有用:

To read about Shared Access Signature, you may find this link helpful: https://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1.

要使用REST API创建共享访问签名,您可能会发现此链接很有用:

To create a Shared Access Signature using REST API, you may find this link helpful: https://docs.microsoft.com/en-us/rest/api/storageservices/Constructing-a-Service-SAS?redirectedfrom=MSDN

这篇关于在外部系统+ REST API中显示来自Azure的文件服务映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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