1
votes

Good morning,

I'm trying to implement Azure Blog Storage for the first time using their example code provided. However my app is through a very broad 400 Bad Request error when trying to UploadFromStream().

I have done a bunch of searching on this issue. Almost everything i have come across identifies naming conventions of the container or blob to be the issue. this is NOT my issue, i'm using all lowercase, etc.

My code is no different from their example code:

The connection string:

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

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: enter image description here

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

1
As i mentioned, this is not my issue. If you look at my code you can see that my container and blob names are all lowercase. Thx for the replyNugs
Glossed right over that, sorry.maccettura
When you get the exception, can you expand RequestInformation node and include all the things that are displayed there. Usually you will find more information about the error. Taking a wild guess from your 2 attempts, I am assuming the redundancy level for the storage account in question is Premium_LRS. Can you please confirm that? If that's the case then you're getting this error because Premium_LRS storage accounts only support page blobs and you're trying to upload a block blob in there.Gaurav Mantri

1 Answers

2
votes

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