This page describes how to use your own encryption key, referred to as a customer-supplied encryption key, with Cloud Storage. For other encryption options in Cloud Storage, see Data Encryption Options.
Generate your own encryption key
There are many ways to generate a Base64-encoded AES-256 encryption key. Here are several examples:
C++
For more information, see the Cloud Storage C++ API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
C#
For more information, see the Cloud Storage C# API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
For more information, see the Cloud Storage Go API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
For more information, see the Cloud Storage Java API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
For more information, see the Cloud Storage Node.js API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
For more information, see the Cloud Storage PHP API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
For more information, see the Cloud Storage Python API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
For more information, see the Cloud Storage Ruby API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Upload with your encryption key
To use customer-supplied encryption keys to upload an object:
Console
The Google Cloud console cannot be used to upload an object with a customer-supplied encryption key. Use the Google Cloud CLI or the client libraries instead.
Command line
Use the gcloud storage cp
command with the --encryption-key
flag:
gcloud storage cp SOURCE_DATA gs://BUCKET_NAME/OBJECT_NAME --encryption-key=YOUR_ENCRYPTION_KEY
Where:
SOURCE_DATA
is the source location of the data you're encrypting. This can be any source location supported by thecp
command. For example, a local file such asDesktop/dogs.png
or another Cloud Storage object such asgs://my-bucket/pets/old-dog.png
.BUCKET_NAME
is the name of the destination bucket for this copy command. For example,my-bucket
.OBJECT_NAME
is the name of the final, encrypted object. For example,pets/new-dog.png
.YOUR_ENCRYPTION_KEY
is the AES-256 key that you want to use for encrypting the uploaded object.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aPOST
Object request:curl -X POST --data-binary @OBJECT \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: OBJECT_CONTENT_TYPE" \ -H "x-goog-encryption-algorithm: AES256" \ -H "x-goog-encryption-key: YOUR_ENCRYPTION_KEY" \ -H "x-goog-encryption-key-sha256: HASH_OF_YOUR_KEY" \ "https://storage.googleapis.com/upload/storage/v1/b/BUCKET_NAME/o?uploadType=media&name=OBJECT_NAME"
Where:
OBJECT
is the path to the object you are uploading. For example,Desktop/dogs.png
.OBJECT_CONTENT_TYPE
is the content type of the object. For example,image/png
.YOUR_ENCRYPTION_KEY
is the AES-256 key used for encrypting the uploaded object.HASH_OF_YOUR_KEY
is the SHA-256 hash for your AES-256 key.BUCKET_NAME
is the name of the bucket to which you are uploading the object. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object you are uploading. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
See Encryption request headers for more information on encryption-specific headers.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aPUT
OBJECT request:curl -X -i PUT --data-binary @OBJECT \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: OBJECT_CONTENT_TYPE" \ -H "x-goog-encryption-algorithm: AES256" \ -H "x-goog-encryption-key: YOUR_ENCRYPTION_KEY" \ -H "x-goog-encryption-key-sha256: HASH_OF_YOUR_KEY" \ "https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME"
Where:
OBJECT
is the path to the object you are uploading. For example,Desktop/dogs.png
.OBJECT_CONTENT_TYPE
is the content type of the object. For example,image/png
.YOUR_ENCRYPTION_KEY
is the AES-256 key used for encrypting the uploaded object.HASH_OF_YOUR_KEY
is the SHA-256 hash for your AES-256 key.BUCKET_NAME
is the name of the bucket to which you are uploading the object. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object you are uploading. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
See Encryption request headers for more information on encryption-specific headers.
Download objects you've encrypted
To download an object stored in Cloud Storage that is encrypted with a customer-supplied encryption key:
Console
The Google Cloud console cannot be used to download objects encrypted with customer-supplied encryption keys. Use the Google Cloud CLI or the client libraries instead.
Command line
Use the gcloud storage cp
command with the --decryption-keys
flag:
gcloud storage cp gs://BUCKET_NAME/OBJECT_NAME OBJECT_DESTINATION --decryption-keys=YOUR_ENCRYPTION_KEY
Where:
BUCKET_NAME
is the name of the bucket containing the object you are downloading. For example,my-bucket
.OBJECT_NAME
is the name of the object you are downloading. For example,pets/dog.png
.OBJECT_DESTINATION
is the location where you want to save your object. For example,Desktop
.YOUR_ENCRYPTION_KEY
is the AES-256 key used to encrypt the object when it was uploaded.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aGET
Object request:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "x-goog-encryption-algorithm: AES256" \ -H "x-goog-encryption-key: YOUR_ENCRYPTION_KEY" \ -H "x-goog-encryption-key-sha256: HASH_OF_YOUR_KEY" \ -o "SAVE_TO_LOCATION" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME?alt=media"
Where:
YOUR_ENCRYPTION_KEY
is the AES-256 key you used to encrypt the object.HASH_OF_YOUR_KEY
is the SHA-256 hash for your AES-256 key.SAVE_TO_LOCATION
is the location where you want to save your object. For example,Desktop/dog.png
.BUCKET_NAME
is the name of the bucket from which you are downloading the object. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object you are downloading. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
See Encryption request headers for more information on encryption-specific headers.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aGET
OBJECT request:curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "x-goog-encryption-algorithm: AES256" \ -H "x-goog-encryption-key: YOUR_ENCRYPTION_KEY" \ -H "x-goog-encryption-key-sha256: HASH_OF_YOUR_KEY" \ -o "SAVE_TO_LOCATION" \ "https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME"
Where:
YOUR_ENCRYPTION_KEY
is the AES-256 key you used to encrypt the object.HASH_OF_YOUR_KEY
is the SHA-256 hash for your AES-256 key.SAVE_TO_LOCATION
is the location where you want to save your object. For example,Desktop/dog.png
.BUCKET_NAME
is the name of the bucket from which you are downloading the object. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object you are downloading. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
See Encryption request headers for more information on encryption-specific headers.
Rotate your encryption keys
To rotate a customer-supplied encryption key:
Console
The Google Cloud console cannot be used to rotate customer-supplied encryption keys. Use the Google Cloud CLI or the client libraries instead.
Command line
Use the gcloud storage objects update
command with the
appropriate flags:
gcloud storage objects update gs://BUCKET_NAME/OBJECT_NAME --encryption-key=NEW_KEY --decryption-keys=OLD_KEY
Where:
BUCKET_NAME
is the name of the bucket that contains the object whose key you are rotating. For example,my-bucket
.OBJECT_NAME
is the name of the object whose key you are rotating. For example,pets/dog.png
.NEW_KEY
is the new customer-supplied encryption key that you want to use to encrypt the object.OLD_KEY
is the current customer-supplied encryption key that is used to encrypt the object.
Client libraries
For more information, see the
Cloud Storage C++ API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage C# API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Go API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Java API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Node.js API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage PHP API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Python API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
For more information, see the
Cloud Storage Ruby API
reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see
Set up authentication for a local development environment.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with aPOST
Object request:curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Length: 0" \ -H "x-goog-encryption-algorithm: AES256" \ -H "x-goog-encryption-key: NEW_ENCRYPTION_KEY" \ -H "x-goog-encryption-key-sha256: HASH_OF_NEW_KEY" \ -H "x-goog-copy-source-encryption-algorithm: AES256" \ -H "x-goog-copy-source-encryption-key: OLD_ENCRYPTION_KEY" \ -H "x-goog-copy-source-encryption-key-sha256: HASH_OF_OLD_KEY" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME/rewriteTo/b/BUCKET_NAME/o/OBJECT_NAME"
Where:
NEW_ENCRYPTION_KEY
is the new AES-256 key used for encrypting your object.HASH_OF_NEW_KEY
is the SHA-256 hash for your new AES-256 key.OLD_ENCRYPTION_KEY
is the current AES-256 key used to encrypt your object.HASH_OF_OLD_KEY
is the current SHA-256 hash for your AES-256 key.BUCKET_NAME
is the name of the bucket containing the relevant object. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object whose keys you are rotating. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
See Encryption request headers for more information on encryption-specific headers.
XML API
The XML API does not support rotating a customer-supplied encryption key through rewriting object. To apply a new customer-supplied key to an object using the XML API, you should:
What's next
Learn more about customer-supplied encryption keys.
Learn how to rotate your customer-supplied encryption key to a Cloud KMS key