【问题标题】:Windows Azure: Creation of a file on cloud blob containerWindows Azure:在云 blob 容器上创建文件
【发布时间】:2011-02-06 19:33:13
【问题描述】:

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

我不知道该怎么做

请问这段代码

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

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


哦..那么如何将内容存储到 blob..

【问题讨论】:

标签: windows azure blob


【解决方案1】:

您是在尝试上传页面 blob 还是块 blob?通常需要块 blob,除非您要从 blob 映像创建 VM,否则需要页 blob。

然而,这样的事情是可行的。这个 sn-p 取自最优秀的 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));
    }

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 2018-11-21
    • 2020-09-15
    • 2013-02-23
    • 2020-07-20
    • 2014-01-23
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多