Class: Gcloud::Storage::Bucket::DefaultAcl

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/storage/bucket/acl.rb

Overview

# Bucket Default Access Control List

Represents a Bucket’s Default Access Control List.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.default_acl.readers.each { |reader| puts reader }

Constant Summary collapse

RULES =
{ "authenticatedRead" => "authenticatedRead",
"auth" => "authenticatedRead",
"auth_read" => "authenticatedRead",
"authenticated" => "authenticatedRead",
"authenticated_read" => "authenticatedRead",
"bucketOwnerFullControl" => "bucketOwnerFullControl",
"owner_full" => "bucketOwnerFullControl",
"bucketOwnerRead" => "bucketOwnerRead",
"owner_read" => "bucketOwnerRead",
"private" => "private",
"projectPrivate" => "projectPrivate",
"project_private" => "projectPrivate",
"publicRead" => "publicRead",
"public" => "publicRead",
"public_read" => "publicRead" }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket) ⇒ DefaultAcl

Must provide a valid Bucket object.



471
472
473
474
475
476
# File 'lib/gcloud/storage/bucket/acl.rb', line 471

def initialize bucket
  @bucket = bucket.name
  @service = bucket.service
  @owners  = nil
  @readers = nil
end

Class Method Details

.predefined_rule_for(rule_name) ⇒ Object



666
667
668
# File 'lib/gcloud/storage/bucket/acl.rb', line 666

def self.predefined_rule_for rule_name
  RULES[rule_name.to_s]
end

Instance Method Details

#add_owner(entity) ⇒ Object

Grants default owner permission to files in the bucket.

Examples:

Grant access to a user by pre-pending ‘“user-”` to an email:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

email = "[email protected]"
bucket.default_acl.add_owner "user-#{email}"

Grant access to a group by pre-pending ‘“group-”` to an email

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

email = "[email protected]"
bucket.default_acl.add_owner "group-#{email}"

Parameters:

  • entity (String)

    The entity holding the permission, in one of the following forms:

    • user-userId

    • user-email

    • group-groupId

    • group-email

    • domain-domain

    • project-team-projectId

    • allUsers

    • allAuthenticatedUsers



580
581
582
583
584
585
# File 'lib/gcloud/storage/bucket/acl.rb', line 580

def add_owner entity
  gapi = @service.insert_default_acl @bucket, entity, "OWNER"
  entity = gapi.entity
  @owners.push entity unless @owners.nil?
  entity
end

#add_reader(entity) ⇒ Object

Grants default reader permission to files in the bucket.

Examples:

Grant access to a user by pre-pending ‘“user-”` to an email:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

email = "[email protected]"
bucket.default_acl.add_reader "user-#{email}"

Grant access to a group by pre-pending ‘“group-”` to an email

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

email = "[email protected]"
bucket.default_acl.add_reader "group-#{email}"

Parameters:

  • entity (String)

    The entity holding the permission, in one of the following forms:

    • user-userId

    • user-email

    • group-groupId

    • group-email

    • domain-domain

    • project-team-projectId

    • allUsers

    • allAuthenticatedUsers



624
625
626
627
628
629
# File 'lib/gcloud/storage/bucket/acl.rb', line 624

def add_reader entity
  gapi = @service.insert_default_acl @bucket, entity, "READER"
  entity = gapi.entity
  @readers.push entity unless @readers.nil?
  entity
end

#auth!Object Also known as: authenticatedRead!, auth_read!, authenticated!, authenticated_read!

Convenience method to apply the default ‘authenticatedRead` predefined ACL rule to files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.acl.auth!


686
687
688
# File 'lib/gcloud/storage/bucket/acl.rb', line 686

def auth!
  update_predefined_default_acl! "authenticatedRead"
end

#delete(entity) ⇒ Object

Permanently deletes the entity from the bucket’s default access control list for files.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

email = "[email protected]"
bucket.default_acl.delete "user-#{email}"

Parameters:

  • entity (String)

    The entity holding the permission, in one of the following forms:

    • user-userId

    • user-email

    • group-groupId

    • group-email

    • domain-domain

    • project-team-projectId

    • allUsers

    • allAuthenticatedUsers



658
659
660
661
662
663
# File 'lib/gcloud/storage/bucket/acl.rb', line 658

def delete entity
  @service.delete_default_acl @bucket, entity
  @owners.delete entity  unless @owners.nil?
  @readers.delete entity unless @readers.nil?
  true
end

#owner_full!Object Also known as: bucketOwnerFullControl!

Convenience method to apply the default ‘bucketOwnerFullControl` predefined ACL rule to files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.acl.owner_full!


708
709
710
# File 'lib/gcloud/storage/bucket/acl.rb', line 708

def owner_full!
  update_predefined_default_acl! "bucketOwnerFullControl"
end

#owner_read!Object Also known as: bucketOwnerRead!

Convenience method to apply the default ‘bucketOwnerRead` predefined ACL rule to files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.acl.owner_read!


727
728
729
# File 'lib/gcloud/storage/bucket/acl.rb', line 727

def owner_read!
  update_predefined_default_acl! "bucketOwnerRead"
end

#ownersArray<String>

Lists the default owners for files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.default_acl.owners.each { |owner| puts owner }

Returns:

  • (Array<String>)


518
519
520
521
# File 'lib/gcloud/storage/bucket/acl.rb', line 518

def owners
  reload! if @owners.nil?
  @owners
end

#private!Object

Convenience method to apply the default ‘private` predefined ACL rule to files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.acl.private!


746
747
748
# File 'lib/gcloud/storage/bucket/acl.rb', line 746

def private!
  update_predefined_default_acl! "private"
end

#project_private!Object Also known as: projectPrivate!

Convenience method to apply the default ‘projectPrivate` predefined ACL rule to files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.acl.project_private!


764
765
766
# File 'lib/gcloud/storage/bucket/acl.rb', line 764

def project_private!
  update_predefined_default_acl! "projectPrivate"
end

#public!Object Also known as: publicRead!, public_read!

Convenience method to apply the default ‘publicRead` predefined ACL rule to files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.acl.public!


783
784
785
# File 'lib/gcloud/storage/bucket/acl.rb', line 783

def public!
  update_predefined_default_acl! "publicRead"
end

#readersArray<String>

Lists the default readers for files in the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.default_acl.readers.each { |reader| puts reader }

Returns:

  • (Array<String>)


538
539
540
541
# File 'lib/gcloud/storage/bucket/acl.rb', line 538

def readers
  reload! if @readers.nil?
  @readers
end

#reload!Object Also known as: refresh!

Reloads all Default Access Control List data for the bucket.

Examples:

require "gcloud"

gcloud = Gcloud.new
storage = gcloud.storage

bucket = storage.bucket "my-bucket"

bucket.default_acl.reload!


491
492
493
494
495
496
497
498
499
500
# File 'lib/gcloud/storage/bucket/acl.rb', line 491

def reload!
  gapi = @service.list_default_acls @bucket
  acls = Array(gapi.items).map do |acl|
    return acl if acl.is_a? Google::Apis::StorageV1::ObjectAccessControl
    fail "Unknown ACL format: #{acl.class}" unless acl.is_a? Hash
    Google::Apis::StorageV1::ObjectAccessControl.from_json acl.to_json
  end
  @owners  = entities_from_acls acls, "OWNER"
  @readers = entities_from_acls acls, "READER"
end