将MemoryStream保存到Azure Blob存储 [英] Saving a MemoryStream to Azure blob storage

查看:67
本文介绍了将MemoryStream保存到Azure Blob存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Infragistics报表系统将报表创建为PDF,并将其保存到Azure Blob存储中,但是我无法使其正常工作.我将报告生成为Report对象,没有任何问题.该对象具有一种称为 Publish 的方法,该方法将报告以指定的文件格式(在我的情况下为PDF)发布到流中.当代码尝试从流中上传时,出现此错误

I'm using the Infragistics report system to create reports as PDFs and save them to Azure Blob Storage but I'm having trouble getting it to work. I generate the report as a Report object without any problems. This object has a method called Publish which publishes the report to a stream in a specified file format, which in my case is PDF. When the code attempts to Upload from the stream, I get this error

Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误的请求

Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request

我不知道是什么原因造成的,错误消息实际上并没有给我太多工作.我在开发时使用的是本地存储,这一切似乎都很好(我可以存储图像而没有任何问题).这是崩溃的方法

I have no idea what's causing this, the error message doesn't actually give me much to work on. I'm using local storage while I'm developing, this all seems to be working fine (I can store images without any problems). This is the method that's crashing out

public async Task<CloudBlockBlob> UploadAndSaveReportAsPDFToBlobAsync(Report report, EnumHelper.Reports reportName, string containerName)
{
    chpBlobContainer = blobClient.GetContainerReference(containerName);
    string blobName = Guid.NewGuid().ToString() + Path.GetExtension(reportName.GetDescription());
    // Retrieve reference to a blob. 
    CloudBlockBlob reportBlob = chpBlobContainer.GetBlockBlobReference(blobName);
    using (Stream stream = new MemoryStream())
    {
        try
        {
            report.Publish(stream, FileFormat.PDF);
        }
        catch(Exception ex)
        {
            //
        }

        await reportBlob.UploadFromStreamAsync(stream);
    }
    return reportBlob;
}

这是更多错误消息

执行函数时发生异常:Functions.ProcessQueueMessageMicrosoft.Azure.WebJobs.Host.FunctionInvocationException:执行功能时发生异常:Functions.ProcessQueueMessage ---> Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误的请求.---> System.Net.WebException:远程服务器返回错误:(400)错误的请求.在Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException [T](HttpStatusCode ExpectedStatusCode,HttpStatusCode actualStatusCode,T retVal,StorageCommandBase 1 cmd,Exception ex)位于c:\ Program Files(x86)\ Jenkins \ workspace中\ release_dotnet_master \ Lib \ Common \ Shared \ Protocol \ HttpResponseParsers.Common.cs:line 50在Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.<> c__DisplayClass42.< PutBlobImpl> b__41(c:\ Program Files(x86)\中的RESTCommand 1 cmd,HttpWebResponse响应,Exception ex,OperationContext ctx)\Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Blob \ CloudBlockBlob.cs:line 2339在Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse [T](IAsyncResult getResponseResult)中的c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Core \ Executor \ Executor.cs:line299---内部异常堆栈跟踪的结尾---

Exception while executing function: Functions.ProcessQueueMessage Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ProcessQueueMessage ---> Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode, HttpStatusCode actualStatusCode, T retVal, StorageCommandBase1 cmd, Exception ex) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Shared\Protocol\HttpResponseParsers.Common.cs:line 50 at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.<>c__DisplayClass42.<PutBlobImpl>b__41(RESTCommand1 cmd, HttpWebResponse resp, Exception ex, OperationContext ctx) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 2339 at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse[T](IAsyncResult getResponseResult) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 299 --- End of inner exception stack trace ---

有人有什么建议吗?

我已经在blob服务的构造函数上更新了连接代码,现在看起来像这样,但是仍然无法正常工作,而且提琴手给了我这个

I've updated my connection code on the constructor of my blob service, it now looks like this, but it still doesn't work and fiddler gives me this

CONNECT本地主机:10000 HTTP/1.1主机:localhost:10000

CONNECT localhost:10000 HTTP/1.1 Host: localhost:10000

HTTP/1.1 200连接已建立FiddlerGateway:直接开始时间:14:23:33.061连接方式:关闭结束时间:14:23:33.063ClientToServerBytes:125ServerToClientBytes:505

HTTP/1.1 200 Connection Established FiddlerGateway: Direct StartTime: 14:23:33.061 Connection: close EndTime: 14:23:33.063 ClientToServerBytes: 125 ServerToClientBytes: 505

但是它的http,我的连接指定了https,我还是一个聪明的人

but its http, my connection specified https, Im still none the wiser

public BlobService()
    {
        var storageCredentials = new StorageCredentials("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==");
        var blobEndpoint = new Uri("https://localhost.fiddler:10000");
        var queueEndpoint = new Uri("https://localhost.fiddler:10001");
        var tableEndpoint = new Uri("https://localhost.fiddler:10002");
        var storageAccount = new CloudStorageAccount(storageCredentials, blobEndpoint, queueEndpoint, tableEndpoint, null);
        var blobClient = storageAccount.CreateCloudBlobClient();
        chpBlobContainer = blobClient.GetContainerReference("CHPReports");
        if (chpBlobContainer.CreateIfNotExists())
        {
            // Enable public access on the newly created "images" container.
            chpBlobContainer.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });
        }
    }

推荐答案

您不能将"CHPReports"用作容器名,因为名称必须为小写.

You cannot use "CHPReports" as the container name as the name must be lowercase.

确保容器名称为有效:

容器名称必须是有效的DNS名称,符合以下要求命名规则:容器名称必须以字母或数字开头,并且只能包含字母,数字和破折号(-)字符.每一个破折号(-)必须紧跟在前面,并在后面加上一个字母或数字;容器中不允许连续的破折号名称.容器名称中的所有字母都必须小写.容器名称必须在3到63个字符之间.

A container name must be a valid DNS name, conforming to the following naming rules: Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character. Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names. All letters in a container name must be lowercase. Container names must be from 3 through 63 characters long.

这篇关于将MemoryStream保存到Azure Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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