C# 如何将字节数组(即 byte[])保存到 Azure Blob 存储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15115212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:06:46  来源:igfitidea点击:

How do I save byte arrays i.e. byte[] to Azure Blob Storage?

c#azureazure-storage-blobs

提问by Shane

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.

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

This is what I'm doing now to save the Stream:

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

采纳答案by Rob Church

This used to be in the Storage Client library (version 1.7 for sure) - but they removed it in version 2.0

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

"All upload and download methods are now stream based, the FromFile, ByteArray, Text overloads have been removed."

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

http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/windows-azure-storage-client-library-2-0-breaking-changes-amp-migration-guide.aspx

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

更新:UploadFromByteArray 回来了

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.

MSDN 文档- 从我在源代码中可以看出,这在 3.0 版中回来了,并且在 4.0 版中仍然存在。

回答by spender

I also know nothing about Azure, but using Streams, you could approach it as follows:

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

//byte[] data;

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

回答by QFDev

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

回答by iceburg

update:

更新:

UploadFromByteArray is back.

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

http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.uploadfrombytearray.aspx