将 MemoryStream 保存到 Azure blob 存储

Saving a MemoryStream to Azure blob storage

我正在使用 Infragistics 报告系统将报告创建为 PDF 并将它们保存到 Azure Blob 存储,但我无法让它正常工作。我将报告生成为 Report 对象,没有任何问题。这个对象有一个名为 Publish 的方法,它以指定的文件格式将报告发布到流中,在我的例子中是 PDF。当代码尝试从流上传时,出现此错误

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

我不知道是什么原因造成的,错误消息实际上并没有给我太多帮助。我在开发时使用本地存储,这一切似乎都运行良好(我可以毫无问题地存储图像)。这是崩溃的方法

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

这是错误信息的一部分

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 服务的构造函数上更新了我的连接代码,它现在看起来像这样,但它仍然不起作用,fiddler 给了我这个

连接 localhost:10000 HTTP/1.1 主持人:localhost:10000

HTTP/1.1 200 连接已建立 FiddlerGateway:直接 开始时间:14:23:33.061 连接:关闭 结束时间:14:23:33.063 客户端到服务器字节数:125 ServerToClientBytes:505

但它是 http,我的连接指定了 https,我还是none更聪明

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" 作为容器名称e,因为名称必须小写。

确保容器名称是valid:

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.