I use Google Cloud Storage and I want to know if my file upload is a success.
I use resumable uploads.
Google doc : google doc link
I can upload file with a session url :
byte[] byteArray = Files.readAllBytes(path);
long byteCount = byteArray.length;
//UrlForUp is my session_uri for resumable upload
String urlForUp = ObjectManager.getUrl(bucketName, objectName, properties, "image/gif", byteCount);
HttpURLConnection connection;
URL url = new URL(urlForUp);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Length", Long.toString(byteCount));
connection.connect();
OutputStream os = connection.getOutputStream();
//send file
os.write(byteArray);
os.flush();
os.close();
Map<String, List<String>> headerData;
headerData = connection.getHeaderFields();
Set listKeys = headerData.keySet();
Iterator iterator = listKeys.iterator();
System.out.println("UPLOAD RESPONSE CODE---------------------------------");
System.out.println(connection.getResponseCode());
System.out.println("UPLOAD RESPONSE HEADER---------------------------------");
while (iterator.hasNext()) {
Object key = iterator.next();
if (key != null) {
List<String> values = headerData.get(key);
for (int i = 0; i < values.size(); i++) {
if (values.get(i) != null) {
System.out.println(key.toString() + " : " + values.get(i));
}
}
}
}
Google send me 200 and my file hass been sent.
Google response :
UPLOAD RESPONSE CODE---------------------------------
200
UPLOAD RESPONSE HEADER---------------------------------
ETag : CICQ7oauzcICEAE=
Date : Wed, 17 Dec 2014 15:10:38 GMT
Vary : X-Origin
Vary : Origin
Content-Length : 810
Expires : Fri, 01 Jan 1990 00:00:00 GMT
Alternate-Protocol : 443:quic,p=0.02
Content-Type : application/json; charset=UTF-8
Server : UploadServer ("Built on Dec 2 2014 12:42:30 (1417552950)")
Pragma : no-cache
Cache-Control : no-cache, no-store, max-age=0, must-revalidate
Then I want to check this with a request.
I make a request like this : ( from google cloud doc )
PUT {session_uri} HTTP/1.1
Authorization: Bearer your_auth_token
Content-Length: 0
Content-Range: bytes */2000000
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
Credential credential = createCredential(properties);
credential.refreshToken();
connection.setRequestProperty("Authorization", "Bearer " + credential.getAccessToken());
connection.setRequestProperty("Content-Length", "0");
connection.setRequestProperty("Content-Range","bytes */"+fileSize);
connection.connect();
System.out.println("CONNECTION MESSAGE-----------------------------------------");
System.out.println(connection.getResponseMessage());
Map<String, List<String>> headerData;
headerData = connection.getHeaderFields();
Set listKeys = headerData.keySet();
Iterator iterator = listKeys.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
if (key != null) {
List<String> values = headerData.get(key);
for (int i = 0; i < values.size(); i++) {
if (values.get(i) != null) {
System.out.println(key.toString() + " : " + values.get(i));
}
}
}
}
System.out.println(connection.getResponseCode());
Message google send me :
CONNECTION MESSAGE-----------------------------------------
Length Required
Date : Thu, 18 Dec 2014 12:12:29 GMT
Content-Length : 1428
Content-Type : text/html; charset=UTF-8
Server : GFE/2.0
411
I don't understand.
Google says me to set Content-Length to 0 and send me :
Length Required