【问题标题】:Generate gcs signed URL to upload large files生成 gcs 签名 URL 以上传大文件
【发布时间】:2021-02-20 23:33:13
【问题描述】:

我一直在尝试通过生成签名 URL 来上传大文件。这是我为生成签名 URL 所遵循的文档:https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#code-samples

它对于 100 MB 的文件运行良好,但一旦文件大小达到 1 GB,即使在增加过期时间后 curl 命令也开始超时。我尝试在这里查看答案:https://stackoverflow.com/a/63789297/7466551,但我仍然无法让 URL 正常工作以上传 URL。

我正在使用这个命令来上传文件: curl -X POST -H 'x-goog-resumable: start' --upload-file file-name 'pre_signed_google_url'。我正在添加 'x-goog-resumable: start' 标头,因为我将 "x-goog-resumable", "start" 标头作为我的代码的一部分来生成 URL。

如果我需要做任何额外的事情来生成上传大文件的 URL,有人可以告诉我吗?

【问题讨论】:

  • x-goog-resumable:start 表示您正在尝试初始化可恢复上传。这样的调用应该是 POST,而不是 put。看看这个指南:cloud.google.com/storage/docs/…
  • 感谢您指出我命令中的错误。将 curl 命令更改为 POST 后,我能够使链接正常工作。我正在更新我的问题。此外,我还发现了这篇文章:medium.com/google-cloud/…,它解释了为可恢复上传创建签名 URL。我能够结合使用我在问题中提供的 StackOverflow 链接和中篇文章来提出我正在寻找的解决方案。我会尽快发布我的答案。

标签: java google-cloud-storage pre-signed-url


【解决方案1】:

回答我自己的问题,因为我必须使用两个不同的来源才能找到解决方案。

这里的java代码上面:https://stackoverflow.com/a/63789297/7466551,我这里参考了中篇文章:https://medium.com/google-cloud/google-cloud-storage-signedurl-resumable-upload-with-curl-74f99e41f0a2

因此,您需要在 StackOverflow 答案之上添加以下代码行来获取可恢复上传的签名 URL:

// Open a HTTP connection and add header
URL obj = new URL(url.toString());
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("x-goog-resumable", "start");
connection.setDoOutput(true);

// Connect to the URL and post headers
DataOutputStream writer = new DataOutputStream(
        connection.getOutputStream());
writer.writeBytes("");
writer.flush();
writer.close();

// Checking the responseCode to
if (connection.getResponseCode() == connection.HTTP_CREATED) {
    connection.disconnect();
    System.out.println("Location: " + connection.getHeaderField("Location"));
}
else {
    // Throw error
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = reader.readLine()) != null) {
        response.append(inputLine);
    }
    reader.close();
    String errorMessage = response.toString();
    connection.disconnect();
    throw new IOException(errorMessage);

}

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2019-01-30
    • 2020-09-09
    • 2021-11-22
    • 2019-05-01
    • 2020-09-06
    • 1970-01-01
    • 2022-08-20
    • 2015-02-05
    相关资源
    最近更新 更多