2013-01-20 69 views
0

我正在使用c#爲新資源創建共享訪問簽名(用戶應具有創建特權以在我的存儲帳戶上創建新資源)。 MS文檔已過時,我似乎無法使用我經歷過的不同博客文章工作。azure共享訪問簽名創建

現在我的代碼看起來像這樣:

public static string GetBlobSharedAccessSignitureUrl(CloudBlobContainer container,string nameOfBlobToCreateSaSfor) 
     { 
      var blob = container.GetBlockBlobReference(nameOfBlobToCreateSaSfor); 
      var policy = new SharedAccessBlobPolicy 
          { 
           SharedAccessExpiryTime = DateTime.Now.AddHours(1), 
           Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read 
          }; 


      container.GetSharedAccessSignature(policy); 
      string sas = blob.GetSharedAccessSignature(policy); 

      return blob.Uri.AbsoluteUri + sas; 

     } 

和返回的URL(對我的本地機器)看起來是這樣的(似乎是正確的)

http://127.0.0.1:10000/devstoreaccount1/photos/photos_4.jpg?sv=2012-02-12&se=2013-01-20T10%3A13%3A17Z&sr=b&sp=rw&sig=xxx 

我開始在Azure存儲模擬器,並通過提琴手試圖張貼到這個網址(也試過PUT)

我得到錯誤(404或400,取決於不同的代碼,我試過這個功能)

我需要做點別的嗎? (在舊的例子中,我看到他們在手前創建了一個資源 - 我已經嘗試過,但也沒有工作......)

Azure SDK版本是2.0,因此MS博客文章(和2012年10月之前的其他教程)被破壞(也可根據MS dev的博客http://blogs.msdn.com/b/windowsazurestorage/archive/2012/10/29/windows-azure-storage-client-library-2-0-breaking-changes-amp-migration-guide.aspx

任何幫助,將不勝感激

回答

1

如果您通過小提琴手或通過您的代碼發佈,請確保您添加「X- ms-blob-type「請求標題並將其值設置爲」BlockBlob「。看看這個示例代碼,它試圖上傳一個文件:

   FileInfo fInfo = new FileInfo(fileName);//fileName is the full path of the file. 
       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(blobSaSUrl); 
       NameValueCollection requestHeaders = new NameValueCollection(); 
       requestHeaders.Add("x-ms-blob-type", "BlockBlob"); 
       req.Method = "PUT"; 
       req.Headers.Add(requestHeaders); 
       req.ContentLength = fInfo.Length; 
       byte[] fileContents = new byte[fInfo.Length]; 
       using (FileStream fs = fInfo.OpenRead()) 
       { 
        fs.Read(fileContents, 0, fileContents.Length); 
        using (Stream s = req.GetRequestStream()) 
        { 
         s.Write(fileContents, 0, fileContents.Length); 
        } 
        using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) 
        { 
        } 
       } 
相關問題