【发布时间】:2015-10-06 08:47:52
【问题描述】:
Microsoft 声明获取 Blob 只是普通的 http 获取 https://myaccount.blob.core.windows.net/mycontainer/myblob。但是当我有一个帐户+共享密钥时如何格式化字符串?
我知道有一个 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 中的文件
【问题讨论】:
-
您可能想删除您在问题中输入的键并更改/删除 azure 服务器上的键值。
-
私有 blob 存储,所以我需要密钥。这只是一个例子,关键要长得多
-
好吧,我不知道,只是觉得这是关键,所以想提醒你。
-
您要使用两个访问密钥之一还是要使用共享访问签名?
-
@PeterKirchner 我已经更新了这个问题。我想使用共享密钥签名