Use Autoclass

Overview

This page shows you how to enable, disable, and check the status of the Autoclass feature, which is set on a bucket in Cloud Storage.

Required roles

To get the permissions that you need to set and manage Autoclass on a bucket, ask your administrator to grant you the Storage Admin (roles/storage.admin) IAM role on the bucket. This predefined role contains the permissions required to set and manage Autoclass on a bucket. To see the exact permissions that are required, expand the Required permissions section:

Required permissions

  • storage.buckets.get
  • storage.buckets.list
    • This permission is only required for using the Google Cloud console to perform the tasks on this page.
  • storage.buckets.update

You can also get these permissions with custom roles.

For information about granting roles on buckets, see Use IAM with buckets.

Set Autoclass for a bucket

To set Autoclass for a bucket, complete the following instructions:

Console

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets

  2. In the list of buckets, click the name of the desired bucket.

  3. In the Bucket details page, click the Configuration tab.

  4. Click the Edit icon () for Default storage class.

  5. In the overlay window, select Autoclass to enable Autoclass. To disable Autoclass, select Set a default class, and then select a default storage class for the bucket.

  6. Click Confirm.

  7. To switch the terminal storage class for a bucket that has Autoclass enabled, click the Edit icon () for Included classes, and click Confirm in the overlay window that appears.

Command line

Use the gcloud storage buckets update command with the appropriate flag:

gcloud storage buckets update gs://BUCKET_NAME FLAG

Where:

  • BUCKET_NAME is the name of the relevant bucket. For example, my-bucket.

  • FLAG is one of the following:

    • --enable-autoclass to enable Autoclass.
    • --no-enable-autoclass to disable Autoclass.
    • --autoclass-terminal-storage-class=CLASS to set the terminal storage class for Autoclass. CLASS can be either NEARLINE or ARCHIVE.

If successful, the response looks similar to the following example:

Updating gs://my-bucket/...
  Completed 1  

Client libraries

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.

namespace gcs = ::google::cloud::storage;
[](gcs::Client client, std::string const& bucket_name, bool enabled) {
  auto metadata = client.PatchBucket(
      bucket_name, gcs::BucketMetadataPatchBuilder().SetAutoclass(
                       gcs::BucketAutoclass{enabled}));
  if (!metadata) throw google::cloud::Status(std::move(metadata).status());

  std::cout << "The autoclass configuration for bucket " << bucket_name
            << " was successfully updated.";
  if (!metadata->has_autoclass()) {
    std::cout << " The bucket no longer has an autoclass configuration.\n";
    return;
  }
  std::cout << " The new configuration is " << metadata->autoclass() << "\n";
}

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.


using Google.Cloud.Storage.V1;
using Google.Apis.Storage.v1.Data;
using static Google.Apis.Storage.v1.Data.Bucket;
using System;

public class SetAutoclassSample
{
    public Bucket SetAutoclass(string bucketName)
    {
        var storage = StorageClient.Create();
        var bucket = storage.GetBucket(bucketName);
        bucket.Autoclass = new AutoclassData { Enabled = true, TerminalStorageClass = "ARCHIVE" };
        storage.PatchBucket(bucket);
        Console.WriteLine($"Autoclass enabled is set to {bucket.Autoclass.Enabled} for {bucketName} at {bucket.Autoclass.ToggleTimeDateTimeOffset}.");
        Console.WriteLine($"Autoclass terminal storage class is {bucket.Autoclass.TerminalStorageClass}.");
        return bucket;
    }
}

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.

import (
	"context"
	"fmt"
	"io"
	"time"

	"cloud.google.com/go/storage"
)

