2017-03-08 106 views
0

我想通過身份驗證(通過JWT)GET請求返回存儲在Azure blob中的映像。當我在本地機器上運行項目並使用郵遞員來請求圖像並且請求經過並且我得到我要求的圖像時。但是,一旦我將代碼部署到Azure並擊中同一個端點,我就會得到一個403.代碼在我試圖調用DownloadToStreamAsync的那一行失敗。這裏是我使用的代碼:嘗試在Web API請求上檢索Azure blob時出現403錯誤

public async Task<BlobDownloadModel> DownloadBlob(Guid blobId) 
    { 
     try 
     { 
      //get picture record 
      Picture file = await _media.GetPictureAsync(blobId); 

      await _log.CreateLogEntryAsync("got picture record"); 

      // get string format blob name 
      var blobName = file.PictureId.ToString() + file.Extension; 

      await _log.CreateLogEntryAsync("got name of blob " + blobName); 

      if (!String.IsNullOrEmpty(blobName)) 
      { 
       await _log.CreateLogEntryAsync("blob not empty"); 

       var blob = _container.GetBlockBlobReference(blobName); 

       await _log.CreateLogEntryAsync("got blob: " + blob.ToString()); 

       var ms = new MemoryStream(); 

       await blob.DownloadToStreamAsync(ms); 

       await _log.CreateLogEntryAsync("blob downloaded to memory stream"); 

       var lastPos = blob.Name.LastIndexOf('/'); 
       var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); 

       var download = new BlobDownloadModel 
       { 
        BlobStream = ms, 
        BlobFileName = fileName, 
        BlobLength = blob.Properties.Length, 
        BlobContentType = blob.Properties.ContentType 
       }; 

       return download; 
      } 
     } 
     catch(Exception ex) 
     { 
      await _log.CreateLogEntryAsync("exception thrown: " + ex.ToString()); 
     } 

我將不勝感激任何幫助,我可以得到。

UPDATE:

我改變了我的代碼,這一點,並試圖再次:

public async Task<AzureBlobModel> DownloadBlob(Guid blobId) 
    { 
     try 
     { 
      //get picture record 
      Picture file = await _media.GetPictureAsync(blobId); 

      await _log.CreateLogEntryAsync("got picture record"); 

      // get string format blob name 
      var blobName = file.PictureId.ToString() + file.Extension; 

      await _log.CreateLogEntryAsync("got name of blob " + blobName); 

      if (!String.IsNullOrEmpty(blobName)) 
      { 
       await _log.CreateLogEntryAsync("blob not empty"); 

       var blob = _container.GetBlockBlobReference(blobName); 

       await _log.CreateLogEntryAsync("got blob: " + blob.ToString()); 

       // Strip off any folder structure so the file name is just the file name 
       var lastPos = blob.Name.LastIndexOf('/'); 
       var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); 

       await _log.CreateLogEntryAsync("got fileName: " + fileName); 

       //await blob.DownloadToStreamAsync(ms); 

       await _log.CreateLogEntryAsync("about to open read stream"); 

       var stream = await blob.OpenReadAsync(); 

       await _log.CreateLogEntryAsync("opened read stream"); 

       var result = new AzureBlobModel() 
       { 
        FileName = fileName, 
        FileSize = blob.Properties.Length, 
        Stream = stream, 
        ContentType = blob.Properties.ContentType 
       }; 

       await _log.CreateLogEntryAsync("blob downloaded to memory stream"); 

       return result; 

       // Build and return the download model with the blob stream and its relevant info 
       //var download = new BlobDownloadModel 
       //{ 
       // BlobStream = ms, 
       // BlobFileName = fileName, 
       // BlobLength = blob.Properties.Length, 
       // BlobContentType = blob.Properties.ContentType 
       //}; 

       //return download; 
      } 
     } 
     catch(Exception ex) 
     { 
      await _log.CreateLogEntryAsync("exception thrown: " + ex.ToString()); 
     } 

     await _log.CreateLogEntryAsync("returning null"); 

     // Otherwise 
     return null; 
    } 

從日誌中進行最後一次嘗試的結果是這樣的:

