Know About Google Cloud Storage And Google Cloud SQL

Reading Time: 2 minutes

What is Google Cloud Storage?

Google Cloud Storage is the object storage service. Google Cloud offers a storage service. It comes with a number of useful capabilities out of the box, including object versioning and fine-grained permissions (by object or bucket), which may simplify development and lower operating costs. Several services rely on Google Cloud Storage. Google Cloud Storage allows you to store any kind of file in one or more locations. In Spring Cloud GCP to auto-configure the different Storage components, a Spring Boot starter is supplied.

Maven Coordinates:-

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-storage</artifactId>
</dependency>

Gradle Coordinates:-

dependencies {
    implementation("com.google.cloud:spring-cloud-gcp-starter-storage")
}

Storage in the Cloud

In the Spring application environment, the starter automatically configures and registers a Storage bean. List/create/update/delete buckets (a set of objects with identical rights and resilience needs) and objects using the Storage bean (Javadoc).

@Autowired
private Storage storage;

public void createFile() {
    Bucket bucket = storage.create(BucketInfo.of("storage-bucket"));

    storage.create(
        BlobInfo.newBuilder("storage-bucket", "subdirectory/MyFile").build(),
            "file contents".getBytes()
    );
}

Spring Resources: Cloud Storage Objects

A Google Cloud Storage (GCS) object is a new resource type in spring Cloud GCP. The @Value annotation allows GCS objects to be accessed by their GCS URL in the Spring Resource Abstraction for Google Cloud Storage:

@Value("gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]")
private Resource gcsResource;

an example application is provided here.

What is Google Cloud SQL?

Google Cloud SQL is a fully-managed database service that assists with setting up and managing relational databases on the Google Cloud Platform (Google Cloud Platform). Simply said, Google Cloud SQL gives you the freedom to build up database infrastructure once you’ve finished developing your cloud project.

Spring Cloud GCP now supports Spring JDBC and Spring R2DBC, allowing you to use Spring JDBC and other libraries that rely on it, such as Spring Data JPA and Spring Data R2DBC, to run your MySQL or PostgreSQL databases on Google Cloud SQL.

Spring Cloud GCP provides Cloud SQL support in the form of two Spring Boot starters, one for MySQL and the other for PostgreSQL. The starters’ job is to read properties for configuration and assume default settings so that connecting to MySQL and PostgreSQL is as simple as feasible.

Support for JDBC

To use MySQL –

Maven Coordinates :-
<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
</dependency>

Gradle Coordinates :-
dependencies {
implementation("com.google.cloud:spring-cloud-gcp-starter-sql-mysql")
}

To use Postgre SQL –

Maven Coordinates :-
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
</dependency>

Gradle Coordinates :-
dependencies {
implementation("com.google.cloud:spring-cloud-gcp-starter-sql-postgresql")
}

Spring Boot Starter for Google Cloud SQL

The Google Cloud SQL Spring Boot Starters include an auto-configured DataSource object. It comes with a JdbcTemplate object bean that allows you to query and alter databases when used with Spring JDBC.

public List<Map<String, Object>> listUsers() {
    return jdbcTemplate.queryForList("SELECT * FROM user;");
}

To configure a DataSource bean, you may rely on Spring Boot data source auto-configuration. To put it another way, spring.datasource.username and spring.datasource.password are properties that can be used to hold SQL usernames and passwords.

Conclusion

In this blog, we have covered what is Google Cloud Storage and Google Cloud SQL in Spring Cloud GCP. Google Cloud Storage and Google Cloud SQL play a major role in Spring Cloud GCP. If you have any feedback or queries, please let me know in the comments.

“Keep Reading, Keep Coding and Keep Sharing”

Written by 

Ranu Rajput is currently working as a Software Engineer in JAVA domain at Knoldus Inc. She always eager to learn new things in new technologies. She is a PERFIVIR and believes in sharing knowledge.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading