How to write files in a Google Cloud bucket

2 minute read

dist files

Photo by Sixteen Miles Out on Unsplash

In this post, we are going to see how to write files into Google Cloud bucket from a Java Maven application.

Google Cloud Storage

The first thing to know is that a bucket in Google Cloud is like a directory in an OS filesystem.

The Google Cloud Storage API allows us to interact with the Google Cloud ecosystem. In the following link we can find documentation of the API.

Adding dependency in the pom.xml file

To use Google Cloud storage API we have to add in the pom.xml file the Maven artifact corresponding to the client version that we want to use.

We need to add the following lines in the pom.xml file of our Java project. This way we will be able to use the Java classes to use Google Cloud Storage.

 <dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>google-cloud-storage</artifactId>
 <version>[1.113.4]</version>
</dependency>

If IntelliJ IDE is not importing the dependencies we can refresh them in the maven tab, pressing the refresh icon.

dist files

Creating a configuration class

We need to have a Java file where we set the configuration to be able to connect with the Google cloud storage system. We need two things to configure a connection: a bucket name and the credentials.


import com.google.inject.Singleton;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Singleton
@Getter
@Setter
@ToString
public class BucketConfiguration {

 private String bucketName;
 private String googleCloudStorageCredentials;
}

and then a properties like BucketConfiguration.properties

bucketName = <bucket-name>
googleCloudStorageCredentials = <store-credentials>

you will store those credentials in someplace like vault and not in the code of the application. Then our application should replace the properties placements from the properties file.

Storing secrets in a repository of software is a bad practice. Don’t do it.

Creating a Google Guice module

Here in this Google Guice module, we are going to create a module to provide a Storage class to the code that interacts with Google cloud storage. Creating a Storage is a detail that our code doesn’t need to know and it’s a good candidate to be injected.

If you want to learn more about dependency injection you can read a post around this that I wrote some time ago.


import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class StorageModule extends AbstractModule {

 private static final String GOOGLE_API_SCOPE = "https://www.googleapis.com/auth/cloud-platform";

    @Override
 protected void configure() {
 //nothing to do here
    }

    @Provides
    @Singleton
 public Storage getStorageConnection(BucketConfiguration googleCloudStorageConfiguration) throws IOException {
 InputStream inputStream = new ByteArrayInputStream(googleCloudStorageConfiguration.getGoogleCloudStorageCredentials().getBytes(Charset.defaultCharset()));
 GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream).createScoped(GOOGLE_API_SCOPE);
 return StorageOptions.newBuilder().setCredentials(credentials).build().getService();
    }
}

Creating a class to interact with Google cloud storage API

In the next class is where we use the BucketConfiguration where we have the config and the Storage that both have been injected using Guice.

@Singleton
public class BucketService {

 private static final String CONTENT_TYPE = "text/plain";
 private final BucketConfiguration bucketConfiguration;
 private final Storage storage;

    @Inject
 public Bucket(BucketConfiguration bucketConfiguration, Storage storage) {
 this.bucketConfiguration = bucketConfiguration;
 this.storage = storage;
    }

 public Blob createFile(String blobName, byte[] fileContent) {
 BlobId blobId = BlobId.of(bucketConfiguration.getBucketName(), blobName);
 BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType(CONTENT_TYPE).build();
 return storage.create(blobInfo, fileContent);
    }

In the API documentation we can see that they refer to the logic that we have implemented in the previous class as uploading objects.

Conclusion

In this post, we have seen how to write files into Google cloud storage using Java. Hope that this is helpful and that you have learned how to do it if you have to write files in Google Cloud Storage.

I won't give your address to anyone else, won't send you any spam, and you can unsubscribe at any time.
Disclaimer: Opinions are my own and not the views of my employer

Updated:

Comments