Hello All,
I am able to create signed URL, when I tried to upload Image/text file to
G-cloud it always return me *403-forbidden error*.
Any help will really appreciated, below is my code which create Signed URL
//Needed data members
*//Base URL*
private static final String BASE_URL = "https://storage.googleapis.com";
* // Bucket name I created on on console account*
private static final String BUCKET = "reacheme-dev";//"reachme-dev";
String contentType = "text/plain";
String fileNameText = "reachme.txt";
* // privatekey.p12 I get .p12 file from my G-console after creating my
google Service account, I kept this file in resource folder of AppEngine
(EndPoint)*
public static final String KEY_FILENAME = "privatekey.p12";
*//default password to read the file provided by google*
public static final String KEY_PASSWORD = "notasecret";
*//Method to create Signed URL*
public String getSignedUrl(final Context context, final String httpVerb,
final String path, final String fileName) throws Exception {
final long expiration = System.currentTimeMillis() + 1000 * 10;
mContext = context;
String file = "reachme.txt";
final String sign = signString("PUT\n\n" + contentType + "\n" +
expiration + "\n" + BUCKET + "/" +fileNameText);
return new StringBuilder(BASE_URL).append("/")
.append(BUCKET)
.append("/")
.append(fileNameText)
.append("?GoogleAccessId=")
.append(clientId())
.append("&Expires=")
.append(expiration)
.append("&Signature=")
.append(URLEncoder.encode(sign, "UTF-8")).toString();
}
*//Create Sign URL *
public String signString(String stringToSign) throws Exception {
// load key
PrivateKey key = loadKeyFromPkcs12(KEY_FILENAME,
KEY_PASSWORD.toCharArray());
// sign data
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(key);
signer.update(stringToSign.getBytes("UTF-8"));
byte[] rawSignature = signer.sign();
return new String(Base64.encodeBase64(rawSignature, false),
"UTF-8");
}
private static PrivateKey loadKeyFromPkcs12(String filename, char[]
password) throws Exception {
final FileInputStream fis = new FileInputStream(filename);
final KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(fis, password);
return (PrivateKey) ks.getKey("privatekey", password);
}
*// Asynch task to update data to G-storage*
private synchronized void uploadToGCS(final String uploadUrl) {
new AsyncTask<Message, Integer, Boolean>() {
private Message f_message;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Message... messages) {
final int code = uploadImageToGCS(uploadUrl, mByteData);
Log.d("###", "Response code: " + code);
if (code == HttpStatusCodes.STATUS_CODE_OK) {
//TODO Handle success
} else {
//TODO handle failure
}
return false;
}
protected void onPostExecute(Boolean result) {
}
}.execute();
}
*//Method to update data to G-storage*
public int uploadImageToGCS(final String uploadUrl, final byte[] data) {
try {
final URL url = new URL(uploadUrl);
byte[] hardByte = "ReachMe if you can!".getBytes();
final HttpsURLConnection connection = (HttpsURLConnection)
url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
final OutputStream out = new
BufferedOutputStream(connection.getOutputStream());
out.write(hardByte);
out.close();
final int responseCode = connection.getResponseCode();
Log.d("###fromGCS", "Service returned response code " +
responseCode);
return responseCode;
} catch (IOException e) {
e.printStackTrace();
}
return -1;
}
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/a46e9ee4-66b0-457e-b682-c96208ea925b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.