我一直在尝试通过生成签名的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上传大文件?
发布于 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:
// 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);
}
https://stackoverflow.com/questions/66297644
复制相似问题