How to upload file to google cloud storage using Java?

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

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

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.