Create File in Azure Blob Storage Using UploadText

3.3k views Asked by At

Thanks in advance for any help.

I have a string that I create programmatically in C# that I want to persist to Azure BLOB storage. I build the string in code, then attempt to store it as the final step (a text file). All works well until my string becomes a certain size. The process truncates the string at a point. The file gets created, but the overflow amount is simply chopped off. It seems that I need to break the operation into chunks, but I'm not quite sure how to get there. I'm thinking I need to read through the string and then insert in chunks.

I'm using the Azure Management Libraries. Here is my code:

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(MyString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer cloudContainer = blobClient.GetContainerReference(DestinationContainerName);
        cloudContainer.CreateIfNotExists();
        CloudBlockBlob blockBlob = cloudContainer.GetBlockBlobReference(DestinationFileName);

        blockBlob.UploadText(sTheFile) 

The variable "sTheFile" is the string that appears to be too large.

I do need to use UploadText as the file is being built virtually in code. Does anyone know how to parse through my string (stheFile) and create the BLOB in chunks?

Sincere thanks...

2

There are 2 answers

2
RogueThinking On BEST ANSWER

Block blobs are limited to 64MB for a single upload and the storage client defaults to 32MB which you can increase to 64MB by setting SingleBlobUploadThresholdInBytes.

For blobs above that size you will need to convert the string to a MemoryStream and use PutBlock to send it in blocks.

The article at this link shows how to do that:

Windows Azure - BlockBlob PutBlock Method

0
DanielG On

Thanks everyone, I feel like a moron. Zhaoxing was bang on. Because I was using a programmatically generated string, I converted to a MemoryStream instead of a FileStream.

Final solution for those who may need it in the future:

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("MyConnectionString");
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer cloudContainer = blobClient.GetContainerReference(DestinationContainerName);
        cloudContainer.CreateIfNotExists();
        CloudBlockBlob blockBlob = cloudContainer.GetBlockBlobReference(DestinationFileName);
        using (MemoryStream ms = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(sFile.ToString())))
        {
            blockBlob.UploadFromStream(ms);
        }