new Iam(bucket)
Get and set IAM policies for your Cloud Storage bucket.
See Cloud Storage IAM Management See Granting, Changing, and Revoking Access See IAM Roles
Parameters:
| Name | Type | Description |
|---|---|---|
bucket |
Bucket |
The parent instance. |
Methods
getPolicy(optionsopt, callbackopt) → {Promise.<GetPolicyResponse>}
Get the IAM policy.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
options |
GetPolicyOptions |
<optional> |
Request options. |
callback |
GetPolicyCallback |
<optional> |
Callback function. |
Returns:
| Type | Description |
|---|---|
| Promise.<GetPolicyResponse> |
Examples
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
bucket.iam.getPolicy(
{requestedPolicyVersion: 3},
function(err, policy, apiResponse) {
},
);
//-
// If the callback is omitted, we'll return a Promise.
//-
bucket.iam.getPolicy({requestedPolicyVersion: 3})
.then(function(data) {
const policy = data[0];
const apiResponse = data[1];
});
```
include:samples/iam.js
region_tag:storage_view_bucket_iam_members
Example of retrieving a bucket's IAM policy:
setPolicy(policy, optionsopt, callback) → {Promise.<SetPolicyResponse>}
Set the IAM policy.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
policy |
Policy |
The policy. |
|
options |
SetPolicyOptions |
<optional> |
Configuration options. |
callback |
SetPolicyCallback |
Callback function. |
Returns:
| Type | Description |
|---|---|
| Promise.<SetPolicyResponse> |
Throws:
-
If no policy is provided.
- Type
- Error
Examples
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
const myPolicy = {
bindings: [
{
role: 'roles/storage.admin',
members:
['serviceAccount:myotherproject@appspot.gserviceaccount.com']
}
]
};
bucket.iam.setPolicy(myPolicy, function(err, policy, apiResponse) {});
//-
// If the callback is omitted, we'll return a Promise.
//-
bucket.iam.setPolicy(myPolicy).then(function(data) {
const policy = data[0];
const apiResponse = data[1];
});
```
include:samples/iam.js
region_tag:storage_add_bucket_iam_member
Example of adding to a bucket's IAM policy:
include:samples/iam.js
region_tag:storage_remove_bucket_iam_member
Example of removing from a bucket's IAM policy:
testPermissions(permissions, optionsopt, callbackopt) → {Promise.<TestIamPermissionsResponse>}
Test a set of permissions for a resource.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
permissions |
string | Array.<string> |
The permission(s) to test for. |
|
options |
TestIamPermissionsOptions |
<optional> |
Configuration object. |
callback |
TestIamPermissionsCallback |
<optional> |
Callback function. |
Returns:
| Type | Description |
|---|---|
| Promise.<TestIamPermissionsResponse> |
Throws:
-
If permissions are not provided.
- Type
- Error
Example
```
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
//-
// Test a single permission.
//-
const test = 'storage.buckets.delete';
bucket.iam.testPermissions(test, function(err, permissions, apiResponse) {
console.log(permissions);
// {
// "storage.buckets.delete": true
// }
});
//-
// Test several permissions at once.
//-
const tests = [
'storage.buckets.delete',
'storage.buckets.get'
];
bucket.iam.testPermissions(tests, function(err, permissions) {
console.log(permissions);
// {
// "storage.buckets.delete": false,
// "storage.buckets.get": true
// }
});
//-
// If the callback is omitted, we'll return a Promise.
//-
bucket.iam.testPermissions(test).then(function(data) {
const permissions = data[0];
const apiResponse = data[1];
});
```