Spring Resources are an abstraction for a number of low-level resources, such as file system files, classpath files, servlet context-relative files, etc. Spring Cloud GCP adds a new resource type: a Google Cloud Storage (GCS) object.
A Spring Boot starter is provided to auto-configure the various Storage components.
Maven coordinates, using Spring Cloud GCP BOM:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gcp-starter-storage</artifactId> </dependency>
Gradle coordinates:
dependencies { compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-storage' }
The Spring Resource Abstraction for Google Cloud Storage allows GCS objects to be accessed by their GCS URL:
@Value("gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]") private Resource gcsResource;
This creates a Resource
object that can be used to read the object, among
other possible operations.
It is also possible to write to a Resource
, although a WriteableResource
is required.
@Value("gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]") private Resource gcsResource; ... try (OutputStream os = ((WritableResource) gcsResource).getOutputStream()) { os.write("foo".getBytes()); }
If the resource path refers to an object on Google Cloud Storage (as opposed to a bucket), then the resource
can be cast as a GoogleStorageResourceObject
and the getGoogleStorageObject
method can be called
to obtain a Blob
.
This type represents a GCS file, which has associated metadata, such as content-type, that can be set.
The createSignedUrl
method can also be used to obtain signed URLs for GCS objects.
However, creating signed URLs requires that the resource was created using service account credentials.
The Spring Boot Starter for Google Cloud Storage auto-configures the Storage
bean required by the
spring-cloud-gcp-storage
module, based on the CredentialsProvider
provided by the Spring Boot
GCP starter.
The Spring Boot Starter for Google Cloud Storage provides the following configuration options:
Name | Description | Optional | Default value |
| Creates files and buckets on Google Cloud Storage when writes are made to non-existent files | Yes |
|
| OAuth2 credentials for authenticating with the Google Cloud Storage API, if different from the ones in the Spring Cloud GCP Core Module | Yes | |
| OAuth2 scope for Spring Cloud GCP Storage credentials | Yes |