如何使用ID参数作为容器将多个文件上载到Azure blob存储?

时间:2018-05-22 15:03:27

标签: azure model-view-controller kendo-ui azure-storage-blobs

我在Azure上创建了一个存储帐户,用于将文件上传到。我正在使用KenoUI异步上传小部件,它允许多个文件上传。我想做的是让系统根据传入的Id参数创建一个容器,如果它还不存在的话。

我在实际上传部分遇到困难但是,我不确定如何将可枚举文件传递到存储帐户。到目前为止,这是我的代码:

public ActionResult Async_Save(IEnumerable<HttpPostedFileBase> files, string id)
    {
        //Connect to Azure
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("my_AzureStorageConnectionString"));

        //Create Blob Client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        //Retrieve a reference to a container
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("vehicle_" + id);

        try
        {
            // Create the container if it doesn't already exist
            blobContainer.CreateIfNotExists();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.InnerException);

        }
        foreach (var file in files)
        {
            //This doesn't seem right to me and it's where I'm struggling
            var fileName = Path.GetFileName(file.FileName);
            var physicalPath = Path.Combine(blobContainer, fileName);
            file.SaveAs(physicalPath);
        }

        // Return an empty string to signify success
        return Content("");
    }

我尝试做的是创建一个连接到我的Azure存储帐户的方法,并且:

  1. 检查是否存在与ID相同的容器 传入的参数,如果不存在,则创建它。
  2. 将文件上传到现有或新创建的目录,并为其提供ID的前缀,即&#34; _&#34;。

2 个答案:

答案 0 :(得分:2)

要在Azure存储中上载Blob,您需要使用Azure Storage SDK。

请替换以下代码:

    foreach (var file in files)
    {
        //This doesn't seem right to me and it's where I'm struggling
        var fileName = Path.GetFileName(file.FileName);
        var physicalPath = Path.Combine(blobContainer, fileName);
        file.SaveAs(physicalPath);
    }

有类似的东西:

  foreach (var file in files)
  {
      var blob = blobContainer.GetBlockBlobReference(file.FileName);
      using (var s = file.InputStream)
      {
          blob.UploadFromStream(s);
      }
  }

答案 1 :(得分:1)

正如Gaurav Mantri所说,我们需要使用Azure Storage SDK。

为了获得更好的性能,我们可以执行以下操作:

Parallel.ForEach(files, file =>

{

    CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName); 

    blob.UploadFromStream(file.InputStream);

    file.InputStream.Close();


});

您的代码会更好,如下所示:

public ActionResult Async_Save(List<HttpPostedFileBase> files, string id)

{

// Connect to Azure

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("my_AzureStorageConnectionString"));

try

{

   //Create Blob Client

   CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

   //Retrieve a reference to a container

   CloudBlobContainer blobContainer = blobClient.GetContainerReference("vehicle_" + id);

   // Create the container if it doesn't already exist

   blobContainer.CreateIfNotExists();

   Parallel.ForEach(files, file =>

   {

       CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName);

       blob.UploadFromStream(file.InputStream);

       file.InputStream.Close();

   });

}

catch (Exception ex)

{

    Console.WriteLine(ex.Message);

    Console.WriteLine(ex.InnerException);

} 

// Return an empty string to signify success

    return new ContentResult();

}

有关如何使用Azure Storage SDK的更多信息,我们可以参考:

Get Quick Start with Storage SDK