gpt4 book ai didi

asp.net - 尝试通过 Web API 请求检索 Azure blob 时出现 403 错误

转载 作者:行者123 更新时间:2023-12-03 04:26:28 26 4
gpt4 key购买 nike

我正在尝试让经过身份验证(通过 JWT)的 GET 请求返回存储在 Azure blob 中的图像。当我在本地计算机上运行该项目并使用 postman 请求图像并且请求通过时,我确实得到了我请求的图像。但是,一旦我将代码部署到 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: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 的名称,从而消除了导致问题的罪魁祸首是错误的帐户 key 。

解决方案

我能够让我的代码与以下代码一起使用:

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

最佳答案

However, once I deploy the code to Azure and hit the same endpoint I get a 403.

首先,请检查您的 Azure 帐户和 key ,确保它们正确。其次,请检查服务器上的时钟。存储服务确保请求到达服务时的时间不超过 15 分钟。如果您的服务器上的时间与存储服务器上的时间不同步,则会出现 403 错误。

关于asp.net - 尝试通过 Web API 请求检索 Azure blob 时出现 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42661661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com