Google Cloud Storage Java XML API chunk by uploading chunk file
I am using Java Cloud Storage Java XML API to upload a file from a client to a Google Cloud Storage bucket. The file uploads successfully, but before uploading it, I read the entire file into a buffer, so I have to allocate a buffer up to the size of the file.
How do I make the chunk when the chunk is loaded so that I don't have to immediately read the entire content into the buffer? Below is the code snippet that I used. What changes need to be made for this so that I can make the chunk using chunk upload?
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();
+3
source to share
1 answer
With the XML API, you can either implement resumable downloads (sorry, didn't find any code samples), or use a composition of objects that's easier to start with:
- split your data locally into as many blocks as required
- Loading each fragment as a separate object
- compose your final object
- remove any temporary objects
+1
source to share