2017-05-17 5 views
-1

CloudFileクラスを正しく使用して、MVCアプリケーション内からファイルをAzureファイルストレージにアップロードするというMicrosoftの例が見つかりません。 MicrosoftのドキュメントにはCloud​File.​Upload​From​Byte​Array​Asyncメソッドがあります。私はファイルの内容がバイト[]であるので、UploadFromByteArrayAsyncはAzure共有上のファイル位置にファイルの内容を保存する正しい方法のようです。byte []をAzureファイルストレージにアップロードするための推奨コード

しかし、CloudFileクラスには、再アップロードとエンデッドアップロードmethodsがあります。どのような条件下でこれらの方法を使用する必要がありますか?

答えて

0

これを試してみてください:

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

// Create the blob client. 
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

// Retrieve reference to a previously created container. 
CloudBlobContainer container = blobClient.GetContainerReference(containerName.ToString().ToLower()); 
await container.CreateIfNotExistsAsync(); 
// Retrieve reference to blob 
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobRef); 
// Upload the file 
await blockBlob.UploadFromByteArrayAsync(myByteArray, 0, myByteArray.Length); 
2

これらのメソッドを使用する正しい方法は何ですか?

私が知る限り、UploadFromByteArrayAsyncメソッドは、バイト配列の内容をファイルにアップロードするための非同期操作を実行するタスクを返します。

データはcanファイルに非同期で転送されます。

詳細は、あなたは以下のコードを参照できます。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    "connectionstring"); 
      CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); 

      CloudFileShare share = fileClient.GetShareReference("filesharename"); 

      CloudFileDirectory rootDir = share.GetRootDirectoryReference(); 


      int size = 5 * 1024 * 1024; 
      byte[] buffer = new byte[size]; 
      Random rand = new Random(); 
      rand.NextBytes(buffer); 

      CloudFile file = rootDir.GetFileReference("Log3.txt"); 

      file.UploadFromByteArrayAsync(buffer, 0, buffer.Length); 

BeginUploadFromByteArray方法は、ファイルにバイト配列の内容をアップロードするための非同期操作を開始します。

このメソッドを使用すると、プログラムはuploadFromByteArrayAsyncというように、couldファイルに非同期でデータを転送します。

メソッドを使用する場合は、非同期操作が完了したときに通知を受け取るコールバックメソッドを作成する必要があります。

詳細は、あなたは以下のコードを参照できます。

static void Main(string[] args) 
     { 

      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    "connectionstring"); 
      CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); 

      CloudFileShare share = fileClient.GetShareReference("fileshare"); 

      CloudFileDirectory rootDir = share.GetRootDirectoryReference(); 


      int size = 5 * 1024 * 1024; 
      byte[] buffer = new byte[size]; 
      Random rand = new Random(); 
      rand.NextBytes(buffer); 

      CloudFile file = rootDir.GetFileReference("Log3.txt"); 

      //This string will pass to the callback function 
      string result = "aa"; 

      //Begins an asynchronous operation to upload the contents of a byte array to a file. If the file already exists on the service, it will be overwritten. 
      var res = file.BeginUploadFromByteArray(buffer, 0, buffer.Length, ProcessInformation, result); 


     } 

     static void ProcessInformation(IAsyncResult result) 
     { 

      //The callback delegate that will receive notification when the asynchronous operation completes. 
      string Name = (string)result.AsyncState; 

      //this Name is aa 
      Console.WriteLine(Name); 
      Console.WriteLine("Complete"); 
     } 

EndUploadFromByteArray方法が完全に実行BeginUploadFromByteArray方法を待ちます。それを使用する方法について

、あなたはコード以下を参照できます。

static void Main(string[] args) 
     { 
      // TableTest(); 
      CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    "connectionstring"); 
      CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); 

      CloudFileShare share = fileClient.GetShareReference("fileshare"); 

      CloudFileDirectory rootDir = share.GetRootDirectoryReference(); 


      int size = 5 * 1024 * 1024; 
      byte[] buffer = new byte[size]; 
      Random rand = new Random(); 
      rand.NextBytes(buffer); 

      CloudFile file = rootDir.GetFileReference("Log3.txt"); 

      file.UploadFromByteArrayAsync(buffer, 0, buffer.Length); 

      string result = "aa"; 

      //Begins an asynchronous operation to upload the contents of a byte array to a file. If the file already exists on the service, it will be overwritten. 
      var res = file.BeginUploadFromByteArray(buffer, 0, buffer.Length, ProcessInformation, result); 

      //Ends an asynchronous operation to upload the contents of a byte array to a file. 
      //wait for the BeginUploadFromByteArray method execute completely then continue run the codes 
      file.EndUploadFromByteArray(res); 

      Console.ReadLine(); 

     } 

     static void ProcessInformation(IAsyncResult result) 
     { 

      //The callback delegate that will receive notification when the asynchronous operation completes. 
      string Name = (string)result.AsyncState; 

      //this Name is aa 
      Console.WriteLine(Name); 

      Console.WriteLine("Complete"); 


     } 
関連する問題