26

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
Shane
  • 4,185
  • 8
  • 47
  • 64

5 Answers5

33

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.

Rob Church
  • 6,783
  • 3
  • 41
  • 46
  • 2
    Ahhh, so a definitive no, its no longer available. Gotcha. Thanks. – Shane Feb 27 '13 at 15:17
  • Can we achieve the same in PowerShell? Especially in the context of inline [deployment script](https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deployment-script-template#use-inline-scripts) running in ARM template? – Koder101 Dec 23 '21 at 19:16
  • Prefere async : myBlobClient.UploadAsync(stream, true); //with overwrite – Emmanuel Gleizer May 19 '22 at 09:44
15

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);
}
rcruz
  • 697
  • 8
  • 9
  • thanks for new methods, have a small query on blobName .. is it file name that we can use here as blobname – Glory Raj Jul 27 '20 at 17:11
  • 1
    Yes, blobname is filename! – rcruz Jul 28 '20 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 – Guilherme Flores Jan 28 '21 at 09:39
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

iceburg
  • 1,768
  • 3
  • 17
  • 25
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);
}
spender
  • 117,338
  • 33
  • 229
  • 351
  • so you can only upload streams, so you have to convert each time? There is no way to just push a byte array? – Shane Feb 27 '13 at 15:00
  • I don't know anything about Azure. Sorry. – spender Feb 27 '13 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 Feb 27 '13 at 15:07
  • My testing (detailed in http://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file/14748265#14748265 ) suggests pretty strongly that it does re-use that byte array. – Rob Church Feb 27 '13 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();
    }
}
QFDev
  • 8,668
  • 14
  • 58
  • 85