// setAutoclass sets the Autoclass configuration for a bucket.
func setAutoclass(w io.Writer, bucketName string) error {
	// bucketName := "bucket-name"

	// To update the configuration for Autoclass.TerminalStorageClass,
	// Autoclass.Enabled must also be set to true.
	// To disable autoclass on the bucket, set to an empty &Autoclass{}.
	enabled := true
	terminalStorageClass := "ARCHIVE"
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %w", err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
	defer cancel()

	bucket := client.Bucket(bucketName)
	bucketAttrsToUpdate := storage.BucketAttrsToUpdate{
		Autoclass: &storage.Autoclass{
			Enabled:              enabled,
			TerminalStorageClass: terminalStorageClass,
		},
	}
	if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
		return fmt.Errorf("Bucket(%q).Update: %w", bucketName, err)
	}
	fmt.Fprintf(w, "Autoclass enabled was set to %v on bucket %q \n", bucketAttrsToUpdate.Autoclass.Enabled, bucketName)
	fmt.Fprintf(w, "Autoclass terminal storage class was last updated to %v at %v", bucketAttrsToUpdate.Autoclass.TerminalStorageClass, bucketAttrsToUpdate.Autoclass.TerminalStorageClassUpdateTime)
	return nil
}

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.


import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo.Autoclass;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.BucketTargetOption;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageOptions;

public class SetBucketAutoclass {
  public static void setBucketAutoclass(
      String projectId, String bucketName, StorageClass storageClass) throws Exception {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    // The storage class that objects in an Autoclass bucket eventually transition to if not read
    // for a certain length of time
    // StorageClass storageClass = StorageClass.ARCHIVE;

    // Configure the Autoclass setting for a bucket.

    // Note:  terminal_storage_class field is optional and defaults to NEARLINE if not otherwise
    // specified. Valid terminal_storage_class values are NEARLINE and ARCHIVE.
    boolean enabled = true;

    try (Storage storage =
        StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
      Bucket bucket = storage.get(bucketName);

      Bucket toUpdate =
          bucket
              .toBuilder()
              .setAutoclass(
                  Autoclass.newBuilder()
                      .setEnabled(enabled)
                      .setTerminalStorageClass(storageClass)
                      .build())
              .build();

      Bucket updated = storage.update(toUpdate, BucketTargetOption.metagenerationMatch());

      System.out.println(
          "Autoclass for bucket "
              + bucketName
              + " was "
              + (updated.getAutoclass().getEnabled() ? "enabled." : "disabled."));
      System.out.println(
          "Autoclass terminal storage class is "
              + updated.getAutoclass().getTerminalStorageClass().toString());
    }
  }
}

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.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// The terminal storage class to be set on your GCS bucket. Valid values are NEARLINE and ARCHIVE.
// const terminalStorageClass = 'NEARLINE';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function setAutoclass() {
  // Configure the Autoclass setting for a bucket.
  // terminalStorageClass field is optional and defaults to NEARLINE if not otherwise specified.
  // Valid terminalStorageClass values are NEARLINE and ARCHIVE.
  const [metadata] = await storage.bucket(bucketName).setMetadata({
    autoclass: {
      enabled: toggle,
      terminalStorageClass,
    },
  });

  console.log(
    `Autoclass terminal storage class is ${metadata.autoclass.terminalStorageClass}.`
  );
}

setAutoclass().catch(console.error);

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.

use Google\Cloud\Storage\StorageClient;

/**
 * Updates an existing bucket with provided autoclass config.
 *
 * @param string $bucketName The name of your Cloud Storage bucket (e.g. 'my-bucket').
 * @param bool $autoclassStatus If true, enables Autoclass. Disables otherwise.
 * @param string $terminalStorageClass This field is optional and defaults to `NEARLINE`.
 *        Valid values are `NEARLINE` and `ARCHIVE`.
 */
function set_bucket_autoclass(
    string $bucketName,
    bool $autoclassStatus,
    string $terminalStorageClass
): void {
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);

    $bucket->update([
        'autoclass' => [
            'enabled' => $autoclassStatus,
            'terminalStorageClass' => $terminalStorageClass
        ],
    ]);

    $info = $bucket->info();
    printf(
        'Updated bucket %s with autoclass set to %s.' . PHP_EOL,
        $info['name'],
        $autoclassStatus ? 'true' : 'false'
    );
    printf(
        'Autoclass terminal storage class is %s.' . PHP_EOL,
        $info['autoclass']['terminalStorageClass']
    );
}

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.

