30

I know how to save Streams, but I want to take that stream and create thumbnails and other sized images, but I don't know how to save a byte[] to the Azure Blob Storage.

This is what I'm doing now to save the Stream:

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

        // upload from Stream object during file upload
        blockBlob.UploadFromStream(stream);

        // But what about pushing a byte[] array?  I want to thumbnail and do some image manipulation

5 Answers 5

37

This used to be in the Storage Client library (version 1.7 for sure) - but they removed it in version 2.0

"All upload and download methods are now stream based, the FromFile, ByteArray, Text overloads have been removed."

http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/windows-azure-storage-client-library-2-0-breaking-changes-amp-migration-guide.aspx

Creating a read-only memory stream around the byte array is pretty lightweight though:

byte[] data = new byte[] { 1, 2, 3 };
using(var stream = new MemoryStream(data, writable: false)) {
    blockBlob.UploadFromStream(stream);
}

Update: UploadFromByteArray is back

MSDN documentation - from what I can tell in the source code, this came back for version 3.0 and is still there for version 4.0.

3
  • 3
    Ahhh, so a definitive no, its no longer available. Gotcha. Thanks.
    – Shane
    Commented Feb 27, 2013 at 15:17
  • Can we achieve the same in PowerShell? Especially in the context of inline deployment script running in ARM template?
    – Koder101
    Commented Dec 23, 2021 at 19:16
  • Prefere async : myBlobClient.UploadAsync(stream, true); //with overwrite Commented May 19, 2022 at 9:44
18

Using the new SDK azure.storage.blob

var blobContainerClient = new BlobContainerClient(storageConnectionString, containerName);
BlobClient blob = blobContainerClient.GetBlobClient(blobName);

using(var ms = new MemoryStream(data, false))
{
     await blob.UploadAsync(ms);
}
5
  • thanks for new methods, have a small query on blobName .. is it file name that we can use here as blobname
    – Glory Raj
    Commented Jul 27, 2020 at 17:11
  • 1
    Yes, blobname is filename!
    – rcruz
    Commented Jul 28, 2020 at 18:39
  • @EnigmaState you can use the whole path with directories, and when you use Azure Storage Explorer it transforms in directories for a better visualisation. Example: folder1/folder2/filename.txt Commented Jan 28, 2021 at 9:39
  • What kind of type is 'data'?
    – FelixHJ
    Commented Jan 18 at 9:11
  • 'data' is byte arrays i.e. byte[]
    – rcruz
    Commented Jan 26 at 12:29
8

update:

UploadFromByteArray is back.

public void UploadFromByteArray (
    byte[] buffer,
    int index,
    int count,
    [OptionalAttribute] AccessCondition accessCondition,
    [OptionalAttribute] BlobRequestOptions options,
    [OptionalAttribute] OperationContext operationContext
)

http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.uploadfrombytearray.aspx

new link: https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.blob.cloudblockblob.uploadfrombytearray?view=azure-dotnet-legacy

1
  • Link gives a 404
    – Sean
    Commented Jul 29, 2022 at 11:02
5

I also know nothing about Azure, but using Streams, you could approach it as follows:

//byte[] data;

using(var ms = new MemoryStream(data, false))
{
    blockBlob.UploadFromStream(ms);
}
4
  • so you can only upload streams, so you have to convert each time? There is no way to just push a byte array?
    – Shane
    Commented Feb 27, 2013 at 15:00
  • I don't know anything about Azure. Sorry.
    – spender
    Commented Feb 27, 2013 at 15:01
  • 1
    @Shane, I think if you use the MemoryStream constructor as per my edit above, there will be no copying of the byte array, so I'd view this approach as efficient.
    – spender
    Commented Feb 27, 2013 at 15:07
  • My testing (detailed in stackoverflow.com/questions/8624071/… ) suggests pretty strongly that it does re-use that byte array.
    – Rob Church
    Commented Feb 27, 2013 at 15:21
-1

This is the function I currently use:

//CREATE FILE FROM BYTE ARRAY
public static string createFileFromBytes(string containerName, string filePath, byte[] byteArray)
{

    try {

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings("StorageConnectionString").ConnectionString);



        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);

        if (container.Exists == true) {
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(filePath);


            try {
                using (memoryStream == new System.IO.MemoryStream(byteArray)) {
                    blockBlob.UploadFromStream(memoryStream);
                }
                return "";
            } catch (Exception ex) {
                return ex.Message.ToString();
            }
        } else {
            return "Container does not exist";
        }
    } catch (Exception ex) {
        return ex.Message.ToString();
    }
}
1
  • CloudBlockBlob is deprecated, so please use BlobClient.
    – SDG6
    Commented Jun 13 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.