【问题标题】:Uploading a file to Azure Blob on the fly即时将文件上传到 Azure Blob
【发布时间】:2015-05-11 12:46:02
【问题描述】:

我正在尝试使用CloudBlockBlob.UploadFromStreamAsync() 方法创建一个文件并将其放入 blob。

代码如下:

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

更新了新要求:

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

问题:

  1. 文件是动态创建的,而不是从客户端上传的。这是正确的做法吗?
  2. 编译器抱怨2个问题:1)StreamWriter的Ctor不带0参数; 2) 'StreamWriter' 类型不能转换为 'Stream' (我在这里进行转换是因为UploadFromStreamAsync() 方法将 Stream 类型作为参数)。如何修复编译器错误? 谢谢!

【问题讨论】:

  • 我希望您的任何字段都不包含逗号,因为这会导致 CSV 损坏。 Description 看起来可能......你可能想看看at this
  • 是的,这是一个很好的观点。感谢您的提示!

标签: c# azure-blob-storage


【解决方案1】:

所以我之前有一个流到 blob,如下所示:

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

现在你可以

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

【讨论】:

  • 感谢斯彭德!如果我需要在将文件保存到 blob 之前对其进行加密,我该怎么办。
  • 你必须在'rec'上调用勒索逻辑
  • @Amit 你能详细说明一下吗?
  • hmm.. 我看到我上面的评论中有错字,应该是加密。也就是说,行 csvFile.WriteLine(rec);将您作为“rec”提供的任何字符串提交给 blob,因此您只需使用任何加密算法对字符串进行加密。 MSND Link 显示 .net 上的加密支持。
猜你喜欢
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2017-08-19
  • 2012-04-15
  • 2014-01-23
  • 1970-01-01
  • 2021-12-30
相关资源
最近更新 更多