You can use Cloud Storage to store and serve files, such as movies or images or other static content.
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.
You can use the sample application in this guide for any
supported version of
Node.js by specifying the runtime version
and operating system in your app.yaml
file.
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:
gcloud storage buckets create gs://YOUR_BUCKET_NAME
Make the bucket publicly readable so it can serve files:
gcloud storage buckets add-iam-policy-binding gs://<var>YOUR_BUCKET_NAME</var> --member=allUsers --role=roles/storage.objectViewer
Download the sample
Go
To clone the repository:
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
cd golang-samples/appengine_flexible/storage
Java
To clone the repository:
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/flexible/java-17/cloudstorage
Node.js
To clone the repository:
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples/
cd nodejs-docs-samples/appengine/storage/flexible
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:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/flexible/storage
Ruby
To clone the repository:
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
cd ruby-docs-samples/appengine/flexible/storage/
.NET
Download the sample app and extract it.
If you're using the command line, navigate into the following app directory:
dotnet-docs-samples\appengine\flexible\CloudStorage\CloudStorage.Sample
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 Service account list, and select 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.
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.
Java
In app.yaml
, set the BUCKET_NAME
to the Cloud Storage you previously created for your project.
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.
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.
In package.json
, add @google-cloud/storage
as a dependency, which
provides the functions to use Cloud Storage.
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.
In requirements.txt
, notice that you must include the
google-cloud-storage
library, because this provides Cloud Storage
functions.
Ruby
In app.yaml
, set the GCLOUD_STORAGE_BUCKET
to the Cloud Storage you previously created for your project.
Note that you must include the gcloud
library in Gemfile
to use Cloud Storage functions.
.NET
In app.yaml
file , set the TEST_GOOGLE_BUCKET_NAME
to the Cloud Storage
you previously created for your project.
env_variables:
TEST_GOOGLE_BUCKET_NAME: [your-bucket-name]
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.
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.
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.