[英]Getting 403 error when trying to retrieve an Azure blob on Web API request
我正在嘗試通過身份驗證(通過JWT)GET請求返回存儲在Azure blob中的圖像。 當我在我的本地機器上運行項目並使用郵遞員請求圖像並且請求通過時我得到了我請求的圖像。 但是,一旦我將代碼部署到Azure並命中同一個端點,我就得到了403.代碼在我嘗試調用DownloadToStreamAsync的行中失敗。 這是我正在使用的代碼:
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());
}
我非常感謝能得到的任何幫助。
更新:
我將代碼更改為此並再次嘗試:
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:3/10/2017 5:28:26 AM - 5:28:26 AM
id received:b3bc7faf-0c86-4ce2-af84-30636825a485 - 5:28:27 AM
得到圖片記錄-5:28:27 AM
得到blob的名字b3bc7faf-0c86-4ce2-af84-30636825a485.JPG - 5:28:27 AM
blob不空 - 上午5:28:27
得到blob:Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob - 5:28:27 AM
得到fileName:b3bc7faf-0c86-4ce2-af84-30636825a485.JPG - 5:28:27 AM
即將開放閱讀流 - 上午5:28:27
我能夠檢索文件/ blob的名稱,這消除了不正確的帳戶密鑰作為問題的罪魁禍首。
解
我能夠使用以下代碼獲取我的代碼:
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;
}
但是,一旦我將代碼部署到Azure並命中相同的端點,我就得到403。
首先,請檢查您的Azure帳戶和密鑰以確保它們是正確的。 其次,請檢查服務器上的時鍾。 存儲服務確保請求在到達服務時不超過15分鍾。 如果服務器上的時間與存儲服務器不同步,則會出現403錯誤。
我能夠通過使用以下代碼解決問題:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.