0
我試圖按照https://cloud.google.com/storage/docs/xml-api/post-object#policydocument上的示例進行操作。使用策略文檔無法正常工作的基於表單的上傳到Google雲端存儲
而且我正在計算引擎上使用Java servlet,並希望用戶瀏覽servlet頁面並允許他們將上傳圖像導向到Google雲端存儲。
但返回的谷歌雲存儲服務器:HTTP 400錯誤的請求
// Upload
String googleCloudStorageBucketFullPath = "http://" + m_bucketName + ".storage.googleapis.com";
String googleAccessIdString = "[email protected]";
String uploadObjectName = "";
String policyDocumentString =
"{" +
"\"expiration\": \"2017-06-16T11:11:11Z\"," +
"\"conditions\": " +
"[" +
"[\"starts-with\", \"$key\", \"" + uploadObjectName + "\" ]," +
"{\"acl\": \"bucket-owner-read\" }," +
"{\"bucket\": \"" + m_bucketName + "\"}," +
//"{\"success_action_redirect\": \"http://www.example.com/success_notification.html\" }," +
"[\"eq\", \"$Content-Type\", \"image/jpeg\" ]," +
"[\"content-length-range\", 0, 1000000]" + //1 MB max.
"]" +
"}";
byte[] signedBase64EncodedPolicyDocumentBytes = null;
String base64EncodedSignedBase64EncodedPolicyDocumentString = "";
//Create private key.
FileInputStream privateKeyInputStream = new FileInputStream(p12PKFullPath);
try
{
String privateKeyPassword = "notasecret";
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(privateKeyInputStream, privateKeyPassword.toCharArray());
//Sign the policy document using private key.
PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", privateKeyPassword.toCharArray());
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(base64EncodedPolicyDocumentString.getBytes());
signedBase64EncodedPolicyDocumentBytes = signature.sign();
}
catch(Exception ex)
{
out.write("<br>Exception=" + ex.getMessage() + "<br>");
}
finally
{
if(privateKeyInputStream != null)
{
privateKeyInputStream.close();
privateKeyInputStream = null;
}
}
base64EncodedSignedBase64EncodedPolicyDocumentString = new String(Base64.encodeBase64(signedBase64EncodedPolicyDocumentBytes));
//Create the html form
String htmlFormString =
"<form action=\"" + googleCloudStorageBucketFullPath +"\" method=\"post\" enctype=\"multipart/form-data\" accept-charset=\"UTF-8\">" +
"<input type=\"hidden\" name=\"key\" value=\"" + uploadObjectName + "\">" +
"<input type=\"hidden\" name=\"bucket\" value=\"" + m_bucketName + "\">" +
"<input type=\"hidden\" name=\"Content-Type\" value=\"image/jpeg\">" +
"<input type=\"hidden\" name=\"GoogleAccessId\" value=\"" + googleAccessIdString + "\">" +
"<input type=\"hidden\" name=\"acl\" value=\"bucket-owner-read\">" +
//"<input type=\"hidden\" name=\"success_action_redirect\" value=\"http://www.example.com/success_notification.html\">" +
"<input type=\"hidden\" name=\"policy\" value=\"" + base64EncodedPolicyDocumentString + "\">" +
"<input type=\"hidden\" name=\"signature\" value=\"" + base64EncodedSignedBase64EncodedPolicyDocumentString + "\">" +
"<input name=\"file\" type=\"file\">" +
"<input type=\"submit\" value=\"Upload\">" +
"</form>";
out.write("<br>signature=" + base64EncodedSignedBase64EncodedPolicyDocumentString + "<br>");
out.write(htmlFormString);