18 lines
12 KiB
HTML
18 lines
12 KiB
HTML
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
<title>151. Spring Resources</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi_spring-cloud-gcp-reference.html" title="Part XVIII. Spring Cloud GCP"><link rel="prev" href="multi__google_cloud_pubsub.html" title="150. Google Cloud Pub/Sub"><link rel="next" href="multi__spring_jdbc.html" title="152. Spring JDBC"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">151. Spring Resources</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__google_cloud_pubsub.html">Prev</a> </td><th width="60%" align="center">Part XVIII. Spring Cloud GCP</th><td width="20%" align="right"> <a accesskey="n" href="multi__spring_jdbc.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="_spring_resources" href="#_spring_resources"></a>151. Spring Resources</h2></div></div></div><p><a class="link" href="https://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html" target="_top">Spring Resources</a> 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.</p><p>A Spring Boot starter is provided to auto-configure the various Storage components.</p><p>Maven coordinates, using Spring Cloud GCP BOM:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><dependency></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><groupId></span>org.springframework.cloud<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></groupId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"><artifactId></span>spring-cloud-gcp-starter-storage<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></artifactId></span>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag"></dependency></span></pre><p>Gradle coordinates:</p><pre class="screen">dependencies {
|
|
compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-storage'
|
|
}</pre><p>This starter is also available from <a class="link" href="https://start.spring.io/" target="_top">Spring Initializr</a> through the <code class="literal">GCP Storage</code> entry.</p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_google_cloud_storage" href="#_google_cloud_storage"></a>151.1 Google Cloud Storage</h2></div></div></div><p>The Spring Resource Abstraction for Google Cloud Storage allows GCS objects to be accessed by their GCS URL using the <code class="literal">@Value</code> annotation:</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Value("gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]")</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> Resource gcsResource;</pre><p>…​or the Spring application context</p><pre class="programlisting">SpringApplication.run(...).getResource(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]"</span>);</pre><p>This creates a <code class="literal">Resource</code> object that can be used to read the object, among <a class="link" href="https://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html#resources-resource" target="_top">other possible operations</a>.</p><p>It is also possible to write to a <code class="literal">Resource</code>, although a <code class="literal">WriteableResource</code> is required.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Value("gs://[YOUR_GCS_BUCKET]/[GCS_FILE_NAME]")</span></em>
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> Resource gcsResource;
|
|
...
|
|
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">try</span> (OutputStream os = ((WritableResource) gcsResource).getOutputStream()) {
|
|
os.write(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"foo"</span>.getBytes());
|
|
}</pre><p>To work with the <code class="literal">Resource</code> as a Google Cloud Storage resource, cast it to <code class="literal">GoogleStorageResource</code>.</p><p>If the resource path refers to an object on Google Cloud Storage (as opposed to a bucket), then the <code class="literal">getBlob</code> method can be called to obtain a <a class="link" href="https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-storage/src/main/java/com/google/cloud/storage/Blob.java" target="_top"><code class="literal">Blob</code></a>.
|
|
This type represents a GCS file, which has associated <a class="link" href="https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata" target="_top">metadata</a>, such as content-type, that can be set.
|
|
The <code class="literal">createSignedUrl</code> method can also be used to obtain <a class="link" href="https://cloud.google.com/storage/docs/access-control/signed-urls" target="_top">signed URLs</a> for GCS objects.
|
|
However, creating signed URLs requires that the resource was created using service account credentials.</p><p>The Spring Boot Starter for Google Cloud Storage auto-configures the <code class="literal">Storage</code> bean required by the <code class="literal">spring-cloud-gcp-storage</code> module, based on the <code class="literal">CredentialsProvider</code> provided by the Spring Boot GCP starter.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_setting_the_content_type" href="#_setting_the_content_type"></a>151.1.1 Setting the Content Type</h3></div></div></div><p>You can set the content-type of Google Cloud Storage files from their corresponding <code class="literal">Resource</code> objects:</p><pre class="programlisting">((GoogleStorageResource)gcsResource).getBlob().toBuilder().setContentType(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"text/html"</span>).build().update();</pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_configuration_3" href="#_configuration_3"></a>151.2 Configuration</h2></div></div></div><p>The Spring Boot Starter for Google Cloud Storage provides the following configuration options:</p><div class="informaltable"><table class="informaltable" style="border-collapse: collapse;border-top: 1px solid ; border-bottom: 1px solid ; "><colgroup><col class="col_1"><col class="col_2"><col class="col_3"><col class="col_4"></colgroup><tbody><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Name</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Description</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Required</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p>Default value</p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.storage.enabled</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Enables the GCP storage APIs.</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">true</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.storage.auto-create-files</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Creates files and buckets on Google Cloud Storage when writes are made to non-existent files</p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">true</code></p></td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.storage.credentials.location</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>OAuth2 credentials for authenticating with the Google Cloud Storage API, if different from the ones in the <a class="link" href="multi_spring-cloud-gcp-core.html" title="149. Spring Cloud GCP Core">Spring Cloud GCP Core Module</a></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"> </td></tr><tr><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.storage.credentials.encoded-key</code></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>Base64-encoded contents of OAuth2 account private key for authenticating with the Google Cloud Storage API, if different from the ones in the <a class="link" href="multi_spring-cloud-gcp-core.html" title="149. Spring Cloud GCP Core">Spring Cloud GCP Core Module</a></p></td><td style="border-right: 1px solid ; border-bottom: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="border-bottom: 1px solid ; " align="left" valign="top"> </td></tr><tr><td style="border-right: 1px solid ; " align="left" valign="top"><p><code class="literal">spring.cloud.gcp.storage.credentials.scopes</code></p></td><td style="border-right: 1px solid ; " align="left" valign="top"><p><a class="link" href="https://developers.google.com/identity/protocols/googlescopes" target="_top">OAuth2 scope</a> for Spring Cloud GCP Storage credentials</p></td><td style="border-right: 1px solid ; " align="left" valign="top"><p>No</p></td><td style="" align="left" valign="top"><p><a class="link" href="https://www.googleapis.com/auth/devstorage.read_write" target="_top">https://www.googleapis.com/auth/devstorage.read_write</a></p></td></tr></tbody></table></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_sample_2" href="#_sample_2"></a>151.3 Sample</h2></div></div></div><p>A <a class="link" href="https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-storage-resource-sample" target="_top">sample application</a> and a <a class="link" href="https://codelabs.developers.google.com/codelabs/spring-cloud-gcp-gcs/index.html" target="_top">codelab</a> are available.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__google_cloud_pubsub.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="multi_spring-cloud-gcp-reference.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="multi__spring_jdbc.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">150. Google Cloud Pub/Sub </td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top"> 152. Spring JDBC</td></tr></table></div></body></html> |