在Azure Blob中保存CSV日志的最佳技术

时间:2013-07-30 19:59:58

标签: c# azure-storage azure-storage-blobs

我正在将注册条目记录到CSV文件中(因为以后需要在整个月内包含所有注册),方法是将条目附加到该文件的最后一行,如:

using (StreamWriter w = new StreamWriter(currentPath, true))
{
    w.WriteLine(fileRow);
}

但在Windows Azure中,我无法访问物理文件,因此我需要使用Blob存储。

如何在Blob块上执行相同的操作?

我试过了:

CloudBlobContainer blobContainer = blobStorage.GetContainerReference(containerName);
cloudBlob = blobContainer.GetBlobReferenceFromServer(blobNameLogs);

MemoryStream oldText = new MemoryStream();
cloudBlob.DownloadToStream(oldText);

// record to save
string fileRow = row.ToFileRow();

// append
MemoryStream newText = new MemoryStream();
using (StreamWriter w = new StreamWriter(newText, System.Text.Encoding.Default))
{
    w.WriteLine(oldText);
    w.WriteLine(fileRow);

    // set blob data
    cloudBlob.UploadFromStream(newText);
}

但是在这次操作后我继续在文件上获取0 bytes ...我错过了什么?简单地将文本附加到现有文件是否有更简单的操作?

1 个答案:

答案 0 :(得分:0)

我建议您使用BlobStream来处理工作对象,如下例所示。

 _container.CreateIfNotExist();

  CloudBlob inputBlob = _container.GetBlobReference(outputBlobUri);
  CloudBlob outputBlob = _container.GetBlobReference(inputBlobUri);

  using (BlobStream input = inputBlob.OpenRead())
  using (BlobStream output = outputBlob.OpenWrite())
  {
    ProcessImage(input, output);

 output.Commit();
 outputBlob.Properties.ContentType = "image/jpeg";
 outputBlob.SetProperties();

 FunnyAppRepository<Post> postRepository = new FunnyAppRepository<Post>();
 Post post = postRepository.Find(partitionKey, rowkey);

 post.PostImage = outputBlobUri;
 post.State = true;
 postRepository.Update(post);
 postRepository.SubmitChange();

 _queue.DeleteMessage(message);
}