Azure Blob存储400错误请求 [英] Azure Blob Storage 400 Bad Request

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

问题描述

早上好

我正在尝试使用提供的示例代码首次实现Azure Blog Storage.但是,当我尝试上传UploadFromStream()时,我的应用程序遇到了一个非常广泛的400 Bad Request错误.

我在这个问题上做了很多搜索.我遇到的几乎所有内容都将容器或Blob的命名约定识别为问题.这不是我的问题,我正在使用所有小写字母,等等.

我的代码与示例代码没有什么不同

连接字符串:

<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=xxxx;AccountKey=xxxxxx;EndpointSuffix=core.windows.net" />

和代码:

// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob"
CloudBlockBlob blob = container.GetBlockBlobReference("myblob");

// Create the container if it doesn't already exist
container.CreateIfNotExists();

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"D:\Files\logo.png"))
{
    blob.UploadFromStream(fileStream);
}

以下是异常详细信息:

这就是我要做的.我唯一能想到的是,我正在使用HTTP而不是HTTPS在我的开发环境上运行它.不知道这可能是个问题吗?

此外,当尝试直接在Azure门户中将文件直接上载到容器时,我会收到

TestAzureFileUpload.txt的验证错误.详细信息:页面Blob 大小必须与512字节边界对齐.当前文件大小为 56."

这与我的问题有关吗?我在这里缺少设置吗?

我知道我在这里没有足够的人去帮助我确定确切的问题,但是我希望有人至少可以指出正确的方向来解决这个问题?

任何帮助将不胜感激

解决方案

我使用高级存储帐户来测试代码,并获得与您相同的"400错误请求". 从异常详细信息中,您可以看到不支持块Blob"消息.

这是我的异常详细信息的图片

要解决您的问题,我认为您应该了解块blob和页面blob之间的区别.

块Blob 由多个块组成,每个块均由一个块ID标识.您可以通过编写一组块并通过其块ID提交来创建或修改块Blob.它们是用于离散存储对象(例如jpg,txt,日志等)的文件.通常在本地OS中将其作为文件查看. 仅受标准存储帐户支持.

页面Blob 是512字节页面的集合,这些页面针对随机读写操作(例如VHD)进行了优化.要创建页面Blob,请初始化页面Blob并指定页面Blob将增长的最大大小.事实是,页面Blob是为Azure虚拟机磁盘设计的. 受标准存储帐户和高级存储帐户支持.

由于您使用的是高级存储,因此当前仅可用于在Azure虚拟机使用的磁盘上存储数据.

所以我的建议是:

如果您希望您的应用程序支持流和随机访问方案,并且能够从任何地方访问应用程序数据.您应该在标准帐户中使用块状Blob.

如果要提起和转移使用本机文件系统API来将数据读取和写入持久性磁盘的应用程序.或者,您想存储不需要从磁盘连接到的虚拟机外部访问的数据.您应该使用Page Blob.

参考链接:

And the code:

// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob"
CloudBlockBlob blob = container.GetBlockBlobReference("myblob");

// Create the container if it doesn't already exist
container.CreateIfNotExists();

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"D:\Files\logo.png"))
{
    blob.UploadFromStream(fileStream);
}

Here is the exception details:

This is all i have to go on. The only other thing i can think of is that i'm running this on my development environment with HTTP not HTTPS. Not sure if this might be a issue?

EDIT: Additionally, when attempting to upload a file directily in the Azure portal to the container i recieve a

Validation error for TestAzureFileUpload.txt. Details: "The page blob size must be aligned to a 512-byte boundary. The current file size is 56."

Could this be related to my issue? Am i missing some setting here?

I know i do not have enough to go on here for anyone to help me identify the exact issue, but i am hoping that someone can at least point me in the right direction to resolve this?

Any help would be appreciated

解决方案

I use a Premium storage account to test the code and get the same "400 bad request" as yours. From the exception details, you can see the "Block blobs are not supported" message.

Here is an image of my exception details

To solve your problem, I think you should know the difference between block blob and page blob.

Block blobs are comprised of blocks, each of which is identified by a block ID. You create or modify a block blob by writing a set of blocks and committing them by their block IDs. they are for you discrete storage objects like jpg, txt, log, etc. That you'd typically view as a file in your local OS. Supported by standard storage account only.

Page blobs are a collection of 512-byte pages optimized for random read and write operations, such as VHD's. To create a page blob, you initialize the page blob and specify the maximum size the page blob will grow. The truth is, page blobs are designed for Azure Virtual Machine disks. Supported by both standard and Premium storage account.

Since you are using the Premium Storage, which is currently available only for storing data on disks used by Azure Virtual Machines.

So my suggestion is :

If you want your application to support streaming and random access scenarios, and be able to access application data from anywhere. You should use block blobs with the standard account.

If you want to lift and shift applications that use native file system APIs to read and write data to persistent disks. Or you want to store data that is not required to be accessed from outside the virtual machine to which the disk is attached. You should use Page blobs.

Reference link:

Understanding Block Blobs, Append Blobs, and Page Blobs

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

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