I am trying to use python to do the same functionality as this command:
gsutil iam ch group:[email protected]:objectAdmin gs://bucket_name
I am trying to give an objectAdmin role to a group using python. The above command works well in cloud powershell, but could not do it in python yet
I have tried to do this by replacing the "members": {member} with "groups": {group_name} in this add_bucket_iam_member function here:
def add_bucket_iam_member(bucket_name, role, member):
"""Add a new member to an IAM Policy"""
# bucket_name = "your-bucket-name"
# role = "IAM role, e.g., roles/storage.objectViewer"
# member = "IAM identity, e.g., user: [email protected]"
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
policy = bucket.get_iam_policy(requested_policy_version=3)
#policy.bindings.append({"role": role, "members": {member}})
policy.bindings.append({"role": role, "groups": {group_name}})
bucket.set_iam_policy(policy)
print("Added {} with role {} to {}.".format(member, role, bucket_name))
It doesn't give an error but did not work either, after finishing it, and after getting policy dict again, it removes the group permission that I have sat. (meanwhile, it works fine with members)
I have also tried:
os.system("gsutil iam ch group:[email protected]:objectAdmin gs://bucket_name")
and
subprocess.run("gsutil iam ch group:[email protected]:objectAdmin gs://bucket_name", shell=True) but did not work yet too.
Any help?
OK, your
policy.bindings.append
is incorrect.You want what you originally had:
Full example:
And:
Yields:
And:
Yields: