Update IAM policy

Update a table's IAM policy.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Go API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set 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. 
 func 
  
 updateIAMPolicy 
 ( 
 projectID 
 , 
  
 datasetID 
 , 
  
 tableID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // datasetID := "mydataset" 
  
 // tableID := "mytable" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 bigquery 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "bigquery.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 table 
  
 := 
  
 client 
 . 
 Dataset 
 ( 
 datasetID 
 ). 
 Table 
 ( 
 tableID 
 ) 
  
 policy 
 , 
  
 err 
  
 := 
  
 table 
 . 
  IAM 
 
 (). 
 Policy 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to get table policy: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 newRole 
  
 := 
  
 iam 
 . 
  RoleName 
 
 ( 
 "roles/bigquery.dataViewer" 
 ) 
  
 newMember 
  
 := 
  
 "allAuthenticatedUsers" 
  
 policy 
 . 
  Add 
 
 ( 
 newMember 
 , 
  
 newRole 
 ) 
  
 if 
  
 err 
  
 := 
  
 table 
 . 
  IAM 
 
 (). 
 SetPolicy 
 ( 
 ctx 
 , 
  
 policy 
 ); 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "failed to set new table policy: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Java API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  import 
  
 com.google.cloud. Identity 
 
 ; 
 import 
  
 com.google.cloud. Policy 
 
 ; 
 import 
  
 com.google.cloud. Role 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQuery 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQueryException 
 
 ; 
 import 
  
 com.google.cloud.bigquery. BigQueryOptions 
 
 ; 
 import 
  
 com.google.cloud.bigquery. TableId 
 
 ; 
 import 
  
 java.util.HashMap 
 ; 
 import 
  
 java.util.Map 
 ; 
 import 
  
 java.util.Set 
 ; 
 // Sample to update iam policy in table 
 public 
  
 class 
 UpdateIamPolicy 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
  String 
 
 [] 
  
 args 
 ) 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
  String 
 
  
 datasetName 
  
 = 
  
 "MY_DATASET_NAME" 
 ; 
  
  String 
 
  
 tableName 
  
 = 
  
 "MY_TABLE_NAME" 
 ; 
  
 updateIamPolicy 
 ( 
 datasetName 
 , 
  
 tableName 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 updateIamPolicy 
 ( 
  String 
 
  
 datasetName 
 , 
  
  String 
 
  
 tableName 
 ) 
  
 { 
  
 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. 
  
  BigQuery 
 
  
 bigquery 
  
 = 
  
  BigQueryOptions 
 
 . 
 getDefaultInstance 
 (). 
 getService 
 (); 
  
  TableId 
 
  
 tableId 
  
 = 
  
  TableId 
 
 . 
 of 
 ( 
 datasetName 
 , 
  
 tableName 
 ); 
  
  Policy 
 
  
 policy 
  
 = 
  
 bigquery 
 . 
  getIamPolicy 
 
 ( 
 tableId 
 ); 
  
 Map<Role 
 , 
  
 Set<Identity> 
>  
 binding 
  
 = 
  
 new 
  
 HashMap 
<> ( 
 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 
  
 ( 
  BigQueryException 
 
  
 e 
 ) 
  
 { 
  
 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 the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: