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
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."
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);
}
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.
update:
UploadFromByteArray is back.
public void UploadFromByteArray (
byte[] buffer,
int index,
int count,
[OptionalAttribute] AccessCondition accessCondition,
[OptionalAttribute] BlobRequestOptions options,
[OptionalAttribute] OperationContext operationContext
)
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);
}
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);
}
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.