Google云存储Java XML API逐块文件上传
我正在使用谷歌云存储JavaXMLAPI将一个文件从客户端上传到谷歌云存储桶中。文件上传成功,但我在上传之前将整个文件读入缓冲区,因此我不得不分配一个与文件大小一样大的缓冲区 如何逐块上传,这样我就不需要一次将整个文件内容读入缓冲区?下面是我使用的一段代码。为了让我可以一块一块上传,需要对它做哪些修改Google云存储Java XML API逐块文件上传,java,google-cloud-storage,Java,Google Cloud Storage,我正在使用谷歌云存储JavaXMLAPI将一个文件从客户端上传到谷歌云存储桶中。文件上传成功,但我在上传之前将整个文件读入缓冲区,因此我不得不分配一个与文件大小一样大的缓冲区 如何逐块上传,这样我就不需要一次将整个文件内容读入缓冲区?下面是我使用的一段代码。为了让我可以一块一块上传,需要对它做哪些修改 httpTransport = GoogleNetHttpTransport.newTrustedTransport(); String p12Content = Files.readFirs
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();
使用XML API,您可以实现(抱歉,没有找到任何代码示例)或使用,这可能更容易开始:
- 根据需要将数据本地划分为尽可能多的块
- 将每个区块作为不同的对象上载
- 组成你的最终目标
- 删除任何临时对象