尝试在Web API请求上检索Azure blob时出现403错误

时间:2017-03-08 02:01:59

标签: asp.net azure asp.net-web-api azure-storage

我正在尝试通过身份验证(通过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 not empty - 5:28:27 AM 得到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;
    }

2 个答案:

答案 0 :(得分:0)

  

但是,一旦我将代码部署到Azure并点击相同的端点,我就得到403。

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

答案 1 :(得分: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;
    }