5

I am trying to create a file and put it in blob with CloudBlockBlob.UploadFromStreamAsync() method.

Here's the code:

    private async void CreateCsvFile(int recId)
    {
        using (var csvFile = new StreamWriter())
        {
            for (int i = 1; i <= recId; ++i)
            {
                Ad ad = db.Ads.Find(i);
                if (ad != null)
                {
                    string rec = String.Format("{0}, {1}, {2}, {3}, {4}", ad.Title, ad.Category, ad.Price, ad.Description, ad.Phone);
                    csvFile.WriteLine(rec);
                }
            }

            csvFile.Flush();
            string blobName = Guid.NewGuid().ToString() + recId.ToString() + ".csv";
            CloudBlockBlob fileBlob = fileBlobContainer.GetBlockBlobReference(blobName);
            await fileBlob.UploadFromStreamAsync((Stream) csvFile);
        }
    }

Updated with a new requirement:

// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for encryption. 
using (MemoryStream msEncrypt = new MemoryStream())
{
   using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
   {
       using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
       {
           //Write all data to the stream.
           swEncrypt.Write(plainText);
       }
       encrypted = msEncrypt.ToArray();
   }
}

Questions:

  1. The file is created on the fly instead of being uploaded from client. Is this the correct way to do that?
  2. Compiler complains about 2 problems: 1) Ctor of StreamWriter does not take 0 argument; 2) Type 'StreamWriter' can't be converted to 'Stream' (I am casting here because UploadFromStreamAsync() method takes Stream type as parameter). How do I fix the compiler errors? Thanks!
itnovice
  • 503
  • 1
  • 4
  • 20
  • I hope none of your fields contains commas because this will cause a corrupted CSV. `Description` looks like it might... You might want to take a look [at this](http://stackoverflow.com/a/6377656/14357) – spender May 11 '15 at 01:03
  • Yeah, this is a great point. Thanks for the tip! – itnovice May 11 '15 at 04:36

1 Answers1

10

So I've previously got a stream to a blob as follows:

public async Task<Stream> GetWriteStreamAsync(string storagePath, string contentType)
{
    var blockBlob = blobContainer.GetBlockBlobReference(storagePath);
    blockBlob.Properties.ContentType = contentType;
    CloudBlobStream bb = await blockBlob.OpenWriteAsync();
    return bb;
}

so now you could

using(var str = await GetWriteStreamAsync("someBlobName.csv", "text/csv"))
using(var csvFile = new StreamWriter(str))
{
    for (int i = 1; i <= recId; ++i)
    {
        Ad ad = db.Ads.Find(i);
        if (ad != null)
        {
            string rec = String.Format("{0}, {1}, {2}, {3}, {4}", 
                                       ad.Title, 
                                       ad.Category, 
                                       ad.Price, 
                                       ad.Description, 
                                       ad.Phone);
            csvFile.WriteLine(rec);
        }
    }
    csvFile.Flush();
}
spender
  • 117,338
  • 33
  • 229
  • 351
  • Thanks Spender! What should I do if I need to encrypt the file before saving it to blob. – itnovice May 11 '15 at 04:38
  • You have to call extortion logic on 'rec' – Amit May 11 '15 at 07:24
  • 1
    @Amit can you please elaborate a bit more? – itnovice May 11 '15 at 13:28
  • hmm.. I see there is a typo in my comment above, it should be encryption. that said, the line csvFile.WriteLine(rec); commits any string you give as 'rec' to a blob, so you just a have to encrypt the string using any encryption algo. [MSND Link](https://msdn.microsoft.com/en-us/library/system.security.cryptography%28v=vs.110%29.aspx) which shows cryptography support on .net. – Amit May 11 '15 at 23:38