The Requester Pays feature lets you shift network usage costs and data
processing costs to the users who access your Cloud Storage data. You can
enable or disable this feature on a bucket, check its status, and specify a
billing project to use when accessing Requester Pays buckets.

For background information, see [Requester Pays](https://docs.cloud.google.com/storage/docs/requester-pays).

## Required roles

In order to get the required permissions for setting and managing Requester
Pays, ask your administrator to grant you the Storage Admin
(`roles.storage.Admin`) role on the project that contains the bucket.

This role contains the permissions required to set and manage Requester Pays.
To see the exact permissions that are required, expand the
**Required permissions** section:

#### Required permissions

- `storage.buckets.get`
- `storage.buckets.update`
- `resourcemanager.projects.createBillingAssignment`
  - This permission is only required if you don't have a billing account to use when disabling Requester Pays. For more information, see [Use and access requirements](https://docs.cloud.google.com/storage/docs/requester-pays#requirements).

You might also be able to get these permissions with other
[predefined roles](https://docs.cloud.google.com/iam/docs/understanding-roles) or [custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles).

For instructions on granting roles on projects, see
[Grant or revoke a role](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access#grant-single-role).

## Set Requester Pays

To enable or disable Requester Pays on a bucket:

### Console

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

   [Go to Buckets](https://console.cloud.google.com/storage/browser)
2. In the list of buckets, find the bucket you want to set and locate the
   **Requester pays** column.

   The value in the column indicates the current state of Requester Pays
   for that bucket.
3. Click the current state of Requester Pays for the bucket.

4. In the window that appears, click **Turn on** or **Turn off**, depending
   on the state you want to set for Requester Pays.

When enabled, a green bubble and **On** appear in the **Requester pays**
column for the bucket. When disabled, a gray bubble and **Off** appear in
the column.

To learn how to get detailed error information about failed Cloud Storage
operations in the Google Cloud console, see
[Troubleshooting](https://docs.cloud.google.com/storage/docs/troubleshooting#trouble-console).

### Command line

Use the [`gcloud storage buckets update`](https://docs.cloud.google.com/sdk/gcloud/reference/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 either `--requester-pays` to enable
  Requester Pays or `--no-requester-pays` to disable it.

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](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name) {
      StatusOr<gcs::BucketMetadata> metadata = client.PatchBucket(
          bucket_name,
          gcs::BucketMetadataPatchBuilder().SetBilling(gcs::BucketBilling{true}));
      if (!metadata) throw std::move(metadata).status();

      std::cout << "Billing configuration for bucket " << metadata->name()
                << " is updated. The bucket now";
      if (!metadata->has_billing()) {
        std::cout << " has no billing configuration.\n";
      } else if (metadata->billing().requester_pays) {
        std::cout << " is configured to charge the caller for requests\n";
      } else {
        std::cout << " is configured to charge the project that owns the bucket"
                  << " for requests.\n";
      }
    }

The following sample disables Requester Pays on a bucket:

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name,
       std::string const& billed_project) {
      StatusOr<gcs::BucketMetadata> metadata = client.PatchBucket(
          bucket_name,
          gcs::BucketMetadataPatchBuilder().SetBilling(gcs::BucketBilling{false}),
          gcs::UserProject(billed_project));
      if (!metadata) throw std::move(metadata).status();

      std::cout << "Billing configuration for bucket " << bucket_name
                << " is updated. The bucket now";
      if (!metadata->has_billing()) {
        std::cout << " has no billing configuration.\n";
      } else if (metadata->billing().requester_pays) {
        std::cout << " is configured to charge the caller for requests\n";
      } else {
        std::cout << " is configured to charge the project that owns the bucket"
                  << " for requests.\n";
      }
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class EnableRequesterPaysSample
    {
        public Bucket EnableRequesterPays(string bucketName = "your-unique-bucket-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            var bucket = storage.GetBucket(bucketName);
            bucket.Billing ??= new Bucket.BillingData();
            bucket.Billing.RequesterPays = true;
            bucket = storage.UpdateBucket(bucket);
            Console.WriteLine($"Requester pays requests have been enabled for bucket {bucketName}.");
            return bucket;
        }
    }

The following sample disables Requester Pays on a bucket:


    using Google.Apis.Storage.v1.Data;
    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class DisableRequesterPaysSample
    {
        public Bucket DisableRequesterPays(
            string projectId = "your-project-id",
            string bucketName = "your-unique-bucket-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            var bucket = storage.GetBucket(bucketName, new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.GetBucketOptions.html
            {
                UserProject = projectId
            });

            bucket.Billing ??= new Bucket.BillingData();
            bucket.Billing.RequesterPays = false;

            bucket = storage.UpdateBucket(bucket, new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.UpdateBucketOptions.html
            {
                UserProject = projectId
            });
            Console.WriteLine($"Requester pays disabled for bucket {bucketName}.");
            return bucket;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

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

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

    // enableRequesterPays sets requester pays flag to true.
    func enableRequesterPays(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_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()

    	bucket := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName)
    	bucketAttrsToUpdate := storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketAttrsToUpdate{
    		RequesterPays: true,
    	}
    	if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
    		return fmt.Errorf("Bucket(%q).Update: %w", bucketName, err)
    	}
    	fmt.Fprintf(w, "Requester pays enabled for bucket %v\n", bucketName)
    	return nil
    }

The following sample disables Requester Pays on a bucket:

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

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

    // disableRequesterPays sets requester pays flag to false.
    func disableRequesterPays(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_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()

    	bucket := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName)
    	bucketAttrsToUpdate := storage.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketAttrsToUpdate{
    		RequesterPays: false,
    	}
    	if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
    		return fmt.Errorf("Bucket(%q).Update: %w", bucketName, err)
    	}
    	fmt.Fprintf(w, "Requester pays disabled for bucket %v\n", bucketName)
    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;

    public class EnableRequesterPays {
      public static void enableRequesterPays(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";

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(bucketName);
        bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html#com_google_cloud_storage_Bucket_toBuilder__().setRequesterPays(true).build().update();

        System.out.println("Requester pays enabled for bucket " + bucketName);
      }
    }

The following sample disables Requester Pays on a bucket:

    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;

    public class DisableRequesterPays {
      public static void disableRequesterPays(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";

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket = storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(bucketName, Storage.BucketGetOption.userProject(projectId));
        bucket.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html#com_google_cloud_storage_Bucket_toBuilder__()
            .setRequesterPays(false)
            .build()
            .update(https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html.BucketTargetOption.userProject(projectId));

        System.out.println("Requester pays disabled for bucket " + bucketName);
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

    /**
     * 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('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function enableRequesterPays() {
      await storage.bucket(bucketName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html();

      console.log(
        `Requester-pays requests have been enabled for bucket ${bucketName}`
      );
    }

    enableRequesterPays().catch(console.error);

The following sample disables Requester Pays on a bucket:


    /**
     * 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('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function disableRequesterPays() {
      // Disables requester-pays requests
      await storage.bucket(bucketName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/bucket.html();

      console.log(
        `Requester-pays requests have been disabled for bucket ${bucketName}`
      );
    }

    disableRequesterPays().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

    use Google\Cloud\Storage\StorageClient;

    /**
     * Enable a bucket's requesterpays metadata.
     *
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     */
    function enable_requester_pays(string $bucketName): void
    {
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);
        $bucket->update([
            'billing' => [
                'requesterPays' => true
            ]
        ]);
        printf('Requester pays has been enabled for %s' . PHP_EOL, $bucketName);
    }

The following sample disables Requester Pays on a bucket:

    use Google\Cloud\Storage\StorageClient;

    /**
     * Disable a bucket's requesterpays metadata.
     *
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     */
    function disable_requester_pays(string $bucketName): void
    {
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);
        $bucket->update([
            'billing' => [
                'requesterPays' => false
            ]
        ]);
        printf('Requester pays has been disabled for %s' . PHP_EOL, $bucketName);
    }

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def enable_requester_pays(bucket_name):
        """Enable a bucket's requesterpays metadata"""
        # bucket_name = "my-bucket"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()

        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)
        bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_requester_pays = True
        bucket.patch()

        print(f"Requester Pays has been enabled for {bucket_name}")

The following sample disables Requester Pays on a bucket:

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def disable_requester_pays(bucket_name):
        """Disable a bucket's requesterpays metadata"""
        # bucket_name = "my-bucket"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()

        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)
        bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_requester_pays = False
        bucket.patch()

        print(f"Requester Pays has been disabled for {bucket_name}")

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

The following sample enables Requester Pays on a bucket:

    def enable_requester_pays bucket_name:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      require "google/cloud/storage"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name

      bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html = true

      puts "Requester pays has been enabled for #{bucket_name}"
    end

The following sample disables Requester Pays on a bucket:

    def disable_requester_pays bucket_name:
      # The ID of your GCS bucket
      # bucket_name = "your-unique-bucket-name"

      require "google/cloud/storage"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name

      bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html = false

      puts "Requester pays has been disabled for #{bucket_name}"
    end

### Rust

The following sample enables Requester Pays on a bucket:

    use google_cloud_storage::client::StorageControl;
    use google_cloud_storage::model::bucket::Billing;
    use google_cloud_wkt::FieldMask;

    pub async fn sample(client: &StorageControl, bucket_id: &str) -> anyhow::Result<()> {
        let bucket = client
            .get_bucket()
            .set_name(format!("projects/_/buckets/{bucket_id}"))
            .send()
            .await?;
        let metageneration = bucket.metageneration;
        let bucket = client
            .update_bucket()
            .set_bucket(bucket.set_billing(Billing::new().set_requester_pays(true)))
            .set_if_metageneration_match(metageneration)
            .set_update_mask(FieldMask::default().set_paths(["billing.requester_pays"]))
            .send()
            .await?;
        println!(
            "Requester pays was enabled for bucket {bucket_id}: {:?}",
            bucket.billing
        );
        Ok(())
    }

The following sample disables Requester Pays on a bucket:

    use google_cloud_gax::options::RequestOptionsBuilder;
    use google_cloud_storage::client::StorageControl;
    use google_cloud_storage::model::bucket::Billing;
    use google_cloud_wkt::FieldMask;

    pub async fn sample(
        client: &StorageControl,
        bucket_id: &str,
        user_project: &str,
    ) -> anyhow::Result<()> {
        let bucket = client
            .get_bucket()
            .set_name(format!("projects/_/buckets/{bucket_id}"))
            .with_quota_project(user_project)
            .send()
            .await?;
        let metageneration = bucket.metageneration;
        let bucket = client
            .update_bucket()
            .set_bucket(bucket.set_billing(Billing::new().set_requester_pays(false)))
            .set_if_metageneration_match(metageneration)
            .set_update_mask(FieldMask::default().set_paths(["billing.requester_pays"]))
            .with_quota_project(user_project)
            .send()
            .await?;
        println!(
            "Requester pays was disabled for bucket {bucket_id}: {:?}",
            bucket.billing
        );
        Ok(())
    }

<br />

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

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

   ```json
   {
     "billing": {
       "requesterPays": STATE
     }
   }
   ```

   Where <var translate="no">STATE</var> is either `true` or `false`.
3. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`PATCH` Bucket](https://docs.cloud.google.com/storage/docs/json_api/v1/buckets/patch) 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=billing"
   ```

   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

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Create an XML file that contains the following information:

   ```
   <BillingConfiguration>
     <RequesterPays>STATE</RequesterPays>
   </BillingConfiguration>
   ```

   Where <var translate="no">STATE</var> is either `Enabled` or `Disabled`.
3. Use [`cURL`](http://curl.haxx.se/) to call the [XML API](https://docs.cloud.google.com/storage/docs/xml-api/overview) with a
   [`PUT` Bucket](https://docs.cloud.google.com/storage/docs/xml-api/put-bucket-metadata) request and `billing` query string parameter:

   ```
   curl -X PUT --data-binary @XML_FILE_NAME \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/BUCKET_NAME?billing"
   ```

   Where:
   - `XML_FILE_NAME` is the path for the XML file that you created in Step 2.
   - `BUCKET_NAME` is the name of the relevant bucket. For example, `my-bucket`.

## Check whether Requester Pays is enabled

To check whether Requester Pays is enabled on a bucket:

### Console

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

   [Go to Buckets](https://console.cloud.google.com/storage/browser)
2. In the list of buckets, the Requester Pays status of each bucket is
   found in the **Requester Pays** column.

If enabled, the status is green and the word **On** appears.

### Command line

Use the [`gcloud storage buckets describe`](https://docs.cloud.google.com/sdk/gcloud/reference/storage/buckets/describe) command with the
`--format` flag:

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

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:

```json
requester_pays: true
```

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    using ::google::cloud::StatusOr;
    [](gcs::Client client, std::string const& bucket_name,
       std::string const& user_project) {
      StatusOr<gcs::BucketMetadata> metadata =
          client.GetBucketMetadata(bucket_name, gcs::UserProject(user_project));
      if (!metadata) throw std::move(metadata).status();

      if (!metadata->has_billing()) {
        std::cout
            << "The bucket " << metadata->name() << " does not have a"
            << " billing configuration. The default applies, i.e., the project"
            << " that owns the bucket pays for the requests.\n";
        return;
      }

      if (metadata->billing().requester_pays) {
        std::cout
            << "The bucket " << metadata->name()
            << " is configured to charge the calling project for the requests.\n";
      } else {
        std::cout << "The bucket " << metadata->name()
                  << " is configured to charge the project that owns the bucket "
                     "for the requests.\n";
      }
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;

    public class GetRequesterPaysStatusSample
    {
        public bool GetRequesterPaysStatus(
            string projectId = "your-project-id",
            string bucketName = "your-unique-bucket-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            var bucket = storage.GetBucket(bucketName, new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.GetBucketOptions.html
            {
                UserProject = projectId
            });
            bool requesterPays = bucket.Billing?.RequesterPays ?? false;
            Console.WriteLine($"RequesterPays: {requesterPays}");
            return requesterPays;
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

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

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

    // getRequesterPaysStatus gets requester pays status.
    func getRequesterPaysStatus(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_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.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucketName).Attrs(ctx)
    	if err != nil {
    		return fmt.Errorf("Bucket(%q).Attrs: %w", bucketName, err)
    	}
    	fmt.Fprintf(w, "Is requester pays enabled? %v\n", attrs.RequesterPays)
    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageException.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;

    public class GetRequesterPaysStatus {
      public static void getRequesterPaysStatus(String projectId, String bucketName)
          throws https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageException.html {
        // The ID of your GCP project
        // String projectId = "your-project-id";

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

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.newBuilder().setProjectId(projectId).build().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Bucket.html bucket =
            storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(bucketName, Storage.BucketGetOption.fields(Storage.BucketField.BILLING));

        System.out.println("Requester pays status : " + bucket.requesterPays());
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    /**
     * 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('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function getRequesterPaysStatus() {
      // Gets the requester-pays status of a bucket
      const [metadata] = await storage.bucket(bucketName).getMetadata();

      let status;
      if (metadata && metadata.billing && metadata.billing.requesterPays) {
        status = 'enabled';
      } else {
        status = 'disabled';
      }
      console.log(
        `Requester-pays requests are ${status} for bucket ${bucketName}.`
      );
    }

    getRequesterPaysStatus().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    use Google\Cloud\Storage\StorageClient;

    /**
     * Get a bucket's requesterpays metadata.
     *
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     */
    function get_requester_pays_status(string $bucketName): void
    {
        $storage = new StorageClient();
        $bucket = $storage->bucket($bucketName);
        $bucketInformation = $bucket->info();
        $requesterPaysStatus = $bucketInformation['billing']['requesterPays'];
        if ($requesterPaysStatus) {
            printf('Requester Pays is enabled for %s' . PHP_EOL, $bucketName);
        } else {
            printf('Requester Pays is disabled for %s' . PHP_EOL, $bucketName);
        }
    }

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def get_requester_pays_status(bucket_name):
        """Get a bucket's requester pays metadata"""
        # bucket_name = "my-bucket"
        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()

        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_get_bucket(bucket_name)
        requester_pays_status = bucket.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.bucket.Bucket.html#google_cloud_storage_bucket_Bucket_requester_pays

        if requester_pays_status:
            print(f"Requester Pays is enabled for {bucket_name}")
        else:
            print(f"Requester Pays is disabled for {bucket_name}")

### Rust

    use google_cloud_gax::options::RequestOptionsBuilder;
    use google_cloud_storage::client::StorageControl;

    pub async fn sample(
        client: &StorageControl,
        bucket_id: &str,
        user_project: &str,
    ) -> anyhow::Result<()> {
        let bucket = client
            .get_bucket()
            .set_name(format!("projects/_/buckets/{bucket_id}"))
            .with_quota_project(user_project)
            .send()
            .await?;
        println!(
            "Bucket {bucket_id} has billing status: {:?}",
            bucket.billing
        );
        Ok(())
    }

<br />

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [JSON API](https://docs.cloud.google.com/storage/docs/json_api) with a
   [`GET` Bucket](https://docs.cloud.google.com/storage/docs/json_api/v1/buckets/get) request:

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

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

### XML API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. Use [`cURL`](http://curl.haxx.se/) to call the [XML API](https://docs.cloud.google.com/storage/docs/xml-api/overview) with a
   [`GET` Bucket](https://docs.cloud.google.com/storage/docs/xml-api/get-bucket-metadata) request and `billing` query string parameter:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     "https://storage.googleapis.com/BUCKET_NAME?billing"
   ```

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

## Access Requester Pays buckets

The following example shows how to include a billing project so that you
can [download an object](https://docs.cloud.google.com/storage/docs/downloading-objects) stored in a Requester Pays bucket. Use a similar
procedure to perform other requests on the Requester Pays bucket or on objects
within it. See [Requester Pays access requirements](https://docs.cloud.google.com/storage/docs/requester-pays#requirements) for prerequisite
considerations.

> [!IMPORTANT]
> **Important:** Buckets that have Requester Pays disabled still accept requests that include a billing project, and charges are applied to the billing project supplied in the request. Consider any billing implications prior to including a billing project in all of your requests.

### Console

> [!NOTE]
> **Note:** If you previously used the Google Cloud console to access a given Requester Pays bucket, you have immediate access to the bucket and can download the object as you normally would. The project you previously specified as the billing project is used for this subsequent access. You can change the project you bill by clicking the **Change project** button located above the list of objects in the bucket.

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

   [Go to Buckets](https://console.cloud.google.com/storage/browser)
2. In the list of buckets, click the name of the bucket that contains
   the object you want to download.

3. In the window that appears, use the drop-down menu to select a project
   for billing.

4. Check the checkbox to confirm you are authorized to use the selected
   project for billing purposes.

5. Click **Save**.

6. [Download the object](https://docs.cloud.google.com/storage/docs/downloading-objects) as you normally would.

To learn how to get detailed error information about failed Cloud Storage
operations in the Google Cloud console, see
[Troubleshooting](https://docs.cloud.google.com/storage/docs/troubleshooting#trouble-console).

### Command line

> [!NOTE]
> **Note:** The `--billing-project` flag also sets the project against which quota is charged.

Use the [`--billing-project`](https://docs.cloud.google.com/sdk/gcloud/reference#--billing-project) flag in your request:

```
gcloud storage cp gs://BUCKET_NAME/OBJECT_NAME SAVE_TO_LOCATION --billing-project=PROJECT_IDENTIFIER
```

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 object you are downloading. For example, `pets/dog.png`.
- `SAVE_TO_LOCATION` is the local path where you are saving your object. For example, `Desktop/Images`.
- `PROJECT_IDENTIFIER` is the ID or number of the project to be billed. For example, `my-project`.

### Client libraries


### C++


For more information, see the
[Cloud Storage C++ API
reference documentation](https://docs.cloud.google.com/cpp/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    namespace gcs = ::google::cloud::storage;
    [](gcs::Client client, std::string const& bucket_name,
       std::string const& object_name, std::string const& billed_project) {
      gcs::ObjectReadStream stream = client.ReadObject(
          bucket_name, object_name, gcs::UserProject(billed_project));

      std::string line;
      while (std::getline(stream, line, '\n')) {
        std::cout << line << "\n";
      }
      if (stream.bad()) throw google::cloud::Status(stream.status());
    }

### C#


For more information, see the
[Cloud Storage C# API
reference documentation](https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    using https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.html;
    using System;
    using System.IO;

    public class DownloadFileRequesterPaysSample
    {
        public void DownloadFileRequesterPays(
            string projectId = "your-project-id",
            string bucketName = "your-unique-bucket-name",
            string objectName = "my-file-name",
            string localPath = "my-local-path/my-file-name")
        {
            var storage = https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html.https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.StorageClient.html#Google_Cloud_Storage_V1_StorageClient_Create();
            using var outputFile = File.OpenWrite(localPath);
            storage.DownloadObject(bucketName, objectName, outputFile, new https://docs.cloud.google.com/dotnet/docs/reference/Google.Cloud.Storage.V1/latest/Google.Cloud.Storage.V1.DownloadObjectOptions.html
            {
                UserProject = projectId
            });
            Console.WriteLine($"Downloaded {objectName} to {localPath} paid by {projectId}.");
        }
    }

### Go


For more information, see the
[Cloud Storage Go API
reference documentation](https://pkg.go.dev/cloud.google.com/go/storage).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

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

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

    // downloadUsingRequesterPays downloads an object using billing project.
    func downloadUsingRequesterPays(w io.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Writer, bucket, object, billingProjectID string) error {
    	// bucket := "bucket-name"
    	// object := "object-name"
    	// billingProjectID := "billing_account_id"
    	ctx := context.Background()
    	client, err := storage.NewClient(ctx)
    	if err != nil {
    		return fmt.Errorf("storage.NewClient: %w", err)
    	}
    	defer client.Close()

    	b := client.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_Client_Bucket(bucket).https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketHandle_UserProject(billingProjectID)
    	src := b.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_BucketHandle_Object(object)

    	// Open local file.
    	f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
    	if err != nil {
    		return fmt.Errorf("os.OpenFile: %w", err)
    	}

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

    	rc, err := src.https://docs.cloud.google.com/go/docs/reference/cloud.google.com/go/storage/latest/index.html#cloud_google_com_go_storage_ObjectHandle_NewReader(ctx)
    	if err != nil {
    		return fmt.Errorf("Object(%q).NewReader: %w", object, err)
    	}
    	if _, err := io.Copy(f, rc); err != nil {
    		return fmt.Errorf("io.Copy: %w", err)
    	}
    	if err := rc.Close(); err != nil {
    		return fmt.Errorf("Reader.Close: %w", err)
    	}
    	fmt.Fprintf(w, "Downloaded using %v as billing project.\n", billingProjectID)
    	return nil
    }

### Java


For more information, see the
[Cloud Storage Java API
reference documentation](https://cloud.google.com/java/docs/reference/google-cloud-storage/latest/overview).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Blob.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.BlobId.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html;
    import com.google.cloud.storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html;
    import java.nio.file.Path;

    public class DownloadRequesterPaysObject {
      public static void downloadRequesterPaysObject(
          String projectId, String bucketName, String objectName, Path destFilePath) {
        // The project ID to bill
        // String projectId = "my-billable-project-id";

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

        // The ID of your GCS object
        // String objectName = "your-object-name";

        // The path to which the file should be downloaded
        // Path destFilePath = Paths.get("/local/path/to/file.txt");

        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html storage = https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.StorageOptions.html.getDefaultInstance().https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.transfermanager.TransferManagerConfig.html#com_google_cloud_storage_transfermanager_TransferManagerConfig_getService__();
        https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Blob.html blob =
            storage.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Storage.html#com_google_cloud_storage_Storage_get_com_google_cloud_storage_BlobId_(
                BlobId.of(bucketName, objectName), Storage.BlobGetOption.userProject(projectId));
        blob.https://docs.cloud.google.com/java/docs/reference/google-cloud-storage/latest/com.google.cloud.storage.Blob.html#com_google_cloud_storage_Blob_downloadTo_java_io_OutputStream_com_google_cloud_storage_Blob_BlobSourceOption____(destFilePath, Blob.BlobSourceOption.userProject(projectId));

        System.out.println(
            "Object " + objectName + " downloaded to " + destFilePath + " and billed to " + projectId);
      }
    }

### Node.js


For more information, see the
[Cloud Storage Node.js API
reference documentation](https://cloud.google.com/nodejs/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).


    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    // The project ID to bill
    // const projectId = 'my-billable-project-id';

    // The ID of your GCS bucket
    // const bucketName = 'your-unique-bucket-name';

    // The ID of your GCS file
    // const srcFileName = 'your-file-name';

    // The path to which the file should be downloaded
    // const destFileName = '/local/path/to/file.txt';

    // Imports the Google Cloud client library
    const {Storage} = require('https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/overview.html');

    // Creates a client
    const storage = new https://docs.cloud.google.com/nodejs/docs/reference/storage-control/latest/storage-control/protos.google.storage.v2.storage-class.html();

    async function downloadFileUsingRequesterPays() {
      const options = {
        destination: destFileName,
        userProject: projectId,
      };

      // Downloads the file
      await storage.bucket(bucketName).file(srcFileName).https://docs.cloud.google.com/nodejs/docs/reference/storage/latest/storage/file_2.html(options);

      console.log(
        `gs://${bucketName}/${srcFileName} downloaded to ${destFileName} using requester-pays requests`
      );
    }

    downloadFileUsingRequesterPays().catch(console.error);

### PHP


For more information, see the
[Cloud Storage PHP API
reference documentation](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/latest/storage/storageclient).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    use Google\Cloud\Storage\StorageClient;

    /**
     * Download file using specified project as requester
     *
     * @param string $projectId The ID of your Google Cloud Platform project.
     *        (e.g. 'my-project-id')
     * @param string $bucketName The name of your Cloud Storage bucket.
     *        (e.g. 'my-bucket')
     * @param string $objectName The name of your Cloud Storage object.
     *        (e.g. 'my-object')
     * @param string $destination The local destination to save the object.
     *        (e.g. '/path/to/your/file')
     */
    function download_file_requester_pays(string $projectId, string $bucketName, string $objectName, string $destination): void
    {
        $storage = new StorageClient([
            'projectId' => $projectId
        ]);
        $userProject = true;
        $bucket = $storage->bucket($bucketName, $userProject);
        $object = $bucket->object($objectName);
        $object->downloadToFile($destination);
        printf('Downloaded gs://%s/%s to %s using requester-pays requests.' . PHP_EOL,
            $bucketName, $objectName, basename($destination));
    }

### Python


For more information, see the
[Cloud Storage Python API
reference documentation](https://cloud.google.com/python/docs/reference/storage/latest).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    from google.cloud import https://docs.cloud.google.com/python/docs/reference/storage/latest


    def download_file_requester_pays(
        bucket_name, project_id, source_blob_name, destination_file_name
    ):
        """Download file using specified project as the requester"""
        # bucket_name = "your-bucket-name"
        # project_id = "your-project-id"
        # source_blob_name = "source-blob-name"
        # destination_file_name = "local-destination-file-name"

        storage_client = https://docs.cloud.google.com/python/docs/reference/storage/latest.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html()

        bucket = storage_client.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.client.Client.html#google_cloud_storage_client_Client_bucket(bucket_name, user_project=project_id)
        blob = bucket.blob(source_blob_name)
        blob.https://docs.cloud.google.com/python/docs/reference/storage/latest/google.cloud.storage.blob.Blob.html#google_cloud_storage_blob_Blob_download_to_filename(destination_file_name)

        print(
            "Blob {} downloaded to {} using a requester-pays request.".format(
                source_blob_name, destination_file_name
            )
        )

### Ruby


For more information, see the
[Cloud Storage Ruby API
reference documentation](https://googleapis.dev/ruby/google-cloud-storage/latest/Google/Cloud/Storage.html).


To authenticate to Cloud Storage, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/storage/docs/authentication#client-libs).

    def download_file_requester_pays bucket_name:, file_name:, local_file_path:
      # The ID of a GCS bucket
      # bucket_name = "your-unique-bucket-name"

      # The ID of a GCS object
      # file_name = "your-file-name"

      # The path to which the file should be downloaded
      # local_file_path = "/local/path/to/file.txt"

      require "google/cloud/storage"

      storage = Google::Cloud::https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage-control/latest/Google-Cloud-Storage.html.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage.html
      bucket  = storage.bucket bucket_name, skip_lookup: true, user_project: true
      file    = bucket.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Bucket.html file_name

      file.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-File.html local_file_path

      puts "Downloaded #{file.name} using billing project #{storage.https://docs.cloud.google.com/ruby/docs/reference/google-cloud-storage/latest/Google-Cloud-Storage-Project.html}"
    end

<br />

### REST APIs

### JSON API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. In your request, include the `userProject` query string parameter set
   to the ID of the project to be billed:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -o "SAVE_TO_LOCATION" \
     "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME?alt=media&userProject=PROJECT_IDENTIFIER"
   ```

   Where:
   - `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 relevant bucket. For example, `my-bucket`.
   - `OBJECT_NAME` is the URL-encoded name of the object you want to download. For example, `pets/dog.png`, URL-encoded as `pets%2Fdog.png`.
   - `PROJECT_IDENTIFIER` is the ID or number of the project to be billed. For example, `my-project`.

### XML API

1. Have gcloud CLI [installed and initialized](https://docs.cloud.google.com/sdk/docs/install), which lets
   you generate an access token for the `Authorization` header.

2. In your request, include the `x-goog-user-project` header set to the
   ID of the project to be billed:

   ```
   curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "x-goog-user-project: PROJECT_ID" \
     -o "SAVE_TO_LOCATION" \
     "https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME"
   ```

   Where:
   - `PROJECT_ID` is the ID of the project to be billed. For example, `my-project`.
   - `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 relevant bucket. For example, `my-bucket`.
   - `OBJECT_NAME` is the URL-encoded name of the object you want to download. For example, `pets/dog.png`, URL-encoded as `pets%2Fdog.png`.

## What's next

- Learn more about [Requester Pays](https://docs.cloud.google.com/storage/docs/requester-pays).
- [Make data publicly available](https://docs.cloud.google.com/storage/docs/access-control/making-data-public).