diff --git a/google-cloud/README.md b/google-cloud/README.md new file mode 100644 index 0000000000..6022796a0e --- /dev/null +++ b/google-cloud/README.md @@ -0,0 +1,16 @@ +## Google Cloud Tutorial Project + +### Relevant Article: +- [Intro to Google Cloud Storage With Java](http://www.baeldung.com/intro-to-google-cloud-storage-with-java/) + +### Overview +This Maven project contains the Java code for the article linked above. + +### Package Organization +Java classes for the intro tutorial are in the org.baeldung.google.cloud package. Please note that Google Cloud requires +a user account and credentials, as explained in the tutorial. + + +### Running the tests + +``` diff --git a/google-cloud/pom.xml b/google-cloud/pom.xml new file mode 100644 index 0000000000..0f1eff36f8 --- /dev/null +++ b/google-cloud/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + google-cloud + 0.1-SNAPSHOT + jar + google-cloud + Google Cloud Tutorials + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + com.google.cloud + google-cloud-storage + 1.16.0 + + + org.projectlombok + lombok + + ${lombok.version} + provided + + + + + 1.16.18 + 1.8 + UTF-8 + + + diff --git a/google-cloud/src/main/java/com/baeldung/google/cloud/storage/GoogleCloudStorage.java b/google-cloud/src/main/java/com/baeldung/google/cloud/storage/GoogleCloudStorage.java new file mode 100644 index 0000000000..a69171f1db --- /dev/null +++ b/google-cloud/src/main/java/com/baeldung/google/cloud/storage/GoogleCloudStorage.java @@ -0,0 +1,105 @@ +package com.baeldung.google.cloud.storage; + +import com.google.api.gax.paging.Page; +import com.google.auth.Credentials; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.cloud.storage.*; +import lombok.extern.slf4j.Slf4j; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.WritableByteChannel; + +import static java.nio.charset.StandardCharsets.UTF_8; + +/** + * Simple class for creating, reading and modifying text blobs on Google Cloud + */ +@Slf4j +public class GoogleCloudStorage { + + private Storage storage; + private Bucket bucket; + + public static void main(String[] args) throws Exception { + + // Use this variation to read the Google authorization JSON from the resources directory with a path + // and a project name. + GoogleCloudStorage googleCloudStorage = + new GoogleCloudStorage("google-cloud/src/main/resources/google_auth.json", "baeldung-cloud-tutorial"); + + // Bucket require globally unique names, so you'll probably need to change this + Bucket bucket = googleCloudStorage.getBucket("baeldung-1-bucket"); + + // Save a simple string + BlobId blobId = googleCloudStorage.saveString("my-first-blob", "Hi there!", bucket); + + // Get it by blob id this time + String value = googleCloudStorage.getString(blobId); + + log.info("Read data: {}", value); + + googleCloudStorage.updateString(blobId, "Bye now!"); + + // Get the string by blob name + value = googleCloudStorage.getString("my-first-blob"); + + log.info("Read modified data: {}", value); + + + } + + + // Use path and project name + private GoogleCloudStorage(String pathToConfig, String projectId) throws IOException { + Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(pathToConfig)); + storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId(projectId).build().getService(); + } + + // Check for bucket existence and create if needed. + private Bucket getBucket(String bucketName) { + bucket = storage.get(bucketName); + if (bucket == null) { + System.out.println("Creating new bucket."); + bucket = storage.create(BucketInfo.of(bucketName)); + } + return bucket; + } + + // Save a string to a blob + private BlobId saveString(String blobName, String value, Bucket bucket) { + byte[] bytes = value.getBytes(UTF_8); + Blob blob = bucket.create(blobName, bytes); + return blob.getBlobId(); + } + + + // get a blob by id + private String getString(BlobId blobId) { + Blob blob = storage.get(blobId); + return new String(blob.getContent()); + } + + + // get a blob by name + private String getString(String name) { + Page blobs = bucket.list(); + for (Blob blob: blobs.getValues()) { + if (name.equals(blob.getName())) { + return new String(blob.getContent()); + } + } + return "Blob not found"; + } + + // Update a blob + private void updateString(BlobId blobId, String newString) throws IOException { + Blob blob = storage.get(blobId); + if (blob != null) { + WritableByteChannel channel = blob.writer(); + channel.write(ByteBuffer.wrap(newString.getBytes(UTF_8))); + channel.close(); + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index a4090fae62..beb377a40b 100644 --- a/pom.xml +++ b/pom.xml @@ -67,6 +67,7 @@ geotools testing-modules/groovy-spock + google-cloud gson guava guava-modules/guava-18