balexandre
balexandre

Reputation: 75103

best technique for saving CSV logs in Azure Blob

I'm logging a registration entry to a CSV file (as it's needed for later to include all registrations throughout the month), by appending the entry to the last line of that file like:

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

but in Windows Azure, I can't access physical files, so I need to use Blob Storage.

How do I perform the same operation on a Blob block?

I tried:

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

But I keep getting 0 bytes on the file after this operation ... what am I missing? Is there an easier operation for simply append text to an existing file?

Upvotes: 0

Views: 1477

Answers (1)

user274512
user274512

Reputation:

I suggest your work objects by using the BlobStream, as in the following example.

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

Upvotes: 0

Related Questions