Skip to content

Commit

Permalink
BAEL-1544 - Intro to Google Cloud Storage with Java (eugenp#3693)
Browse files Browse the repository at this point in the history
* Initial additions

* Initial additions

* BAEL-1544 - Intro to Google Cloud Storage With Java

* BAEL-1544 - Intro to Google Cloud Storage With Java
  • Loading branch information
egoebelbecker authored and maibin committed Feb 24, 2018
1 parent 87f7a2a commit 83b0771
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
16 changes: 16 additions & 0 deletions google-cloud/README.md
Original file line number Diff line number Diff line change
@@ -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

```
38 changes: 38 additions & 0 deletions google-cloud/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>google-cloud</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>google-cloud</name>
<description>Google Cloud Tutorials</description>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.16.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- Check for the most recent available version: https://projectlombok.org/changelog.html -->
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<properties>
<lombok.version>1.16.18</lombok.version>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -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<Blob> 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();
}
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

<module>geotools</module>
<module>testing-modules/groovy-spock</module>
<module>google-cloud</module>
<module>gson</module>
<module>guava</module>
<module>guava-modules/guava-18</module>
Expand Down

0 comments on commit 83b0771

Please sign in to comment.