【问题标题】:Picture upload into Google Cloud Storage only contains whitespace图片上传到谷歌云存储只包含空格
【发布时间】:2016-07-07 13:43:03
【问题描述】:

我正在尝试将 Processing 中的图片上传到 Google Cloud Storage,但是当我查看存储空间时,我只有一张空白图片。

我正在使用“HTTP 请求处理”库来上传图像。

这是以下代码(已更新):

  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++;
  }

来自 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="
  }

这是我到目前为止所遵循的教程:https://cloud.google.com/storage/docs/json_api/v1/how-tos/simple-upload#sending_a_simple_upload_request

还有一个提示,我必须给出图片的内容长度(它的字节数)。可能是因为这个我在存储中只收到了一个空白图像吗?如果是,我怎样才能自动读取图像的字节? The result of the uploaded pic so far

【问题讨论】:

  • 我无法重现此问题。您确定此白色图像的原因了吗?下面提供的任何答案对此有帮助吗?
  • @Nicholas 抱歉,没有看到您的评论。不,很遗憾,我转而使用 Python 并使用 Google 的 gsutil 工具,然后就可以了。

标签: processing google-cloud-storage google-cloud-endpoints google-cloud-platform


【解决方案1】:

您可以使用File#length() function获取文件的大小:

返回此抽象路径名表示的文件的长度。如果此路径名表示目录,则返回值未指定。

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

【讨论】:

  • 从中获取长度,但这不是故障,存储中仍然没有图片
  • @SeGa 你真的在设置标题吗?您能否将更新后的代码发布为minimal reproducible example?
  • 当然,它现在在问题中更新。并且文件本身具有正确的文件大小,它只显示带有灰色边框的白色图像。我在问题的末尾将图片作为链接发布。代码应该可以工作,只有令牌已过期,所以如果需要,我必须生成一个新的。
  • 我无法重现此问题。您确定此白色图像的原因了吗?
  • @Nicholas 您可能想对问题本身发表评论,以便 OP 实际看到它。
【解决方案2】:

我建议您在上传文件时使用像 Multipart Entity builder 这样的包。这是sample。这里不需要内容长度。并且要读取相同的内容,您可以使用 InputStreamReader

【讨论】:

  • Google APi 还有一个分段上传选项,我会尝试使用它
猜你喜欢
  • 2015-03-01
  • 2016-04-12
  • 2022-10-30
  • 1970-01-01
  • 2018-10-02
  • 2020-11-15
  • 2021-06-09
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多