This document describes how to use the Cloud Client Libraries for Cloud Storage in your app to store data and retrieve data from Cloud Storage from Cloud Storage.
Before you begin
- Follow the instructions in Setting up your development environment to set up your environment and project, and to understand how apps are structured in App Engine. Write down and save your project ID, because you will need it to run the sample application described in this document.
Make sure you create a Cloud Storage bucket for your application by invoking the following command:
gsutil mb gs://[YOUR_BUCKET_NAME]
Make the bucket publicly readable so it can serve files:
gsutil defacl set public-read gs://[YOUR_BUCKET_NAME]
Download the sample
Go
To clone the repository:
v1.18 and later
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
cd golang-samples/appengine_flexible/storage
v1.15 and earlier
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
cd golang-samples/appengine_flexible/go115_and_earlier/storage
Java
To clone the repository:
version 11/17
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/flexible/java-11/cloudstorage
version 8
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/flexible/java-8/cloudstorage
Node.js
To clone the repository:
v18 and later
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples/
cd nodejs-docs-samples/appengine/storage/flexible
v16 and earlier
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples/
cd nodejs-docs-samples/appengine/storage/flexible_nodejs16_and_earlier
PHP
To clone the repository:
git clone https://github.com/GoogleCloudPlatform/php-docs-samples
cd php-docs-samples/appengine/flexible/storage
Python
To clone the repository:
v3.8 and later
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/flexible/storage
v3.7 and earlier
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/flexible_python37_and_earlier/storage
Ruby
To clone the repository:
version 3.2
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
cd ruby-docs-samples/appengine/flexible/storage/
version 3.1 and earlier
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
cd ruby-docs-samples/appengine/flexible/ruby31-and-earlier/storage/
.NET
Download the sample app and extract it.
If you're using the command line, navigate into the app directory,
dotnet-docs-samples\appengine\flexible\` on branch
flex-dotnet3-and-earlier`.
To run your application locally, set up a service account and download credentials:
Open the list of credentials in the Google Cloud console.
Click Create credentials.
Select Service account key.
A Create service account key window opens.
Click the drop-down box below Service account, then click Compute Engine default service account.
Select JSON for the Key type.
Click Create.
A New private key window is displayed and the private key for the is downloaded automatically.
Click Close.
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON key that was downloaded. For example, in PowerShell:# For this powershell session. PS > $env:GOOGLE_APPLICATION_CREDENTIALS = "$env:USERPROFILE\Downloads\your-project-id-dea9fa230eae3.json" # For all processes created after this command. PS > [Environment]::SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "$env:USERPROFILE\Downloads\your-project-id-dea9fa230eae3.json", "User")
Edit project configuration and install dependencies
Go
In app.yaml
, set GCLOUD_STORAGE_BUCKET
. This value is the
name of the Cloud Storage bucket you created previously.
v1.18 and later
v1.15 and earlier
Java
In app.yaml
, set the BUCKET_NAME
to the Cloud Storage you previously created for your project.
version 11/17
version 8
In pom.xml
, set com.google.cloud
as a dependency, and specify
google-cloud-storage
as the artifactID for that dependency; this provides
the functions to use Cloud Storage.
version 11/17
version 8
Node.js
In app.yaml
, add your project ID to the GOOGLE_CLOUD_PROJECT
environment
value. Then set the GCLOUD_STORAGE_BUCKET
environment value to the name of
the Cloud Storage bucket you created previously.
v18 and later
v16 and earlier
In package.json
, add @google-cloud/storage
as a dependency, which
provides the functions to use Cloud Storage.
v18 and later
v16 and earlier
See the README.md
file
for instructions on running and testing locally.
PHP
In app.yaml
, set CLOUD_STORAGE_BUCKET
; this value is the
name of the Cloud Storage bucket you created previously.
In composer.json
, notice that you must include the Cloud Client library,
because this provides Cloud Storage functions.
Python
In app.yaml
, set GOOGLE_STORAGE_BUCKET
; this value is the
name of the Cloud Storage bucket you created previously.
v3.8 and later
v3.7 and earlier
In requirements.txt
, notice that you must include the
google-cloud-storage
library, because this provides Cloud Storage
functions.
v3.8 and later
v3.7 and earlier
Ruby
In app.yaml
, set the GCLOUD_STORAGE_BUCKET
to the Cloud Storage you previously created for your project.
version 3.2
version 3.1 and earlier
Note that you must include the gcloud
library in Gemfile
to use Cloud Storage functions.
.NET
In appsettings.json
, set BucketName
; this value is the
name of the Cloud Storage bucket you created previously.
Application code
Go
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler writes the file to the Cloud Storage bucket using Cloud Storage NewWriter function.
Notice that to retrieve this file from Cloud Storage, you will need to specify the bucket name and the filename. You should store these values in your app for future use.
Java
The sample application presents a web page prompting the user to supply a file
to be stored in Cloud Storage. When the user selects a file and clicks
submit, the doPost
request handler writes the file to the
Cloud Storage bucket using
Storage.create
Notice that to retrieve this file from Cloud Storage, you will need to specify the bucket name and the filename. You should store these values in your app for future use.
version 11/17
version 8
Node.js
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.
Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.
PHP
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.
Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.
Python
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a Cloud Storage blob and writes it to the Cloud Storage bucket.
Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.
v3.8 and later
v3.7 and earlier
Ruby
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.
Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.
.NET
The sample application presents a web page prompting the user to supply a file to be stored in Cloud Storage. When the user selects a file and clicks submit, the upload handler loads the file content into a blob and writes it to Cloud Storage.
Notice that after the file is uploaded to Cloud Storage, the public URL to this file is returned, which you can use to serve the file directly from Cloud Storage. You should store this value in your app for future use.
For more information
For complete information on Cloud Storage, see the Cloud Storage documentation.