Java
Chilkat2 Java
Initiate Resumable Upload Session
See more Google Cloud Storage Examples
Initiate a Google Cloud Storage resumable upload session..Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Http http = new Http();
JsonObject jsonToken = new JsonObject();
try {
jsonToken.loadFile("qa_data/tokens/googleCloudStorage.json");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject jsonMetaData = new JsonObject();
jsonMetaData.updateString("contentType", "image/jpeg");
// Adds the "Authorization: Bearer <access_token>" header..
http.setAuthToken(jsonToken.stringOf("access_token"));
http.setUrlVar("bucket_name", "chilkat-bucket-b");
http.setUrlVar("object_name", "penguins2.jpg");
String url = "https://storage.googleapis.com/upload/storage/v1/b/{$bucket_name}/o?uploadType=resumable&name={$object_name}";
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", url, jsonMetaData, "application/json", resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
int statusCode = resp.getStatusCode();
System.out.println("response status code = " + statusCode);
String sessionUrl = "";
if (statusCode != 200) {
System.out.println(resp.getBodyStr());
} else {
// The session URL will be used to upload the file in chunks, in subsequent HTTP POSTs...
sessionUrl = resp.getHeaderField("Location");
System.out.println("Session URL = " + sessionUrl);
}
}
}