Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading a file to Azure Blob on the fly

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!
like image 354
itnovice Avatar asked May 11 '15 00:05

itnovice


People also ask

How do I send data to Azure blob storage?

Upload contents of a folder to Data Box Blob storage To get your account key, in the Azure portal, go to your storage account. Go to Settings > Access keys, select a key, and paste it into the AzCopy command. If the specified destination container does not exist, AzCopy creates it and uploads the file into it.

How do you upload files to Azure blob storage using power automate?

Create Power Automate Desktop FlowGo to containers and create a new container. Open the container and on the and navigate to Shared access signature. Select add, create, and write permission, change the time if needed, and press Generate SAS token and URL. Copy the Blob SAS URL and save it as the variable in the flow.


1 Answers

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();
}
like image 57
spender Avatar answered Sep 21 '22 11:09

spender