Java 将对象放入Azure存储时出现身份验证错误
我正在使用java对Azure进行RESTAPI调用,将一个对象放入其存储。 上周我成功地做到了这一点,但由于某些原因,现在它正在发挥作用。 错误消息如下所示: 身份验证失败 服务器无法对请求进行身份验证。确保包括签名在内的授权标头的值格式正确。请求ID:a15f2626-0001-004f-778c-f34383000000时间:2017-07-02T23:43:20.2826278Z 请求中的日期标头不正确。Java 将对象放入Azure存储时出现身份验证错误,java,azure,azure-storage,azure-storage-blobs,azure-authentication,Java,Azure,Azure Storage,Azure Storage Blobs,Azure Authentication,我正在使用java对Azure进行RESTAPI调用,将一个对象放入其存储。 上周我成功地做到了这一点,但由于某些原因,现在它正在发挥作用。 错误消息如下所示: 身份验证失败 服务器无法对请求进行身份验证。确保包括签名在内的授权标头的值格式正确。请求ID:a15f2626-0001-004f-778c-f34383000000时间:2017-07-02T23:43:20.2826278Z 请求中的日期标头不正确。 如果尚未解决此身份验证错误,则可以参考以下代码: public class Pu
如果尚未解决此身份验证错误,则可以参考以下代码:
public class PutTest {
private static final String account = <your account name>;
private static final String key = <your account key>;
public static void main(String args[]) throws Exception {
File file = new File(<your file path>);
FileInputStream inputStream = new FileInputStream(<your file path>);
String urlString = "https://" + account + ".blob.core.windows.net/<your container>/<your file name e.g:test.txt>";
HttpURLConnection connection = (HttpURLConnection) (new URL(urlString)).openConnection();
getFileRequest(connection, account, key, file.length());
// connection.connect();
connection.setDoInput(true);
connection.setDoOutput(true);
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
OutputStream out = connection.getOutputStream();
out.write(buffer);
out.flush();
out.close();
System.out.println("Response message : " + connection.getResponseMessage());
System.out.println("Response code : " + connection.getResponseCode());
BufferedReader br = null;
if (connection.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}
public static void getFileRequest(HttpURLConnection request, String account, String key, long length)
throws Exception {
SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = fmt.format(Calendar.getInstance().getTime()) + " GMT";
String stringToSign = "PUT\n" + "\n" // content encoding
+ "\n" // content language
+length+"\n"// content length
+ "\n" // content md5
+"\n" // content type
+ "\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "x-ms-blob-type:BlockBlob" + "\n"
+ "x-ms-date:" + date + "\n"
+ "x-ms-version:2015-02-21"+"\n" // headers
+ "/" + account + request.getURL().getPath(); // resources
System.out.println("stringToSign : " + stringToSign);
String auth = getAuthenticationString(stringToSign);
request.setRequestMethod("PUT");
request.setRequestProperty("x-ms-blob-type", "BlockBlob");
request.setRequestProperty("x-ms-date", date);
request.setRequestProperty("x-ms-version", "2015-02-21");
request.setRequestProperty("Authorization", auth);
request.setRequestProperty("Content-Length", String.valueOf(length));
}
private static String getAuthenticationString(String stringToSign) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(Base64.decode(key), "HmacSHA256"));
String authKey = new String(Base64.encode(mac.doFinal(stringToSign.getBytes("UTF-8"))));
String auth = "SharedKey " + account + ":" + authKey;
return auth;
}
}
希望有帮助。为什么不直接使用Azure Storage SDK for Java?这比自己编写RESTAPI调用容易得多。我可能无法使用SDK。你能看到我的代码中有什么问题吗?非常感谢,我已经解决了这个问题。我错误地使用了不同的Base64包,尽管它们有相同的名称…它们都被称为Base64。