Follow these steps to create a private Google Cloud Storage (GCS) bucket and configure access through a load balancer.
Create a Private GCS Bucket
- Create a GCS Bucket: Make sure it’s private by selecting Enforce public access prevention on this bucket and choose Uniform for Access control.
- Upload Your Content: Add your files to the bucket, for example
image.jpg
.
Create a Service Account
- Navigate to IAM & Admin -> Service Accounts.
- Create a Service Account: Add a name for the service account (referred to as PRIVATE_GCS_SA) and leave other settings as default.
Generate HMAC for the Service Account
- Go to Cloud Storage -> Settings.
- Switch to the Interoperability Tab.
- Create an Access Key: Under “Access keys for service accounts,” click
+ Create a key for a Service Account
. - Select the Service Account: Choose the previously created service account (PRIVATE_GCS_SA) and click
Create Key
. - Save the Access Key and Secret: Note the “Access key” (HMAC_ACCESS_KEY) and “Secret” (HMAC_SECRET) for later use.
Add Service Account Permissions to the Bucket
- Go to Cloud Storage -> [BUCKET_NAME].
- Switch to the PERMISSIONS Tab.
- Grant Access: Click
+ GRANT ACCESS
and type the service account email (PRIVATE_GCS_SA). Assign the “Storage Legacy Object Reader” role and clickSave
.
Create Network Endpoint Groups
- Navigate to Compute Engine -> Network Endpoint Groups.
- Create a Network Endpoint Group: Name it like ”private-gcs-neg”
- Configure the Endpoint Group:
- Type: Select “Internet NEG (Global, Regional).”
- Default Port: 443.
- Add Through: Choose “Fully qualified domain name and port.”
- Domain Name: Input “[BUCKET_NAME].storage.googleapis.com.”
- Create the Endpoint Group.
Create an HTTP(S) Load Balancer
- Go to Network Services -> Load Balancing.
- Create a Load Balancer: Click
+ Create Load Balancer
. - Configure the Load Balancer:
- Type: Select Application Load Balancer (HTTP/HTTPS).
- Frontend Configuration:
- Protocol: HTTPS (includes HTTP/2 and HTTP/3).
- IP Address: Static IP address (or create a new one).
- Certificate: Select or create a new one.
- HTTP to HTTPS Redirect: Enable it.
- Backend Configuration:
- Create a Backend Service: Name it.
- Backend Type: Select Internet network endpoint group.
- Protocol: HTTPS.
- Backend Selection: Choose the previously created Network Endpoint Group.
- Cloud CDN: Ensure Cloud CDN is enabled.
- Security: Select None for Cloud Armor backend security policy.
- Custom Request Headers: Add a header with ”host” as the name and ”[BUCKET_NAME].storage.googleapis.com” as the value.
- Create the Load Balancer: Set the name and finalize the creation.
Configure Private Origin Authentication
- Open Cloud Shell or gcloud CLI.
- Export Backend Configuration: Use the command:
- Edit the YAML File: Add the following to the
securitySettings
section: Replace[HMAC_ACCESS_KEY]
,[HMAC_SECRET]
, and[REGION]
with the appropriate values. - Import the Configuration: Use the command:
Testing
Run the following cURL command
IF success, it will return 200 similar like this