I tried to follow the example on https://cloud.google.com/storage/docs/xml-api/post-object#policydocument.
And i am using Java servlet on the Compute Engine and want the user to broswe the servlet page and allow them to direct upload image to the Google Cloud Storage.
But the Google Cloud storage server returned: HTTP 400 Bad Request
// 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);