Skip to content

Commit 83b0771

Browse files
egoebelbeckermaibin
authored andcommitted
BAEL-1544 - Intro to Google Cloud Storage with Java (eugenp#3693)
* Initial additions * Initial additions * BAEL-1544 - Intro to Google Cloud Storage With Java * BAEL-1544 - Intro to Google Cloud Storage With Java
1 parent 87f7a2a commit 83b0771

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

‎google-cloud/README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Google Cloud Tutorial Project
2+
3+
### Relevant Article:
4+
- [Intro to Google Cloud Storage With Java](http://www.baeldung.com/intro-to-google-cloud-storage-with-java/)
5+
6+
### Overview
7+
This Maven project contains the Java code for the article linked above.
8+
9+
### Package Organization
10+
Java classes for the intro tutorial are in the org.baeldung.google.cloud package. Please note that Google Cloud requires
11+
a user account and credentials, as explained in the tutorial.
12+
13+
14+
### Running the tests
15+
16+
```

‎google-cloud/pom.xml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>google-cloud</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>google-cloud</name>
9+
<description>Google Cloud Tutorials</description>
10+
11+
<parent>
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>parent-modules</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.google.cloud</groupId>
20+
<artifactId>google-cloud-storage</artifactId>
21+
<version>1.16.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.projectlombok</groupId>
25+
<artifactId>lombok</artifactId>
26+
<!-- Check for the most recent available version: https://projectlombok.org/changelog.html -->
27+
<version>${lombok.version}</version>
28+
<scope>provided</scope>
29+
</dependency>
30+
</dependencies>
31+
32+
<properties>
33+
<lombok.version>1.16.18</lombok.version>
34+
<java.version>1.8</java.version>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
</properties>
37+
38+
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.baeldung.google.cloud.storage;
2+
3+
import com.google.api.gax.paging.Page;
4+
import com.google.auth.Credentials;
5+
import com.google.auth.oauth2.GoogleCredentials;
6+
import com.google.cloud.storage.*;
7+
import lombok.extern.slf4j.Slf4j;
8+
9+
import java.io.FileInputStream;
10+
import java.io.IOException;
11+
import java.nio.ByteBuffer;
12+
import java.nio.channels.WritableByteChannel;
13+
14+
import static java.nio.charset.StandardCharsets.UTF_8;
15+
16+
/**
17+
* Simple class for creating, reading and modifying text blobs on Google Cloud
18+
*/
19+
@Slf4j
20+
public class GoogleCloudStorage {
21+
22+
private Storage storage;
23+
private Bucket bucket;
24+
25+
public static void main(String[] args) throws Exception {
26+
27+
// Use this variation to read the Google authorization JSON from the resources directory with a path
28+
// and a project name.
29+
GoogleCloudStorage googleCloudStorage =
30+
new GoogleCloudStorage("google-cloud/src/main/resources/google_auth.json", "baeldung-cloud-tutorial");
31+
32+
// Bucket require globally unique names, so you'll probably need to change this
33+
Bucket bucket = googleCloudStorage.getBucket("baeldung-1-bucket");
34+
35+
// Save a simple string
36+
BlobId blobId = googleCloudStorage.saveString("my-first-blob", "Hi there!", bucket);
37+
38+
// Get it by blob id this time
39+
String value = googleCloudStorage.getString(blobId);
40+
41+
log.info("Read data: {}", value);
42+
43+
googleCloudStorage.updateString(blobId, "Bye now!");
44+
45+
// Get the string by blob name
46+
value = googleCloudStorage.getString("my-first-blob");
47+
48+
log.info("Read modified data: {}", value);
49+
50+
51+
}
52+
53+
54+
// Use path and project name
55+
private GoogleCloudStorage(String pathToConfig, String projectId) throws IOException {
56+
Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(pathToConfig));
57+
storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId(projectId).build().getService();
58+
}
59+
60+
// Check for bucket existence and create if needed.
61+
private Bucket getBucket(String bucketName) {
62+
bucket = storage.get(bucketName);
63+
if (bucket == null) {
64+
System.out.println("Creating new bucket.");
65+
bucket = storage.create(BucketInfo.of(bucketName));
66+
}
67+
return bucket;
68+
}
69+
70+
// Save a string to a blob
71+
private BlobId saveString(String blobName, String value, Bucket bucket) {
72+
byte[] bytes = value.getBytes(UTF_8);
73+
Blob blob = bucket.create(blobName, bytes);
74+
return blob.getBlobId();
75+
}
76+
77+
78+
// get a blob by id
79+
private String getString(BlobId blobId) {
80+
Blob blob = storage.get(blobId);
81+
return new String(blob.getContent());
82+
}
83+
84+
85+
// get a blob by name
86+
private String getString(String name) {
87+
Page<Blob> blobs = bucket.list();
88+
for (Blob blob: blobs.getValues()) {
89+
if (name.equals(blob.getName())) {
90+
return new String(blob.getContent());
91+
}
92+
}
93+
return "Blob not found";
94+
}
95+
96+
// Update a blob
97+
private void updateString(BlobId blobId, String newString) throws IOException {
98+
Blob blob = storage.get(blobId);
99+
if (blob != null) {
100+
WritableByteChannel channel = blob.writer();
101+
channel.write(ByteBuffer.wrap(newString.getBytes(UTF_8)));
102+
channel.close();
103+
}
104+
}
105+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
<module>geotools</module>
6969
<module>testing-modules/groovy-spock</module>
70+
<module>google-cloud</module>
7071
<module>gson</module>
7172
<module>guava</module>
7273
<module>guava-modules/guava-18</module>

0 commit comments

Comments
 (0)