我正在使用Google云端存储Java XML API将文件从客户端上传到Google云端存储分区。文件上传成功但我在上传之前将整个文件读入缓冲区,因此我不得不分配一个与文件大小一样大的缓冲区。
如何通过块上传来执行块,这样我就不需要立即将整个文件内容读入缓冲区了?以下是我用过的一段代码。对此进行了哪些修改,以便我可以通过块上传来完成一大块工作?
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
String p12Content = Files.readFirstLine(new File("key.p12"), Charset.defaultCharset());
Preconditions.checkArgument(!p12Content.startsWith("Please"), p12Content);
//[START snippet]
// Build a service account credential.
Path path = Paths.get(MyFilePath);
byte[] byteArray = java.nio.file.Files.readAllBytes(path);
HttpContent contentsend = new ByteArrayContent("text/plain", byteArray );
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
.setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
.build();
// Set up and execute a Google Cloud Storage request.
String URI = "https://storage.googleapis.com/" + BUCKET_NAME + "/myfile";
HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
GenericUrl url = new GenericUrl(URI);
HttpRequest request = requestFactory.buildPutRequest(url,contentsend);
HttpResponse response = request.execute();
答案 0 :(得分:1)
使用XML API,您可以实施resumable uploads(抱歉,没有找到任何代码示例)或使用object composition,这可能更容易入手: