微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

生成 gcs 签名 URL 以上传大文件

如何解决生成 gcs 签名 URL 以上传大文件

我一直在尝试通过生成签名 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,有人可以告诉我吗?

解决方法

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

在上面的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);

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。