直接从流上异步上传到azure

时间:2012-12-15 10:45:19

标签: c# azure

我想直接使用流上传到我的azure本地开发帐户。我已经为azure blob存储创建了一个类。

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。

2 个答案:

答案 0 :(得分:2)

在我看来,你有一个竞争条件。在实际将内容放入其中之前,您就开始上传流。

你可能想要这样的东西:

Stream stream = new MemoryStream();

//Write to stream
for(int i=0;i<100;i++)
{
    for(int j=0;j<50;j++)
    {
        stream.WriteByte(65);
    }
}
stream.SetPosition(0); // <-- need to reset stream position to 0 before uploading

//Upload to azure
CloudBlob blob = container.GetBlobReference("sample.txt");
UploadFinished uploadFinished = delegate(IAsyncResult result)
{
    blob.EndUploadFromStream(result); // <-- have to call this in your handler
    Console.WriteLine("Upload finished {0} {1}", result.IsCompleted, stream.Position);
};
blob.BeginUploadFromStream(stream, new AsyncCallback(uploadFinished));

答案 1 :(得分:2)

来自smarx的好回答!另外,将stream.Close()移动到回调中。

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