Google Cloud Storage Java XML API chunk by chunk file upload
-
I'm using Google Cloud Storage Java XML API to upload a file from client into the Google Cloud Storage bucket. The file is getting uploaded successfully but I'm reading the entire file into the buffer before uploading it, thus I'm forced to allocate a buffer which is as big as size of the file. How to do a chunk by chunk upload so that I don't need to read entire file content into the buffer at once? Following is the piece of code I've used. What are the modifications to be done on this so that I could do a chunk by 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();
-
Answer:
With XML API you can either implement https://cloud.google.com/storage/docs/concepts-techniques#resumable (sorry, didn't find any code samples) or use https://cloud.google.com/storage/docs/composite-objects, which might be easier to start with: divide your data locally into as many chunks as required upload each chunk as a distinct object compose your final object delete any temporary objects
Karthic Rao at Stack Overflow Visit the source
Related Q & A:
- How to cancel a file upload using an iframe?Best solution by Stack Overflow
- How to Read xml file in java?Best solution by Stack Overflow
- How to use angular-file-upload on a page properly?Best solution by angular-file-upload.appspot.com
- How to write XML file with Java and read it?Best solution by Stack Overflow
- How to open file using Google drive api?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.