尝试在Web API请求上检索Azure Blob时出现403错误 [英] Getting 403 error when trying to retrieve an Azure blob on Web API request

查看:107
本文介绍了尝试在Web API请求上检索Azure Blob时出现403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过JWT进行身份验证的GET请求返回存储在Azure blob中的图像.当我在本地计算机上运行项目并使用邮递员请求图像时,请求通过,并且我确实获得了所请求的图像.但是,将代码部署到Azure并命中相同的端点后,我得到403.代码在尝试调用DownloadToStreamAsync的行失败.这是我正在使用的代码:

I am trying to have an authenticated (by JWT) GET request return an image stored in an Azure blob. When I run the project on my local machine and use postman to request an image and the request goes through and I do get the image I requested. However, once I deploy the code to Azure and hit the same endpoint I get a 403. The code fails at the line in which I try to invoke DownloadToStreamAsync. Here is the code I'm using:

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

我将非常感谢我能提供的任何帮助.

I would greatly appreciate any help I can get.

更新:

我将代码更改为此,然后再次尝试:

I changed my code to this and tried again:

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

上次尝试的日志结果如下:

the results from the log for the last try is this:

已收到请求并进行了身份验证,时间戳为UTC时间戳:2017年3月10日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

request received and authenticated, timestamp UTC: 3/10/2017 5:28:26 AM - 5:28:26 AM
id received: b3bc7faf-0c86-4ce2-af84-30636825a485 - 5:28:27 AM
got picture record -5:28:27 AM
got name of blob b3bc7faf-0c86-4ce2-af84-30636825a485.JPG - 5:28:27 AM
blob not empty - 5:28:27 AM
got blob: Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob - 5:28:27 AM
got fileName: b3bc7faf-0c86-4ce2-af84-30636825a485.JPG - 5:28:27 AM
about to open read stream - 5:28:27 AM

我能够检索文件/blob的名称,从而消除了不正确的帐户密钥作为问题的根源.

I was able to retrieve the name of the file/blob which eliminates an incorrect account key as the culprit of the issue.

解决方案

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

I was able to get my code to work with the following code:

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

推荐答案

我能够通过使用以下代码解决问题:

I was able to solve the problem by using the following code:

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

这篇关于尝试在Web API请求上检索Azure Blob时出现403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