1

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

jterrace
  • 64,866
  • 22
  • 157
  • 202
damien marchand
  • 864
  • 1
  • 13
  • 30
  • What is your PUT request trying to do? Also, any reason you're using direct HTTP instead of the [google-api-java-client](https://code.google.com/p/google-api-java-client/) ? – jterrace Dec 18 '14 at 16:36
  • I use this PUT request with my token and file size to know if the file is totally uploaded. Is there a function in the google-api-java-client to check a resumable upload ? – damien marchand Dec 18 '14 at 17:14
  • 1
    Yes, the Java client handles resumable uploads for you. See the comment about `setDirectUploadEnabled` in [the sample](https://code.google.com/p/google-api-java-client/source/browse/storage-cmdline-sample/src/main/java/com/google/api/services/samples/storage/cmdline/StorageSample.java?repo=samples). – jterrace Dec 18 '14 at 19:21
  • I can see that `getObject.executeMediaAndDownloadTo(out);` download the object. Does an other solution exist ? Just for getting size information. – damien marchand Dec 19 '14 at 09:18
  • Yes, see getObjectMetadata in the sample above. – jterrace Dec 19 '14 at 14:36
  • I tried to use getObjectMetadata. It works fine but only if the file has been successfully uploaded. – damien marchand Dec 21 '14 at 23:58
  • If it doesn't work, then you know it's not successfully uploaded... You can just use a try block. You either get the metadata or you know it was not uploaded properly... – Ying Li Apr 20 '15 at 19:31

0 Answers0