21

我知道如何保存 Streams,但我想获取该流并创建缩略图和其他大小的图像,但我不知道如何将 byte[] 保存到 Azure Blob 存储。

这就是我现在为保存 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
4

5 回答 5

30

这曾经在 Storage Client 库中(肯定是 1.7 版) - 但他们在 2.0 版中删除了它

“所有上传和下载方法现在都是基于流的,FromFile、ByteArray、Text 重载已被删除。”

http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/windows-azure-storage-client-library-2-0-break-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 版中仍然存在。

于 2013-02-27T15:14:41.403 回答
9

更新:

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

于 2014-07-02T01:36:42.590 回答
9

使用新的 SDKazure.storage.blob

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

using(var ms = new MemoryStream(data, false))
{
     await blob.UploadAsync(ms);
}
于 2020-07-02T22:10:32.873 回答
4

我对 Azure 也一无所知,但使用 Streams,您可以按如下方式处理它:

//byte[] data;

using(var ms = new MemoryStream(data, false))
{
    blockBlob.UploadFromStream(ms);
}
于 2013-02-27T14:59:05.383 回答
-1

这是我目前使用的功能:

//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();
    }
}
于 2013-02-27T19:48:42.980 回答