快速将文件上传到Azure Blob [英] Uploading a file to Azure Blob on the fly

查看:77
本文介绍了快速将文件上传到Azure Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

代码如下:

    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类型作为参数).如何解决编译器错误?谢谢!
  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!

推荐答案

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

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

所以现在您可以

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

这篇关于快速将文件上传到Azure Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