2015-10-01 53 views
0

Microsoft聲明獲取Blob只是一個普通的http獲取https://myaccount.blob.core.windows.net/mycontainer/myblob。但是當我擁有一個賬戶+共享密鑰時,如何格式化字符串?GET文件的私有blob存儲url的格式。 Rest API

我知道有一個Azure的SDK,但我創造一個「附加」到現有的Java EE系統,並且不能在Azure上運行,所以我使用REST API。這是我試過到目前爲止:

String account = "myaccount"; 
    String key = "243fedfsdf23f4f"; 
    String protocol = "http"; 
    String storageConnectionString = String.format("DefaultEndpointsProtocol=%s;AccountName=%s;AccountKey=%s", protocol, account, key); 
    System.out.println(storageConnectionString);   

    URL url = new URL("https://mysite.azureweb.com/myfile.txt"); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

    if (conn.getResponseCode() != 200) { 
     throw new IOException(conn.getResponseMessage()); 
    } 

    // Buffer the result into a string 
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    StringBuilder sb = new StringBuilder(); 
    String line; 
    while ((line = rd.readLine()) != null) { 
     sb.append(line); 
    } 
    rd.close(); 
    conn.disconnect(); 

字符串可能需要一些的Base64編碼

更新

HTTP請求看起來像GET https://myAccount.blob.core.windows.net/myDir/myfile.txt HTTP/1.1 x-ms-date: Thu, 01 Oct 2015 12:56:11 GMT x-ms-version: 2015-02-21 Authorization: SharedKey myAccount:asdfkjsladjfsdf827fhwf298f924f92723dfh23f273f2h7h4f Host: myAccount.blob.core.windows.net

我 「只是」 需要打包到這個請求得到文件/mydir/myfile.txt

+0

您可能想要刪除已放入問題中的密鑰並更改/刪除Azure服務器上的密鑰值。 –

+0

私人blob存儲,所以我需要密鑰。這只是一個例子,關鍵要長得多 –

+0

好吧,我不知道,只是認爲這是關鍵,所以想到提醒你。 –

回答

2

有針對Azure存儲的兩種訪問類型爲。一個通過共享密鑰和其他一個通過共享訪問簽名

共享密鑰可以訪問整個存儲帳戶。每個存儲帳戶有兩個共享密鑰,它們都相同。通常你永遠不會把你的共享密鑰丟掉。通常,您只能在服務器端使用它們,而不能在客戶端的應用程序中使用它們。

你只想給一個單一的文件某人訪問。因此使用共享密鑰將是錯誤的解決方案。

共享訪問簽名爲您提供了創建(REST)請求的可能性,該請求僅限於某些文件或容器。您可以選擇寫入,讀取,刪除等權限,並定義訪問有效的時間範圍。對於共享訪問簽名,您有兩種選擇:a)ad-hoc和b)基於策略。臨時共享訪問簽名不能輕易撤銷(您可以刪除該文件或使您用來創建共享訪問簽名的共享密鑰無效)。通過刪除策略,可輕鬆吊銷基於策略的共享訪問簽名。

如果你不想使用Azure的SDK,您可以創建自己的共享訪問簽名。 如何構建他們在下面的鏈接解釋說:

Constructing a Service SAS

也有樣。

Service SAS Examples

你的文件存儲在一個BLOB。所以你必須使用BLOB服務。在示例頁面上,您可以找到以下BLOB示例。

signedstart=2013-08-16 
signedexpiry=2013-08-17 
signedresource=c 
signedpermissions=r 
signature=dD80ihBh5jfNpymO5Hg1IdiJIEvHcJpCMiCMnN/RnbI= 
signedidentifier=YWJjZGVmZw== 
signedversion=2013-08-15 
responsecontent-disposition=file; attachment 
responsecontent-type=binary 


StringToSign = r + \n 
       2013-08-16 + \n 
       2013-08-17 + \n 
       /myaccount/pictures + \n 
       YWJjZGVmZw== + \n 
       2013-08-15 + \n 
       + \n 
       file; attachment + \n 
       + \n 
       + \n 
       binary 


HMAC-SHA256(URL.Decode(UTF8.Encode(StringToSign))) = a39+YozJhGp6miujGymjRpN8tsrQfLo9Z3i8IRyIpnQ= 

最後,您將獲得您的REST請求的URL。

GET https://myaccount.blob.core.windows.net/pictures/profile.jpg?sv=2013-08-15&st=2013-08-16&se=2013-08-17&sr=c&sp=r&rscd=file;%20attachment&rsct=binary &sig=YWJjZGVmZw%3d%3d&sig=a39%2BYozJhGp6miujGymjRpN8tsrQfLo9Z3i8IRyIpnQ%3d HTTP/1.1 

看看這兩頁上的完整說明。

+0

好的!是的,我有一個共享密鑰,可以讓我訪問整個blob存儲。所以StringToSign +用base64解碼的共享密鑰給了我共享訪問簽名? –

+0

最後一個令牌(參數'sig')是base64編碼的。你用你的共享密鑰散列StringToSign。散列函數對二進制數據起作用,所以你必須用UTF8對你的StringToSign進行編碼(在散列之前)。散列之後,用base64 - >對參數sig的值進行編碼。 –

0

有一種簡單的方法可以通過使用Azure存儲SDK來生成用於在私有容器中獲取文件的SAS。

按照下面的示例代碼發電機密封的SAS鍵和格式化的網址:

String accountName = "<your_account_name>"; 
String accountKey = "<your_account_key>"; 
String containerName = "<your_private_container_name>"; 
String blobFileName = "<your_blob_file_name>"; 
String storageConnectionString = String.format("DefaultEndpointsProtocol=%s;AccountName=%s;AccountKey=%s", "https", accountName, accountKey); 
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString); 
CloudBlobClient blobClient = account.createCloudBlobClient(); 
CloudBlobContainer container = blobClient.getContainerReference(containerName); 
CloudBlockBlob blob = container.getBlockBlobReference(blobFileName); 
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy(); 
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 
calendar.setTime(new Date()); 
policy.setSharedAccessStartTime(calendar.getTime()); 
calendar.add(Calendar.HOUR, 1); 
policy.setSharedAccessExpiryTime(calendar.getTime()); 
policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ)); 
String sas = blob.generateSharedAccessSignature(policy, null); 
System.out.println(sas) 
String urlstr = String.format("https://%s.blob.core.windows.net/%s/%s?%s", accountName, containerName, blobFileName, sas); 
System.out.println(urlstr); 

有關詳細信息,您可以參考文檔https://msdn.microsoft.com/en-us/library/hh875756.aspx