> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Accessing GCS data securely

> This article demonstrates how ClickHouse Cloud customers can access their GCS data securely

export const Image = ({img, alt, size}) => {
  return <Frame>
      <img src={img} alt={alt} />
    </Frame>;
};

This guide demonstrates how to securely authenticate with Google Cloud Storage (GCS) and access your data from ClickHouse Cloud.

<h2 id="introduction">
  Introduction
</h2>

ClickHouse Cloud connects to GCS using HMAC (Hash-based Message Authentication Code) keys associated with a Google Cloud service account.
This approach provides secure access to your GCS buckets without embedding credentials directly in your queries.

How it works:

1. You create a Google Cloud service account with appropriate GCS permissions
2. You generate HMAC keys for that service account
3. You provide these HMAC credentials to ClickHouse Cloud
4. ClickHouse Cloud uses these credentials to access your GCS buckets

This approach allows you to manage all access to GCS buckets through IAM policies on the service account, making it easier to grant or revoke access without modifying individual bucket policies.

<h2 id="prerequisites">
  Prerequisites
</h2>

For following this guide you will need:

* An active ClickHouse Cloud service
* A Google Cloud project with Cloud Storage enabled
* Permissions to create service accounts and generate HMAC keys in your GCP project

<h2 id="setup">
  Setup
</h2>

<Steps>
  <Step>
    <h3 id="create-gcs-account">
      Create a Google Cloud service account
    </h3>

    1. In the Google Cloud Console, navigate to IAM & Admin → Service Accounts

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/IAM_and_admin.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=8896778362bedf8bb825b577fc9e52f2" size="md" alt="" width="2248" height="1254" data-path="images/cloud/guides/accessing-data/GCS/IAM_and_admin.png" />

    2. Click `Service accounts` from the left-hand menu, then click `Create service account`:

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/create_service_account.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=fce2bb665af76f66b1906596a0965104" size="md" alt="" width="2416" height="1356" data-path="images/cloud/guides/accessing-data/GCS/create_service_account.png" />

    Enter a name and description for your service account, for example:

    ```text theme={null}
    Service account name: clickhouse-gcs-access (or your preferred name)
    Service account description: Service account for ClickHouse Cloud to access GCS buckets
    ```

    Click `Create and continue`

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/create_and_continue.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=59f587c1f9c25b3e1a6af2c600b76908" size="sm" alt="" width="1152" height="1052" data-path="images/cloud/guides/accessing-data/GCS/create_and_continue.png" />

    Grant the service account the `Storage Object User` role:

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/0hBOxpNzeJRe9LGe/images/cloud/guides/accessing-data/GCS/storage_object_user.png?fit=max&auto=format&n=0hBOxpNzeJRe9LGe&q=85&s=a265f1260d8c616f8a49491fe8b26ed9" size="sm" alt="" width="1090" height="1106" data-path="images/cloud/guides/accessing-data/GCS/storage_object_user.png" />

    This role provides read and write access to GCS objects

    <Tip>
      For read-only access, use `Storage Object Viewer` instead
      For more granular control, you can create a custom role
    </Tip>

    Click `Continue`, then `Done`

    Make note of the service account email address:

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/note_service_account_email.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=4f2c5960051b2e21ec65438be2623299" size="md" alt="" width="1916" height="736" data-path="images/cloud/guides/accessing-data/GCS/note_service_account_email.png" />
  </Step>

  <Step>
    <h3 id="grant-bucket-access-to-service-account">
      Grant bucket access to the service account
    </h3>

    You can grant access at either the project level or individual bucket level.

    <h4 id="option-1">
      Option 1: Grant access to specific buckets (recommended)
    </h4>

    1. Navigate to `Cloud Storage` → `Buckets`
    2. Click on the bucket you want to grant access to
    3. Go to the `Permissions` tab
    4. Under "Permissions" click `Grant access` for the principal created in the previous steps
    5. In the "New principals" field, enter your service account email
    6. Select the appropriate role:

    * Storage Object User for read/write access
    * Storage Object Viewer for read-only access

    7. Click `Save`
    8. Repeat for any additional buckets

    <h4 id="option-2">
      Option 2: Grant project-level access
    </h4>

    1. Navigate to `IAM & Admin` → `IAM`
    2. Click `Grant access`
    3. Enter your service account email in the `New principals` field
    4. Select Storage Object User (or Storage Object Viewer for read-only)
    5. Click SAVE

    <Warning>
      **Security best practice**

      Grant access only to the specific buckets that ClickHouse needs to access, rather than project-wide permissions.
    </Warning>
  </Step>

  <Step>
    <h3 id="generate-hmac-keys-for-service-account">
      Generate HMAC keys for the service account
    </h3>

    Navigate to `Cloud Storage` → `Settings` → `Interoperability`:

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/cloud_storage_settings.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=4d8adfe4a015adff713da17a18579ae4" size="sm" alt="" width="850" height="910" data-path="images/cloud/guides/accessing-data/GCS/cloud_storage_settings.png" />

    If you don't see an "Access keys" section, click `Enable interoperability access`

    Under "Access keys for service accounts", click `Create a key for a service account`:

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/create_key_for_service_account.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=e5823fb9d87d82b8b95c0cfb81794f6c" size="md" alt="" width="1570" height="1308" data-path="images/cloud/guides/accessing-data/GCS/create_key_for_service_account.png" />

    Select the service account you created earlier (e.g [clickhouse-gcs-access@your-project.iam.gserviceaccount.com](mailto:clickhouse-gcs-access@your-project.iam.gserviceaccount.com))

    Click `Create key`:

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/create_a_key.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=b5008d286412d98b8d103b793cd2d80b" size="md" alt="" width="1408" height="464" data-path="images/cloud/guides/accessing-data/GCS/create_a_key.png" />

    The HMAC key will be displayed.
    Save both the Access Key and Secret immediately - you won't be able to view the secret again.

    Example keys are shown below:

    ```vbnet theme={null}
    Access Key: GOOG1EF4YBJVNFQ2YGCP3SLV4Y7CMFHW7HPC6EO7RITLJDDQ75639JK56SQVD
    Secret: nFy6DFRr4sM9OnV6BG4FtWVPR25JfqpmcdZ6w9nV
    ```

    <Danger>
      **Important**

      Store these credentials securely.
      The secret can't be retrieved again after this screen is closed.
      You will need to generate new keys if you lose the secret.
    </Danger>

    <h2 id="use-hmac-keys-with-clickhouse-cloud">
      Use HMAC keys with ClickHouse Cloud
    </h2>

    Now you can use the HMAC credentials to access GCS from ClickHouse Cloud.
    For this, use the GCS table function:

    ```sql theme={null}
    SELECT *
    FROM gcs(
        'https://storage.googleapis.com/clickhouse-docs-example-bucket/epidemiology.csv',
        'GOOG1E...YOUR_ACCESS_KEY',
        'YOUR_SECRET_KEY',
        'CSVWithNames'
    );
    ```

    Use wildcards for multiple files:

    ```sql theme={null}
    SELECT *
    FROM gcs(
    'https://storage.googleapis.com/clickhouse-docs-example-bucket/*.parquet',
    'GOOG1E...YOUR_ACCESS_KEY',
    'YOUR_SECRET_KEY',
    'Parquet'
    );
    ```

    <h2 id="hmac-authentication-clickpipes-for-gcs">
      HMAC authentication in ClickPipes for GCS
    </h2>

    ClickPipes uses HMAC (Hash-based Message Authentication Code) keys to authenticate with Google Cloud Storage.

    When [setting up a GCS ClickPipe](/integrations/clickpipes/object-storage/google-cloud-storage/get-started):

    1. Select `Credentials` under `Authentication method` during ClickPipe setup
    2. Provide HMAC credentials obtained in the previous steps

    <Image img="https://mintcdn.com/private-7c7dfe99-mintlify-fbfa8bee/TCLsdkFwl4CgDiNY/images/cloud/guides/accessing-data/GCS/clickpipes_hmac_key.png?fit=max&auto=format&n=TCLsdkFwl4CgDiNY&q=85&s=47e694dd17df8386f1808d89e3d92a13" size="md" alt="" width="617" height="411" data-path="images/cloud/guides/accessing-data/GCS/clickpipes_hmac_key.png" />

    <Note>
      Service account authentication isn't currently supported - you must use HMAC keys
      The GCS bucket URL must use the format: `https://storage.googleapis.com/<bucket>/<path>` (not `gs://`)
    </Note>

    The HMAC keys must be associated with a service account that has the `roles/storage.objectViewer` role, which includes:

    * `storage.objects.list`: to list objects in the bucket
    * `storage.objects.get`: to fetch/read objects
  </Step>
