直接从流异步上传到天蓝色 [英] Uploading to azure asynchronously directly from a stream

查看:79
本文介绍了直接从流异步上传到天蓝色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接使用流上传到我的Azure本地开发帐户.我已经创建了一个用于存储天蓝色blob的类.

I want to upload to my azure local development account directly using a stream. I have created a class for azure blob storage.

public class AzureBlob
{
    delegate void UploadFinished(IAsyncResult result);
    public void uploadFile()
    {
        //Initial configuration
        CloudStorage account = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobClient client = account.CreateBlobClient();
        CloudBlobContainer container = client.GetContainerReference("myfiles");
        Stream stream = new MemoryStream();

        //Upload to azure
        CloudBlob blob = container.GetBlobReference("sample.txt");
        UploadFinished uploadFinished = delegate(IAsyncResult result)
        {
            Console.WriteLine("Upload finished {0} {1}", result.IsCompleted, stream.Position);
        };
        blob.BeginUploadFromStream(stream, new AsyncCallback(uploadFinished));

        //Write to stream
        for(int i=0;i<100;i++)
        {
            for(int j=0;j<50;j++)
            {
                stream.WriteByte(65);
            }
        }
        stream.Close();


    }
}

我面临的第一个问题是我在存储中获取了文件,但其中不包含任何数据.即使我在回调中使用EndOfUploadStream方法(根据一些stackoverflow的答案,这也是解决方案).其次,我将断点放在回调中,发现关闭流之前,该回调执行一次,并且程序会不断更新流.此时,流位置约为913(大多数情况下是随机的).请帮助我直接通过流异步上传Blob.

The first problem that i face is i get the file in the storage but it contains no data. Even if i use EndOfUploadStream method in the callback (which according to some stackoverflow answer is a solution). Secondly, i put the break point inside the callback and i found that the callback gets executed once, before i close the stream, and the program keeps updating the stream. At this point the stream position is around 913(mostly random). Please help me in uploading the blob directly through the stream asynchronously.

推荐答案

smarx的绝佳答案!另外,将stream.Close()移到回调中.

great answer from smarx! Also, move stream.Close() into the callback.

delegate void BeginUploadFinished(IAsyncResult result);
public void UploadFile()
{
    //Initial configuration
    var account = CloudStorageAccount.DevelopmentStorageAccount;
    var client = account.CreateCloudBlobClient();
    var container = client.GetContainerReference("myfiles");
    Stream stream = new MemoryStream();
    var state = new Object{};

    //Upload to azure
    var blob = container.GetBlobReference("test.txt");
    BeginUploadFinished beginUploadFinished = delegate(IAsyncResult result)
    {
        blob.EndUploadFromStream(result);
        Trace.WriteLine("EndUploadFromStream", "Information");
        stream.Close();
        stream.Dispose();
    };

    //Write to stream
    Trace.WriteLine("Writing Stream", "Information");
    for (var i = 0; i < 100; i++)
    {
        for (var j = 0; j < 50; j++)
        {
            stream.WriteByte(65);
        }
    }
    stream.Seek(0, SeekOrigin.Begin);

    Trace.WriteLine("BeginUploadFromStream", "Information");
    var _result =  blob.BeginUploadFromStream(stream, new AsyncCallback(beginUploadFinished), state);
    _result.AsyncWaitHandle.WaitOne();
}

这篇关于直接从流异步上传到天蓝色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