GET文件的私有blob存储url的格式。 Rest API

微软表示,获取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中的文件

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

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

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

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

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

构建服务SAS

还有样品。

服务SAS示例

您的文件存储在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 

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

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

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

 String accountName = ""; String accountKey = ""; String containerName = ""; String blobFileName = ""; 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 。