from google.cloud import storage


def set_autoclass(bucket_name):
    """Configure the Autoclass setting for a bucket.

    terminal_storage_class field is optional and defaults to NEARLINE if not otherwise specified.
    Valid terminal_storage_class values are NEARLINE and ARCHIVE.
    """
    # The ID of your GCS bucket
    # bucket_name = "my-bucket"
    # Enable Autoclass for a bucket. Set enabled to false to disable Autoclass.
    # Set Autoclass.TerminalStorageClass, valid values are NEARLINE and ARCHIVE.
    enabled = True
    terminal_storage_class = "ARCHIVE"

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)

    bucket.autoclass_enabled = enabled
    bucket.autoclass_terminal_storage_class = terminal_storage_class
    bucket.patch()
    print(f"Autoclass enabled is set to {bucket.autoclass_enabled} for {bucket.name} at {bucket.autoclass_toggle_time}.")
    print(f"Autoclass terminal storage class is {bucket.autoclass_terminal_storage_class}.")

    return bucket

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.

require "google/cloud/storage"

##
# This is a snippet for showcasing how to set the autoclass
# configuration of a bucket.
#
# @param bucket_name [String] The ID of your GCS bucket (e.g. "your-unique-bucket-name")
# @param toggle [Boolean] if true, enables Autoclass; if false, disables Autoclass
#
def set_autoclass bucket_name:, toggle:, terminal_storage_class: nil
  # Initialize client
  storage = Google::Cloud::Storage.new

  # Fetch the GCS bucket
  bucket = storage.bucket bucket_name

  # Update the autoclass configuration
  bucket.update_autoclass({ enabled: toggle, terminal_storage_class: terminal_storage_class })
  terminal_class_update_time = bucket.autoclass_terminal_storage_class_update_time

  # Get autoclass config of the bucket
  puts "Bucket #{bucket.name} has autoclass config set to #{bucket.autoclass_enabled}."
  puts "Bucket #{bucket.name} has autoclass toggle time set to #{bucket.autoclass_toggle_time}."
  puts "Bucket #{bucket.name} has autoclass terminal storage class set to #{bucket.autoclass_terminal_storage_class}."
  puts "Bucket #{bucket.name} has autoclass terminal storage class update time set to #{terminal_class_update_time}."
end

REST APIs

JSON API

  1. Have gcloud CLI installed and initialized, which lets you generate an access token for the Authorization header.

  2. Create a JSON file that contains the following information:

    {
      "storageClass": "DEFAULT_CLASS",
      "autoclass": {
        "enabled": BOOLEAN,
        "terminalStorageClass":TERMINAL_CLASS
      }
    }

    Where:

  3. Use cURL to call the JSON API with a PATCH Bucket request:

    curl -X PATCH --data-binary @JSON_FILE_NAME \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=autoclass"

    Where:

    • JSON_FILE_NAME is the path for the JSON file that you created in Step 2.
    • BUCKET_NAME is the name of the relevant bucket. For example, my-bucket.

XML API

This feature cannot be managed through the XML API. Use the Google Cloud console or another tool instead.

View a bucket's Autoclass status

To view the Autoclass status for an individual bucket:

Console

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets

  2. Click the name of the bucket whose status you want to check.

  3. Click the Configuration tab.

  4. The status for Autoclass is displayed in the Default storage class field.

    If Autoclass is enabled, the field reads Managed with Autoclass, and the Included classes field displays the storage classes that objects can transition between.

Command line

Use the gcloud storage buckets describe command with the --format flag:

gcloud storage buckets describe gs://BUCKET_NAME --format="default(autoclass)"

Where BUCKET_NAME is the name of the bucket whose status you want to view. For example, my-bucket.

If successful, the response looks similar to the following example:

autoclass:
  enabled: true
  terminalStorageClass: ARCHIVE
  terminalStorageClassUpdateTime: '2023-02-10T16:11:02.384000+00:00'
  toggleTime: '2022-11-09T16:01:40.775000+00:00'

Client libraries

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.

namespace gcs = ::google::cloud::storage;
[](gcs::Client client, std::string const& bucket_name) {
  auto metadata = client.GetBucketMetadata(bucket_name);
  if (!metadata) throw google::cloud::Status(std::move(metadata).status());

  if (!metadata->has_autoclass()) {
    std::cout << "The bucket " << metadata->name() << " does not have an"
              << " autoclass configuration.\n";
    return;
  }

  std::cout << "Autoclass is "
            << (metadata->autoclass().enabled ? "enabled" : "disabled")
            << " for bucket " << metadata->name() << ". "
            << " The bucket's full autoclass configuration is "
            << metadata->autoclass() << "\n";
}

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.


using Google.Cloud.Storage.V1;
using System;
using static Google.Apis.Storage.v1.Data.Bucket;

public class GetAutoclassSample
{
    public AutoclassData GetAutoclass(string bucketName)
    {
        var storage = StorageClient.Create();
        var bucket = storage.GetBucket(bucketName);
        Console.WriteLine($"Autoclass enabled is set to {bucket.Autoclass.Enabled} for {bucketName} at {bucket.Autoclass.ToggleTimeDateTimeOffset}.");
        Console.WriteLine($"Autoclass terminal storage class is set to {bucket.Autoclass.TerminalStorageClass} for {bucketName} at {bucket.Autoclass.TerminalStorageClassUpdateTimeDateTimeOffset}.");
        return bucket.Autoclass;
    }
}

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.

import (
	"context"
	"fmt"
	"io"
	"time"

	"cloud.google.com/go/storage"
)

// getAutoclass gets the Autoclass configuration for a bucket.
// See https://cloud.google.com/storage/docs/using-autoclass for more information.
func getAutoclass(w io.Writer, bucketName string) error {
	// bucketName := "bucket-name"
	ctx := context.Background()
	client, err := storage.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("storage.NewClient: %w", err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*10)
	defer cancel()

	attrs, err := client.Bucket(bucketName).Attrs(ctx)
	if err != nil {
		return fmt.Errorf("Bucket(%q).Attrs: %w", bucketName, err)
	}
	fmt.Fprintf(w, "Autoclass enabled was set to %v on bucket %q at %v", attrs.Autoclass.Enabled, bucketName, attrs.Autoclass.ToggleTime)
	fmt.Fprintf(w, "Autoclass terminal storage class was last updated to %v at %v", attrs.Autoclass.TerminalStorageClass, attrs.Autoclass.TerminalStorageClassUpdateTime)
	return nil
}

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.


import com.google.cloud.storage.BucketInfo.Autoclass;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageClass;
import com.google.cloud.storage.StorageOptions;
import java.time.OffsetDateTime;

public class GetBucketAutoclass {
  public static void getBucketAutoclass(String projectId, String bucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID of your GCS bucket
    // String bucketName = "your-unique-bucket-name";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Autoclass autoclass = storage.get(bucketName).getAutoclass();
    String status = autoclass.getEnabled() ? "enabled" : "disabled";
    String toggleTime = autoclass.getToggleTime().toString();
    StorageClass terminalStorageClass = autoclass.getTerminalStorageClass();
    OffsetDateTime terminalStorageClassUpdateTime = autoclass.getTerminalStorageClassUpdateTime();

    System.out.println(
        "Autoclass is currently "
            + status
            + " for bucket "
            + bucketName
            + " and was last changed at "
            + toggleTime
            + ". The terminal storage class is set to be "
            + terminalStorageClass.name()
            + " last updated at "
            + terminalStorageClassUpdateTime.toString());
  }
}

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.

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

