【发布时间】:2017-07-28 10:47:59
【问题描述】:
我正在尝试让经过身份验证的(通过 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 时间戳:2017 年 3 月 10 日上午 5:28:26 - 上午 5:28:26
收到的 ID: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
得到文件名: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;
}
【问题讨论】:
标签: asp.net azure asp.net-web-api azure-storage