Last active
August 28, 2019 14:30
-
-
Save salrashid123/0dc2fb49221f4871dde7582635a5e93a to your computer and use it in GitHub Desktop.
GCS SignedURL Resumable upload in java and curl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import com.google.auth.oauth2.ServiceAccountCredentials; | |
import java.net.URL; | |
import java.io.FileInputStream; | |
import java.net.URLEncoder; | |
import com.google.api.client.util.Base64; | |
import java.net.HttpURLConnection; | |
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.DataOutputStream; | |
public class TestApp { | |
public static void main(String[] args) { | |
TestApp tc = new TestApp(); | |
} | |
public TestApp() { | |
try | |
{ | |
ServiceAccountCredentials creds = ServiceAccountCredentials.fromStream( | |
new FileInputStream("your_svc_account.json")); | |
String BUCKET_NAME = "your_project"; | |
String OBJECT_NAME = "file.txt"; | |
String SERVICE_ACCOUNT_EMAIL = "svc-2-429@your_project.iam.gserviceaccount.com"; | |
String verb = "POST"; | |
long expiration = System.currentTimeMillis()/1000 + 60; | |
String Canonicalized_Extension_Headers = "x-goog-resumable:start"; | |
String content_type = "text/plain"; | |
byte[] sr = creds.sign( (verb + "\n\n"+ content_type + "\n" + expiration + "\n" + Canonicalized_Extension_Headers + | |
"\n" + "/" + BUCKET_NAME + "/" + OBJECT_NAME ).getBytes() ); | |
String url_signature = new String(Base64.encodeBase64( sr )); | |
String signed_url = "https://storage.googleapis.com/" + BUCKET_NAME + "/" + OBJECT_NAME + | |
"?GoogleAccessId=" + SERVICE_ACCOUNT_EMAIL + | |
"&Expires=" + expiration + | |
"&Signature=" + URLEncoder.encode(url_signature, "UTF-8"); | |
System.out.println(signed_url); | |
/* | |
URL obj = new URL(signed_url); | |
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("content-type", "text/plain"); | |
con.setRequestProperty("x-goog-resumable", "start"); | |
con.setDoOutput(true); | |
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | |
wr.writeBytes(""); | |
wr.flush(); | |
wr.close(); | |
int responseCode = con.getResponseCode(); | |
//System.out.println("Response Code : " + responseCode); | |
if (responseCode == con.HTTP_CREATED) | |
System.out.println("Locaiton: " + con.getHeaderField("Location")); | |
else { | |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
String inputLine; | |
StringBuffer response = new StringBuffer(); | |
while ((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
System.out.println(response.toString()); | |
System.exit(1); | |
} | |
con.disconnect(); | |
*/ | |
} | |
catch (Exception ex) { | |
System.out.println("Error: " + ex); | |
} | |
} | |
} |
I'm not sure...the upload for mulit-part stuff is dependent on the client you're using ..i just used HttpURLConnection
but you can use anyother one. The core bit about the signedURL and the protocol is sparate from the http client that manages and does the upload (the latter is upto you)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
when we upload a text file. the contents of text file are having metadata i.e.., am using postman client with form data.
what could be the reason for that.
--A23ieKkw1233edldA
Content-Disposition: form-data; name=""; filename="aditya.txt"
Content-Type: text/plain
gsajd
--A23ieKkw1233edldA--