2017-05-17 10 views
-1

CloudFile 클래스를 올바르게 사용하여 MVC 응용 프로그램 내에서 Azure 파일 저장소에 파일을 업로드하는 방법에 대한 Microsoft의 예제를 찾을 수 없습니다. Microsoft의 설명서에는 Cloud​File.​Upload​From​Byte​Array​Async 방법이 나와 있습니다. 파일 내용을 바이트 []로 가지고 있기 때문에, UploadFromByteArrayAsync는 파일 내용을 Azure 공유의 파일 위치에 저장하는 올바른 방법 인 것 같습니다.byte []를 Azure 파일 저장소에 업로드하는 데 권장되는 코드

그러나 CloudFile 클래스는 또한 다시 업로드하고 endupload 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 메서드는 바이트 배열의 내용을 파일로 업로드하기 위해 비동기 작업을 수행하는 작업을 반환합니다.

데이터를 could 파일로 비동기 적으로 전송합니다.

자세한 내용은 아래 코드를 참조 수 :

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


     }