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:
UploadFromStreamAsync()
method takes Stream type as parameter). How do I fix the compiler errors?
Thanks!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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With