This page describes how to use the public access prevention bucket setting and the public access prevention organization policy constraint. Public access prevention lets you restrict public access to your buckets and objects.
Before you begin
Before using public access prevention in Cloud Storage, make sure you have the required IAM role and review the considerations for enforcing public access prevention.
Get required roles
To manage the public access prevention organization policy at the project,
folder, or organization level, ask your administrator to grant you the
Organization Policy Administrator ( roles/orgpolicy.policyAdmin
)
role on the organization. This predefined role contains the
permissions required to manage public access prevention at the project, folder,
or organization level. For information about the permissions that are included
in this role, refer to details about the Organization Administrator role
.
To manage the public access prevention setting on a bucket, ask your
administrator to grant you the Storage Admin ( roles/storage.admin
) role
on the bucket. This role contains the permissions required to manage public
access prevention on a bucket. To see the exact permissions that are required,
expand the Required permissionssection:
Required permissions
-
storage.buckets.update
-
storage.buckets.setIamPolicy
For information about the other permissions that are included in the Storage Admin role, refer to details about the Storage Admin role .
Review considerations
Before you begin, it's recommended that you ensure no workflows break as a result of blocking public access. See Considerations when enforcing on existing resources for more details.
Use the bucket setting
This section shows how to enforce and remove public access prevention for individual buckets, as well as how to check the status of individual buckets.
Set public access prevention
To change the public access prevention setting for an individual bucket:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets
page.
-
In the list of buckets, click the name of the bucket for which you want to enforce or remove public access prevention.
-
In the Bucket detailspage, click the Permissionstab.
-
In the Public accesscard, click Prevent public accessto enforce public access prevention, or click Allow public accessto remove public access prevention.
-
Click Confirm.
To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, see Troubleshooting .
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 either--public-access-prevention
to enable public access prevention or--no-public-access-prevention
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 .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a bucket:
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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a 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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a bucket:
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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a bucket:
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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a bucket:
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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a bucket:
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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a 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 client libraries .
The following sample enforces public access prevention on a bucket:
The following sample sets public access prevention to inherited
for a bucket: