This page describes how to change the storage class of objects within a bucket through rewriting the object. To learn how to change object storage classes without rewriting an object, see the Object Lifecycle Management feature.
Prerequisites
Prerequisites can vary based on the tool used:
Console
Individual object storage classes cannot be set through the Google Cloud console. Instead, use the command line.
Command line
In order to complete this guide using a command-line utility, you must have the proper IAM permissions. If the object you want to access exists in a project that you did not create, you might need the project owner to give you a role that contains the necessary permissions.
For a list of permissions required for specific actions, see IAM permissions for gsutil commands.
For a list of relevant roles, see Cloud Storage roles. Alternatively, you can create a custom role that has specific, limited permissions.
Code samples
In order to complete this guide using the Cloud Storage client libraries, you must have the proper IAM permissions. If the object you want to access exists in a project that you did not create, you might need the project owner to give you a role that contains the necessary permissions. Unless otherwise noted, client library requests are made through the JSON API.
For a list of permissions required for specific actions, see IAM permissions for JSON methods.
For a list of relevant roles, see Cloud Storage roles. Alternatively, you can create a custom role that has specific, limited permissions.
REST APIs
JSON API
In order to complete this guide using the JSON API, you must have the proper IAM permissions. If the object you want to access exists in a project that you did not create, you might need the project owner to give you a role that contains the necessary permissions.
For a list of permissions required for specific actions, see IAM permissions for JSON methods.
For a list of relevant roles, see Cloud Storage roles. Alternatively, you can create a custom role that has specific, limited permissions.
Change an object's storage class
Complete the following steps to change an object's storage class:
Console
Individual object storage classes cannot be set through the Google Cloud console. Instead, use the command line.
Command line
gcloud
Use the gcloud alpha storage objects update
command with the
--storage-class
flag. For example:
gcloud alpha storage objects update gs://BUCKET_NAME/OBJECT_NAME --storage-class=STORAGE_CLASS
Where:
BUCKET_NAME
is the name of the bucket containing the object whose class you want to change. For example,my-bucket
.OBJECT_NAME
is the name of the object whose class you want to change. For example,pets/dog.png
.STORAGE_CLASS
is the new storage class for your object. For example,nearline
.
gsutil
Use the -s
flag in a gsutil rewrite
command. For example:
gsutil rewrite -s STORAGE_CLASS gs://BUCKET_NAME/OBJECT_NAME
Where:
STORAGE_CLASS
is the new storage class for your object. For example,nearline
.BUCKET_NAME
is the name of the bucket containing the object whose class you want to change. For example,my-bucket
.OBJECT_NAME
is the name of the object whose class you want to change. For example,pets/dog.png
.
Code samples
For more information, see the
Cloud Storage C++ API reference documentation.
For more information, see the
Cloud Storage C# API reference documentation.
For more information, see the
Cloud Storage Go API reference documentation.
For more information, see the
Cloud Storage Java API reference documentation.
For more information, see the
Cloud Storage Node.js API reference documentation.
For more information, see the
Cloud Storage PHP API reference documentation.
For more information, see the
Cloud Storage Python API reference documentation.
For more information, see the
Cloud Storage Ruby API reference documentation.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
REST APIs
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. For instructions, see API authentication.
Create a JSON file that contains the following information:
{ "storageClass": "STORAGE_CLASS" }
Where:
STORAGE_CLASS
is the new storage class for your object. For example,nearline
.
Use
cURL
to call the JSON API with aPOST
Object, request:curl -X POST --data-binary @JSON_FILE_NAME \ -H "Authorization: Bearer OAUTH2_TOKEN" \ -H "Content-Type: application/json" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME/rewriteTo/b/BUCKET_NAME/o/OBJECT_NAME"
Where:
JSON_FILE_NAME
is the path for the JSON file that you created in Step 2.OAUTH2_TOKEN
is the access token you generated in Step 1.BUCKET_NAME
is the name of the bucket containing the original object. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials. For instructions, see API authentication.
Use
cURL
to call the XML API with aPUT
Object request:curl -X PUT --data-binary @OBJECT \ -H "Authorization: Bearer OAUTH2_TOKEN" \ -H "Content-Type: OBJECT_CONTENT_TYPE" \ -H "x-goog-storage-class: STORAGE_CLASS" \ "https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME"
Where:
OBJECT
is the local path to the object whose storage class you want to change (you must re-upload the object when changing storage class with the XML API). For example,Desktop/dog.png
.OAUTH2_TOKEN
is the access token you generated in Step 1.OBJECT_CONTENT_TYPE
is the content type of the object. For example,image/png
.STORAGE_CLASS
is the new storage class for your object. For example,nearline
.BUCKET_NAME
is the name of the bucket containing the object you are rewriting. For example,my-bucket
.OBJECT_NAME
is the URL-encoded name of the object you are rewriting. For example,pets/dog.png
, URL-encoded aspets%2Fdog.png
.
What's next
- Learn about available storage classes.
- Change object storage classes with Object Lifecycle Management.
- Learn about object metadata.