我尝试按照https://cloud.google.com/storage/docs/xml-api/post-object#policydocument上的示例进行操作。
我正在Compute Engine上使用Java servlet,并希望用户浏览servlet页面并允许他们将上传图像定向到Google云端存储。
但Google Cloud存储服务器已返回:HTTP 400 Bad Request
// Upload
String googleCloudStorageBucketFullPath = "http://" + m_bucketName + ".storage.googleapis.com";
String googleAccessIdString = "XXXXXXX-compute@developer.gserviceaccount.com";
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);
答案 0 :(得分:0)
最后,我发现问题是上传的对象名称丢失,但在https://cloud.google.com/storage/docs/xml-api/post-object上,没有提供对象名称,因此我不知道为什么示例代码不会被删除。 t提供上传对象名称并工作..