2016-06-09 40 views
1

我是Xamarin開發新手。我正在使用Azure存儲桶在Android移動應用中上傳文件。 爲了測試目的,我構建了無需中斷即可成功上傳Azure存儲桶上的文件的控制檯應用程序。 但是,當我試圖從移動開發中創建訪問令牌時,它會卡住並給我超時異常。如果我從控制檯應用程序創建令牌並將該令牌用於文件上傳,則會給我另一個例外,如「找不到佔位符」。 正如我所料,我需要一個用於代幣生成的移動服務,如果您有任何想法,請與我分享您的意見,這將是非常有益的。 我還在上傳用於Android移動開發的代碼。Xamarin中的Azure文件同步

[Activity(Label = "WedAndroidApp", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{ 
    int count = 1; 
    //string sas = "https://supplypark.blob.core.windows.net/transaction-images?sv=2015-04-05&sr=c&sig=iJ8CZOi%2BktarlmrbZVHK7rYLdMOnKCeBjuPqjrrkGnM%3D&se=2016-06-09T14%3A21%3A49Z&sp=rwdl"; 
    string sas_token = "https://supplypark.blob.core.windows.net/transaction-images?sv=2015-04-05&sr=c&sig=AeWe8rghAlKz77Xh%2BUM6S46AuUQzAaD2djqhaW9wdN8%3D&se=2016-06-09T14%3A21%3A49Z&sp=rwdl"; 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     // Get our button from the layout resource, 
     // and attach an event to it 
     Button button = FindViewById<Button>(Resource.Id.MyButton); 

     //button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; 
     button.Click += async delegate { 
      button.Text = string.Format("{0} clicks!", count++); 
      await UseContainerSAS(sas_token); 
     }; 
    } 

    static async Task UseContainerSAS(string sas) 
    { 
     //Try performing container operations with the SAS provided. 

     //Return a reference to the container using the SAS URI. 
     CloudBlobContainer container = new CloudBlobContainer(new Uri(sas)); 
     string date = DateTime.Now.ToString(); 
     try 
     { 
      //Write operation: write a new blob to the container. 
      CloudBlockBlob blob = container.GetBlockBlobReference("tdi" + date + ".txt"); 

      string blobContent = "This blob was created with a shared access signature granting write permissions to the container. "; 
      MemoryStream msWrite = new 
      MemoryStream(Encoding.UTF8.GetBytes(blobContent)); 
      msWrite.Position = 0; 
      using (msWrite) 
      { 
       await blob.UploadFromStreamAsync(msWrite); 
      } 
      Console.WriteLine("Write operation succeeded for SAS " + sas); 
      Console.WriteLine(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Write operation failed for SAS " + sas); 
      Console.WriteLine("Additional error information: " + e.Message); 
      Console.WriteLine(); 
     } 

    } 
} 
+0

我正在關注這個https://azure.microsoft.com/en-in/documentation/articles/storage-xamarin-blob-storage/的Azure移動開發。但Azure存儲依賴關係不能從neget包獲取下載。 –

+0

你好,夥計們,我解決了這個問題。如果您遇到安裝依賴關係的問題,請使用包控制檯管理器來安裝依賴關係。請按照下列步驟操作: - 「Install-Package WindowsAzure.Storage -Version 4.4.1-preview -Pre」 –

回答

0

如果您已經在使用Azure的移動應用,我會建議你使用內置的功能,訪問Azure存儲:Connect to Azure Storage in your Xamarin.Forms app

否則,你應該寫一個創建SAS令牌上的代碼你的服務器,而不是你的客戶。存儲主密鑰永遠不會與您的客戶端應用程序一起分發,因爲這會帶來很大的安全風險。您應該只在服務器上有密鑰,並且它應該向客戶端發送一個SAS令牌來限制對特定Blob或容器的訪問權限。

如果您使用.NET後端移動應用程序/移動服務,那麼您應該將SAS代碼移至服務器上的自定義API。如果您使用Node.js,則可以按照本教程進行操作:Work with shared access signatures in Node.js

如果您沒有移動應用程序後端,則可以使用Azure函數生成SAS令牌。這裏是一個樣本:https://github.com/lindydonna/GetSasToken-function

請注意,Azure移動服務已棄用,因此您應該使用Azure移動應用程序。

+0

謝謝, 我也在考慮在服務器上移動SAS代碼。由於安全風險,但我生成了SAS令牌,並將其作爲參數傳遞給UseContainerSAS(字符串sas)函數,但它在文件上傳中卡住了。 我是否也應該在服務器上移動文件上傳的邏輯? –

+0

不,文件上傳的邏輯應該仍然在客戶端上。我會在調試器中瀏覽它,看看發生了什麼。您可以從控制檯應用程序開始,以使其更易於調試。另外,如果我的回答有幫助,請接受它。 :) –

+0

我試過它在工作的控制檯應用程序中的相同的東西。我使用了相同的代碼。可能是我用於android開發中的文件上傳的Nuget依賴關係的問題。 –