Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GET文件专用blob存储url的格式。RESTAPI_Java_Rest_Azure - Fatal编程技术网

Java GET文件专用blob存储url的格式。RESTAPI

Java GET文件专用blob存储url的格式。RESTAPI,java,rest,azure,Java,Rest,Azure,微软表示,获取Blob只是一个普通的httpgethttps://myaccount.blob.core.windows.net/mycontainer/myblob。但是,当我有帐户+共享密钥时,如何格式化字符串 我知道有一个Azure SDK,但我正在为现有JavaEE系统创建一个“附加组件”,无法在Azure中运行,所以我使用的是RESTAPI。这就是我迄今为止所尝试的: String account = "myaccount"; String key = "243fedfsdf2

微软表示,获取Blob只是一个普通的httpget
https://myaccount.blob.core.windows.net/mycontainer/myblob
。但是,当我有帐户+共享密钥时,如何格式化字符串

我知道有一个Azure SDK,但我正在为现有JavaEE系统创建一个“附加组件”,无法在Azure中运行,所以我使用的是RESTAPI。这就是我迄今为止所尝试的:

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请求看起来像
GEThttps://myAccount.blob.core.windows.net/myDir/myfile.txt HTTP/1.1
x-ms-date:Thu,2015年10月1日12:56:11 GMT
x-ms-version:2015-02-21
授权:SharedKey myAccount:asdfkjsladjfsdf827fhwf298f924f92723dfh23f273f2h7h4f
主机:myAccount.blob.core.windows.net


我“只是”需要将其打包到请求中,以获取Azure存储中的文件。一个通过共享密钥,另一个通过共享访问签名

共享密钥允许访问整个存储帐户。每个存储帐户都有两个共享密钥,它们都是相等的。通常情况下,您从不将共享密钥送出。通常,您只在服务器端使用它们,而不在客户端的应用程序中使用它们

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

共享访问签名允许您创建(REST)请求,该请求仅限于某些文件或容器。您可以选择写入、读取、删除等权限,并定义访问有效的时间范围。对于共享访问签名,您有两个选项:a)临时和b)基于策略。临时共享访问签名无法轻松撤销(您可以删除文件或使用于创建共享访问签名的共享密钥无效)。通过删除策略,可以轻松撤销基于策略的共享访问签名

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

还有一些样品。

您的文件存储在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 = "<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);
String accountName=“”;
字符串accountKey=“”;
字符串containerName=“”;
字符串blobFileName=“”;
String-storageConnectionString=String.format(“DefaultEndpointsProtocol=%s;AccountName=%s;AccountKey=%s”、“https”、AccountName、AccountKey”);
CloudStorageAccount=CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient=account.createCloudBlobClient();
CloudBlobContainer container=blobClient.getContainerReference(containerName);
CloudBlockBlob blob=container.getBlockBlobReference(blobFileName);
SharedAccessBlobPolicy策略=新的SharedAccessBlobPolicy();
GregorianCalendar日历=新的GregorianCalendar(TimeZone.getTimeZone(“UTC”));
calendar.setTime(新日期());
policy.setSharedAccessStartTime(calendar.getTime());
calendar.add(calendar.HOUR,1);
policy.setSharedAccessExpirTime(calendar.getTime());
setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
字符串sas=blob.generateSharedAccessSignature(策略,null);
系统输出打印LN(sas)
字符串urlstr=String.format(“https://%s.blob.core.windows.net/%s/%s?%s”,accountName,containerName,blobFileName,sas);
System.out.println(urlstr);

有关详细信息,请参阅文档。

您可能希望删除问题中的密钥,并更改/删除azure server.Private blob storage上的密钥值,因此我需要该密钥。这只是一个例子,密钥很长,我不知道,只是认为这就是密钥,所以想提醒你。你想使用两个访问密钥中的一个还是使用共享访问签名?@PeterKirchner我已经更新了问题。我想使用共享密钥签名一个!是的,我有一个共享密钥,可以访问整个blob存储。因此,使用base64解码的StringToSign+共享密钥为我提供了共享访问签名?最后的令牌(参数“sig”)是base64编码的。使用共享密钥散列StringToSign。散列函数处理二进制数据,因此必须使用UTF8(在散列之前)对StringToSign进行编码。散列后,使用参数sig的值base64-->对二进制数据进行编码。