首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >生成gcs签名URL以上传大文件

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

Stack Overflow用户
提问于 2021-02-21 07:33:14
回答 1查看 250关注 0票数 0

我一直在尝试通过生成签名的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,但我仍然无法让网址上传网址。

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

有没有人可以让我知道,如果我需要做任何额外的事情,以生成URL上传大文件?

EN

回答 1

Stack Overflow用户

发布于 2021-02-25 08:57:02

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

在这里的java代码之上:https://stackoverflow.com/a/63789297/7466551,我在这里引用了medium文章:https://medium.com/google-cloud/google-cloud-storage-signedurl-resumable-upload-with-curl-74f99e41f0a2

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

代码语言:javascript
运行
复制
// 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);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66297644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档