Spider
Spider

Reputation: 455

Working with file from google cloud storage

I'm refactoring my code to work with GCS and not just locally, I am parsing xml files.

My original code:

File file = new File("localpath.xml");
DocumentBuilder docBuilder = DocumentBuilderFactory.instance().newDocumentBuilder();
Document d = docBuilder.parse(file);

However, now due to gcs it looks like this:

Blob blob = storage.get(bucketName, filePath);
ReadChannel readChannel = blob.reader();

Now that I have the content in my readChannel, how do I convert that to a File object? Is there a way to work with this without restructuring all of my code?

Upvotes: 0

Views: 2347

Answers (1)

Kolban
Kolban

Reputation: 15246

Rather than convert to a Java File object, we should understand that a ReadChannel object has a read method (see https://googleapis.dev/java/google-cloud-clients/0.97.0-alpha/com/google/cloud/ReadChannel.html). Through the read() method you can populate a ByteBuffer object. At this point you now have the content of the GCS blob in your Java app as a ByteBuffer object.

Given a ByteBuffer, you can now get access to the data as a byte[] array using the array() method (https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#array()). Now that you have a byte[] array, you can wrap that in an Input Stream (https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html).

And finally ... we have gone from a ReadChannel -> InputStream and you can parse the content using the DocumentBuilder parse() method which has an overload that takes an InputStream ...

https://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.io.InputStream)

... later ...

Perhaps a simpler story is to realize that given a Blob object, we can go straight to the byte[] array using Blob.getContent(). Once we have the byte[] we can create the ByteArrayInputStream needed for the DocumentBuilder.parse().

Upvotes: 3

Related Questions