Update IAM policy Stay organized with collections Save and categorize content based on your preferences.
Update a table's IAM policy.
Code sample
Go
Before trying this sample, follow theGo setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryGo API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
import("context""fmt""cloud.google.com/go/bigquery""cloud.google.com/go/iam")// updateIAMPolicy demonstrates updating the ACL on a BigQuery table by adding a new entry to// an existing policy.funcupdateIAMPolicy(projectID,datasetID,tableIDstring)error{// projectID := "my-project-id"// datasetID := "mydataset"// tableID := "mytable"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()table:=client.Dataset(datasetID).Table(tableID)policy,err:=table.IAM().Policy(ctx)iferr!=nil{returnfmt.Errorf("failed to get table policy: %w",err)}newRole:=iam.RoleName("roles/bigquery.dataViewer")newMember:="allAuthenticatedUsers"policy.Add(newMember,newRole)iferr:=table.IAM().SetPolicy(ctx,policy);err!=nil{returnfmt.Errorf("failed to set new table policy: %w",err)}returnnil}Java
Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.
importcom.google.cloud.Identity;importcom.google.cloud.Policy;importcom.google.cloud.Role;importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.TableId;importjava.util.HashMap;importjava.util.Map;importjava.util.Set;// Sample to update iam policy in tablepublicclassUpdateIamPolicy{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringdatasetName="MY_DATASET_NAME";StringtableName="MY_TABLE_NAME";updateIamPolicy(datasetName,tableName);}publicstaticvoidupdateIamPolicy(StringdatasetName,StringtableName){try{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();TableIdtableId=TableId.of(datasetName,tableName);Policypolicy=bigquery.getIamPolicy(tableId);Map<Role,Set<Identity>>binding=newHashMap<>(policy.getBindings());binding.remove(Role.of("roles/bigquery.dataViewer"));policy.toBuilder().setBindings(binding).build();bigquery.setIamPolicy(tableId,policy);System.out.println("Iam policy updated successfully");}catch(BigQueryExceptione){System.out.println("Iam policy was not updated. \n"+e.toString());}}}What's next
To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.