Displaying Images of File Service from Azure in ex

2019-08-22 22:34发布

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

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?

标签: rest azure
1条回答
啃猪蹄的小仙女
2楼-- · 2019-08-22 23:20

So a few things before I answer your question:

  • File storage is not really suitable for what you're trying to accomplish (it's possible though).
  • You should look at Blob storage for this as blob storage is more suitable for this kind of scenario.

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

  • 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.

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.

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

查看更多
登录 后发表回答