How to store byte arrays, that is, byte [] in Azure Blob storage?

I know how to save streams, but I want to take this stream and create thumbnails and images of other sizes, but I don’t know how to save byte [] in Azure Blob storage.

Here is what I am 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
source share
4 answers

It was in the Client Storage library (version 1.7 for sure), but they deleted it in version 2.0

"All loading and loading methods are now based on streams, 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

:

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

: UploadFromByteArray

MSDN - , , 3.0 4.0.

+15

:

UploadFromByteArray .

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

+6

I also don't know anything about Azure, but using Streams, you can approach it like this:

//byte[] data;

using(var ms = new MemoryStream(data, false))
{
    blockBlob.UploadFromStream(ms);
}
+2
source

This is the function I'm using right now:

//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
source

All Articles