用于GET文件的私有blob存储URL的格式。 Rest API

时间:2015-10-01 07:26:35

标签: java rest azure

Microsoft表示获取Blob只是正常的http get 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

中的文件

2 个答案:

答案 0 :(得分:2)

Azure存储有两种访问类型。一个通过共享密钥,另一个通过共享访问签名

共享密钥可授予对整个存储帐户的访问权限。每个存储帐户,您有两个共享密钥,它们是相同的。通常,您永远不会放弃共享密钥。通常,您只在服务器端使用它们,而不是在客户端的应用程序中使用它们。

您只想让某人访问单个文件。因此使用共享密钥将是错误的解决方案。

共享访问签名使您可以创建(REST)请求,该请求仅限于某些文件或容器。您可以选择写入,读取,删除等权限。然后定义访问有效的时间范围。对于共享访问签名,您有两种选择:a)ad-hoc和b)基于策略。 Ad-hoc共享访问签名不能轻易撤销(您可以删除该文件或使用于创建共享访问签名的共享密钥无效)。通过删除策略可以轻松撤消基于策略的共享访问签名。

如果您不想使用Azure SDK,则可以创建自己的共享访问签名。 如何构建它们在以下链接中解释:

Constructing a Service SAS

还有样品。

Service SAS Examples

您的文件存储在BLOB中。所以你必须使用服务BLOB。在样本页面上,您可以找到以下BLOB样本。

1234-4321/ABCD

最后,您将获得REST请求的URL。

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=

有关完整说明,请查看两页。

答案 1 :(得分:0)

有一种简单的方法可以使用Azure Storage SDK生成用于在私有容器中获取文件的SAS。

按照以下示例代码生成SAS密钥并格式化URL:

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