Issue
I want to upload a file in a bucket on my Google Cloud Storage using Java. Due to my project's restriction, I muse use cURL. Google's docs provided this template of a cURL upload command:
curl -X POST --data-binary @OBJECT_LOCATION \
-H "Authorization: Bearer OAUTH2_TOKEN" \
-H "Content-Type: OBJECT_CONTENT_TYPE" \
"https://storage.googleapis.com/upload/storage/v1/b/BUCKET_NAME/o?uploadType=media&name=OBJECT_NAME"
I tried this :
String tokenAuth = "TOKEN_HERE";
String filePth= "C:/DL API/vecteezy_isolated-tree-high-res_17219851_371.png";
ProcessBuilder pb = new ProcessBuilder(
"curl",
"-X POST",
"-data-binary "+filePth+" ",
"-H \"Authorization: Bearer "+tokenAuth+"\"",
"-H \"Content-Type: image/png\"",
"https://storage.googleapis.com/upload/storage/v1/b/BUCKET_NAME/o?uploadType=media&name=test.png");
pb.redirectErrorStream(true);
try{
Process p = pb.start();
InputStream is = p.getInputStream();
String line;
BufferedInputStream bis = new BufferedInputStream(is);
System.out.println("youp "+bis);
}
catch(IOException e){
System.err.println("ERROR" + e.getMessage());
}
I used a buffer reader to get the content of the cURL output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0curl: (6) Could not resolve host: Bearer
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: TOKEN HERE
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0curl: (6) Could not resolve host: image
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 124 100 54 100 70 201 260 --:--:-- --:--:-- --:--:-- 464
<html><title>Error 400 (Bad Request)!!1</title></html>
Solution
Your problem is your usage of ProcessBuilder
. Everything with a space between it (except for quoted strings) is a separate argument. "-X POST" is two arguments, not one. You also don't need to escape quotes. You're not using a shell. For example, this is wrong:
ProcessBuilder pb = new ProcessBuilder("curl",
"-X POST", // Should've been two strings, "-X" and "POST"
"-H \"Foo: Bar\""); // Likewise, should be "-H" and "Foo: Bar".
Try something more like this:
ProcessBuilder pb = new ProcessBuilder("curl",
"-X", "POST",
"-data-binary", filePath,
"-H", "Authorization: Bearer "+tokenAuth,
"-H", "Content-Type: image/png",
"https://storage.googleapis.com/upload/storage/v1/b/BUCKET_NAME/o?"+
"uploadType=media&name=test.png");
Also, you'll find it easier to read the logs and debug your use of cURL if you disable the progress bar updates (that's the "-s" flag) and also turn on verbose logging so you can see the request and make sure it's correct (that's the "-v" flag).
Answered By - Brandon Yarbrough Answer Checked By - Gilberto Lyons (WPSolving Admin)