async function getAutoclass() {
  const [metadata] = await storage.bucket(bucketName).getMetadata();
  console.log(
    `Autoclass is ${
      metadata.autoclass.enabled ? 'enabled' : 'disabled'
    } for ${metadata.name} at ${metadata.autoclass.toggleTime}.
 Autoclass terminal storage class is last updated to ${
   metadata.autoclass.terminalStorageClass
 } at ${metadata.autoclass.terminalStorageClassUpdateTime}.`
  );
}

getAutoclass().catch(console.error);

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.

use Google\Cloud\Storage\StorageClient;

/**
 * Print a bucket autoclass configuration.
 *
 * @param string $bucketName The name of your Cloud Storage bucket (e.g. 'my-bucket').
 */
function get_bucket_autoclass(string $bucketName): void
{
    $storage = new StorageClient();
    $bucket = $storage->bucket($bucketName);

    $info = $bucket->info();

    if (isset($info['autoclass'])) {
        printf('Bucket %s has autoclass enabled: %s' . PHP_EOL,
            $bucketName,
            $info['autoclass']['enabled']
        );
        printf('Bucket %s has autoclass toggle time: %s' . PHP_EOL,
            $bucketName,
            $info['autoclass']['toggleTime']
        );
        printf(
            'Autoclass terminal storage class is set to %s for %s at %s.' . PHP_EOL,
            $info['autoclass']['terminalStorageClass'],
            $info['name'],
            $info['autoclass']['terminalStorageClassUpdateTime'],
        );
    }
}

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.

from google.cloud import storage


def get_autoclass(bucket_name):
    """Get the Autoclass setting for a bucket."""
    # The ID of your GCS bucket
    # bucket_name = "my-bucket"

    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    autoclass_enabled = bucket.autoclass_enabled
    autoclass_toggle_time = bucket.autoclass_toggle_time
    terminal_storage_class = bucket.autoclass_terminal_storage_class
    tsc_update_time = bucket.autoclass_terminal_storage_class_update_time

    print(f"Autoclass enabled is set to {autoclass_enabled} for {bucket.name} at {autoclass_toggle_time}.")
    print(f"Autoclass terminal storage class is set to {terminal_storage_class} for {bucket.name} at {tsc_update_time}.")

    return bucket

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.

require "google/cloud/storage"

##
# This is a snippet for showcasing how to access the autoclass
# configuration of a bucket.
#
# @param bucket_name [String] The ID of your GCS bucket (e.g. "your-unique-bucket-name")
#
def get_autoclass bucket_name:
  # Initialize client
  storage = Google::Cloud::Storage.new

  # Fetch the GCS bucket
  bucket = storage.bucket bucket_name
  terminal_class_update_time = bucket.autoclass_terminal_storage_class_update_time

  # Get autoclass config of the bucket
  puts "Bucket #{bucket.name} has autoclass config set to #{bucket.autoclass_enabled}."
  puts "Bucket #{bucket.name} has autoclass toggle time set to #{bucket.autoclass_toggle_time}."
  puts "Bucket #{bucket.name} has autoclass terminal storage class set to #{bucket.autoclass_terminal_storage_class}."
  puts "Bucket #{bucket.name} has autoclass terminal storage class update time set to #{terminal_class_update_time}."
end

REST APIs

JSON API

  1. Have gcloud CLI installed and initialized, which lets you generate an access token for the Authorization header.

  2. Use cURL to call the JSON API with a GET Bucket request that includes the autoclass field:

    curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME?fields=autoclass"

    Where BUCKET_NAME is the name of the relevant bucket. For example, my-bucket.

    If the bucket has Autoclass enabled, the response looks like the following example:

    {
      "autoclass": {
          "enabled": "BOOLEAN",
          "toggleTime": "DATETIME",
          "terminalStorageClass": "STORAGE_CLASS",
          "terminalStorageClassUpdateTime": "DATETIME"
       },
    }

XML API

This feature cannot be managed through the XML API. Use the Google Cloud console or another tool instead.

What's next