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+ }
0 commit comments