Windows Azure:在云blob容器上创建文件

时间:2010-04-11 22:10:09

标签: windows azure blob

我正在编写一个将在云上执行的程序。该程序将生成一个应写入文件的输出,该文件应保存在blob容器中。

我不知道该怎么做

这段代码

FileStream fs = new FileStream(file, FileMode.Create);
        StreamWriter sw = new StreamWriter(fs);

在云上生成一个名为“file”的文件...


哦..然后如何将内容存储到blob ..

1 个答案:

答案 0 :(得分:1)

您是否尝试上传Page blob或Block blob?通常阻止blob是必需的,除非您要从blob图像创建VM,然后需要页面blob。

然而,这样的东西有效。此片段取自最优秀的Blob Transfer Utility,请检查所有上传和下载blob需求。 (如果您需要VHD,只需从Block To Page更改类型)

public void UploadBlobAsync(ICloudBlob blob, string LocalFile)
    {
        // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems.
        // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client.
        // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called.
        lock (WorkingLock)
        {
            if (!Working)
                Working = true;
            else
                throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.");
        }

        // Attempt to open the file first so that we throw an exception before getting into the async work
        using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { }

        // Create an async op in order to raise the events back to the client on the correct thread.
        asyncOp = AsyncOperationManager.CreateOperation(blob);

        TransferType = TransferTypeEnum.Upload;
        m_Blob = blob;
        m_FileName = LocalFile;

        var file = new FileInfo(m_FileName);
        long fileSize = file.Length;

        FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        ProgressStream pstream = new ProgressStream(fs);
        pstream.ProgressChanged += pstream_ProgressChanged;
        pstream.SetLength(fileSize);
        m_Blob.ServiceClient.ParallelOperationThreadCount = 10;
        asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));
    }