我如何保存字节数组,即byte[]到Azure Blob存储

本文关键字:byte 存储 Blob Azure 数组 何保存 保存 字节数 字节 | 更新日期: 2023-09-27 18:01:28

我知道如何保存流,但我想把流和创建缩略图和其他大小的图像,但我不知道如何保存一个字节[]到Azure Blob存储

这就是我现在所做的来保存流:

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

我如何保存字节数组,即byte[]到Azure Blob存储

这曾经是存储客户端库(1.7版本)-但他们在2.0版本中删除了它

"现在所有的上传和下载方法都是基于流的,FromFile,ByteArray,文本重载已被删除。"

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版本中仍然存在。

使用新的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);
}

update:

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

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

我对Azure也一无所知,但是使用Streams,你可以这样接近它:

//byte[] data;
using(var ms = new MemoryStream(data, false))
{
    blockBlob.UploadFromStream(ms);
}

这是我目前使用的函数:

//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();
    }
}