1
votes

I'm trying to upload a picture from Processing into the Google Cloud Storage, but when I look into the storage I have only a whitespace image.

I'm using the "HTTP Request for Processing" library to upload the images.

Here's the following code(updated):

  import http.requests.*;

  int number = 0;

  void setup(){
    size(640,480);
  }

  void draw() {

    if(number < 1) {
      PostRequest post = new PostRequest("https://www.googleapis.com/upload/storage/v1/b/hs_test01/o?uploadType=media&name=person0.jpg&access_token=ya29.Ci8aA6-n96angt2d9mIyDUL2_4bHP5ybP-5deHb3zbnpbwFgwAGD5vx2LlydFwCjBA");
      File f = new File(sketchPath("data/test.jpg"));
      post.addFile("image/jpeg", f); 
      long size = f.length();
      println(size);
      post.addHeader("Content-Type","image/jpeg");
      post.addHeader("Content-Length","163155");
      post.send();
      println("Reponse Content: " + post.getContent());
      println("Reponse Content-Length Header: " + post.getHeader("Content-Length"));
    }
    number++;
  }

Response from the API:

  Reponse Content: {
    "kind": "storage#object",
    "id": "hs_test01/person0.jpg/1468064800417000",
    "selfLink": "https://www.googleapis.com/storage/v1/b/hs_test01/o/person0.jpg",
    "name": "person0.jpg",
    "bucket": "hs_test01",
    "generation": "1468064800417000",
    "metageneration": "1",
    "contentType": "image/jpeg",
    "timeCreated": "2016-07-09T11:46:40.413Z",
    "updated": "2016-07-09T11:46:40.413Z",
    "storageClass": "STANDARD",
    "size": "1101980",
    "md5Hash": "msH3vhzgwNKPd5aRXfy/vA==",
    "mediaLink": "https://www.googleapis.com/download/storage/v1/b/hs_test01/o/person0.jpg?generation=1468064800417000&alt=media",
    "crc32c": "nGKW7A==",
    "etag": "COjJvsen5s0CEAE="
  }

Here is the tutorial I have followed so far: https://cloud.google.com/storage/docs/json_api/v1/how-tos/simple-upload#sending_a_simple_upload_request

There is also the hint, that I have to give the content-length of the picture (the bytes of it). Could it be because of this that I only receive a whitespace image in the storage? If yes, how can I read the bytes automatically of the image? The result of the uploaded pic so far

2
I have not been able to reproduce this issue. Have you identified the cause of this white image? Have any answers provided below been helpful to this?Nicholas
@Nicholas Sorry, didnt saw your comment. No sadly no, i went over to Python and used the gsutil tool from Google and that worked then.SeGa

2 Answers

0
votes

You can get the size of the file using the File#length() function:

Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

File f = new File(sketchPath("data/test.jpg"));
long size = f.length();
0
votes

I would recommend you to use a package like Multipart Entity builder in case of file uploads. Here is a sample. Content-length is not needed here. And to read the same, you can use InputStreamReader