我正在编写一个将在云上执行的程序。该程序将生成应写入文件的输出,并且该文件应保存在 blob 容器中。
我不知道该怎么做
这段代码
FileStream fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
在云端生成名为“file”的文件...
<小时/>哦..那么如何将内容存储到blob..
最佳答案
您是否正在尝试上传页面 blob 或 block blob?通常需要 block blob,除非您要从 blob 镜像创建 VM,否则需要页 blob。
但是这样的东西是有效的。此片段取自最优秀的Blob Transfer Utility检查一下它是否满足您所有的上传和下载 blob 需求。 (如果您需要 VHD,只需将类型从“ block 到页面”更改)
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));
}
关于Windows Azure : Creation of a file on cloud blob container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2618899/