</Steps>

<h2 id="best-practices">
  Best practices
</h2>

<h3 id="separate-service-accounts">
  Use separate service accounts for different environments
</h3>

Create separate service accounts for development, staging, and production environments. For example:

* `clickhouse-gcs-dev@project.iam.gserviceaccount.com`
* `clickhouse-gcs-staging@project.iam.gserviceaccount.com`
* `clickhouse-gcs-prod@project.iam.gserviceaccount.com`

This allows you to easily revoke access for a specific environment without affecting others.

<h3 id="apply-least-privilege-access">
  Apply least-privilege access
</h3>

Grant only the minimum required permissions:

* Use **Storage Object Viewer** for read-only access
* Grant access to specific buckets rather than project-wide
* Consider using bucket-level conditions to restrict access to specific paths

<h3 id="rotate-hmac-keys">
  Rotate HMAC keys regularly
</h3>

Implement a key rotation schedule:

* Generate new HMAC keys
* Update ClickHouse configurations with new keys
* Verify functionality with new keys
* Delete old HMAC keys

<Tip>
  Google Cloud doesn't enforce HMAC key expiration, so you must implement your own rotation policy.
</Tip>

<h3 id="monitor-access">
  Monitor access with Cloud Audit Logs
</h3>

Enable and monitor Cloud Audit Logs for Cloud Storage:

1. Navigate to IAM & Admin → Audit Logs
2. Find Cloud Storage in the list
3. Enable `Admin Read`, `Data Read`, and `Data Write logs`
4. Use these logs to monitor access patterns and detect anomalies