請求,接收並驗證,時間戳UTC:2017/3/10 5:28:26 - 5:28:26 AM
收到的ID:b3bc7faf-0c86-4ce2-af84-30636825a485 - 5:28:27 AM
得到的圖片記錄-5:28:27 AM
了Blob的名稱b3bc7faf-0c86-4ce2-af84-30636825a485.JPG - 上午05時28分27秒
一滴不爲空 - 上午05時28分27秒
了一滴:Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob - 上午05點28分27秒
了文件名:b3bc7faf-0c86-4ce2-af84-30636825a485.JPG - 上午05點28分27秒
即將開啓讀取流 - 5 :28:27上午

我能夠檢索文件/ blob的名稱,消除了錯誤的帳戶密鑰作爲問題的罪魁禍首。

SOLUTION

我能得到我的代碼用下面的代碼工作:

public async Task<AzureBlobModel> DownloadBlob(Guid blobId) 
    { 
     try 
     { 
      //get picture record 
      Picture file = await _media.GetPictureAsync(blobId); 

      // get string format blob name 
      var blobName = file.PictureId.ToString() + file.Extension; 

      if (!String.IsNullOrEmpty(blobName)) 
      { 
       var blob = _container.GetBlockBlobReference(blobName); 

       // Strip off any folder structure so the file name is just the file name 
       var lastPos = blob.Name.LastIndexOf('/'); 
       var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); 

       var fileLength = blob.Properties.Length; 
       var stream = await blob.OpenReadAsync(); 

       var result = new AzureBlobModel() 
       { 
        FileName = fileName, 
        FileSize = blob.Properties.Length, 
        Stream = stream, 
        ContentType = blob.Properties.ContentType 
       }; 

       return result; 
      } 
     } 
     catch(Exception ex) 
     { 
      await _log.CreateLogEntryAsync("exception thrown: " + ex.ToString()); 
     } 

     await _log.CreateLogEntryAsync("returning null"); 

     // Otherwise 
     return null; 
    } 

回答

0

我能夠解決通過使用下面的代碼的問題:

public async Task<AzureBlobModel> DownloadBlob(Guid blobId) 
    { 
     try 
     { 
      //get picture record 
      Picture file = await _media.GetPictureAsync(blobId); 

      // get string format blob name 
      var blobName = file.PictureId.ToString() + file.Extension; 

      if (!String.IsNullOrEmpty(blobName)) 
      { 
       var blob = _container.GetBlockBlobReference(blobName); 

       // Strip off any folder structure so the file name is just the file name 
       var lastPos = blob.Name.LastIndexOf('/'); 
       var fileName = blob.Name.Substring(lastPos + 1, blob.Name.Length - lastPos - 1); 

       var fileLength = blob.Properties.Length; 
       var stream = await blob.OpenReadAsync(); 

       var result = new AzureBlobModel() 
       { 
        FileName = fileName, 
        FileSize = blob.Properties.Length, 
        Stream = stream, 
        ContentType = blob.Properties.ContentType 
       }; 

       return result; 
      } 
     } 
     catch(Exception ex) 
     { 
      await _log.CreateLogEntryAsync("exception thrown: " + ex.ToString()); 
     } 

     await _log.CreateLogEntryAsync("returning null"); 

     // Otherwise 
     return null; 
    } 
0

但是,一旦我將代碼部署到Azure和打同樣的終點,我得到一個403.

首先,請檢查您的Azure帳戶和密鑰,以確保它們是正確的。其次,請檢查服務器上的時鐘。存儲服務確保請求到達服務時間不超過15分鐘。如果服務器上的時間不與存儲服務器同步,則會出現403錯誤。

+0

我雙檢查,以確保該帳戶的憑據是正確的。 Web服務器和blob存儲位於美國西海岸。我假設服務器時間是同步的。你知道我怎麼能檢查時間嗎? – chesco

+0

您是否使用存儲模擬器進行測試?如果您部署到Azure,請確保使用Azure存儲連接對其進行更改。請檢查帳戶密鑰以確保不會重新生成。檢查服務器時間很容易。請儘量在服務器上查看時間,以確保手錶的時間正確無誤。 –

+0

Jambor,我確定賬戶密鑰是正確的我通過在本地計算機上運行API進行測試,並且能夠從實時存儲帳戶中檢索圖像。我繼續並採取了請求數據的截圖https://24hr.cloudtimecards.com/File/Download/29021dd3-d1b8-4426-ab61-f061a340d855 此外,我無法檢查Azure存儲上的時間服務器。我沒有運氣看着Azure門戶。 – chesco

相關問題