Class: Gcloud::Storage::Bucket

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

Overview

Represents a Bucket. Belongs to a Project and has many Files.

Defined Under Namespace

Classes: Acl, DefaultAcl, List

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBucket

Create an empty Bucket object.



34
35
36
37
# File 'lib/gcloud/storage/bucket.rb', line 34

def initialize
  @connection = nil
  @gapi = {}
end

Instance Attribute Details

#connectionObject

The Connection object.



26
27
28
# File 'lib/gcloud/storage/bucket.rb', line 26

def connection
  @connection
end

#gapiObject

The Google API Client object.



30
31
32
# File 'lib/gcloud/storage/bucket.rb', line 30

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi, conn) ⇒ Object

New Bucket from a Google API Client object.



197
198
199
200
201
202
# File 'lib/gcloud/storage/bucket.rb', line 197

def self.from_gapi gapi, conn #:nodoc:
  new.tap do |f|
    f.gapi = gapi
    f.connection = conn
  end
end

Instance Method Details

#aclObject

Access Control List



185
186
187
# File 'lib/gcloud/storage/bucket.rb', line 185

def acl
  @acl ||= Bucket::Acl.new self
end

#create_file(file, path = nil, options = {}) ⇒ Object

Create a new Gcloud::Storeage::File object by providing a File object to upload and the path to store it with.

storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
bucket.create_file "path/to/local.file.ext"

Additionally, a destination path can be specified.

storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
bucket.create_file "path/to/local.file.ext",
                   "destination/path/file.ext"

A chunk_size value can be provided in the options to be used in resumable uploads. This value is the number of bytes per chunk and must be divisible by 256KB. If it is not divisible by 265KB then it will be lowered to the nearest acceptible value.

bucket.create_file "path/to/local.file.ext",
                   "destination/path/file.ext",
                   chunk_size: 1024*1024 # 1 MB chunk

See Gcloud::Storage::File



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/gcloud/storage/bucket.rb', line 168

def create_file file, path = nil, options = {}
  ensure_connection!
  # TODO: Raise if file doesn't exist
  # ensure_file_exists!
  fail unless ::File.file? file

  options[:acl] = File::Acl.predefined_rule_for options[:acl]

  if resumable_upload? file
    upload_resumable file, path, options[:chunk_size], options
  else
    upload_multipart file, path, options
  end
end

#created_atObject

Creation time of the bucket.



77
78
79
# File 'lib/gcloud/storage/bucket.rb', line 77

def created_at
  @gapi["timeCreated"]
end

#default_aclObject

Default Access Control List



191
192
193
# File 'lib/gcloud/storage/bucket.rb', line 191

def default_acl
  @default_acl ||= Bucket::DefaultAcl.new self
end

#delete(options = {}) ⇒ Object

Permenently deletes the bucket. The bucket must be empty.

bucket.delete

The API call to delete the bucket may be retried under certain conditions. See Gcloud::Backoff to control this behavior, or specify the wanted behavior in the call:

bucket.delete retries: 5


92
93
94
95
96
97
98
99
100
# File 'lib/gcloud/storage/bucket.rb', line 92

def delete options = {}
  ensure_connection!
  resp = connection.delete_bucket name, options
  if resp.success?
    true
  else
    ApiError.from_response(resp)
  end
end

#files(options = {}) ⇒ Object

Retrieves a list of files matching the criteria.

storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
files = bucket.files
files.each do |file|
  puts file.name
end

See Gcloud::Storage::File



113
114
115
116
117
118
119
120
121
# File 'lib/gcloud/storage/bucket.rb', line 113

def files options = {}
  ensure_connection!
  resp = connection.list_files name, options
  if resp.success?
    File::List.from_resp resp, connection
  else
    fail ApiError.from_response(resp)
  end
end

#find_file(path, options = {}) ⇒ Object

Retrieves a file matching the path.

storage = Gcloud.storage
bucket = storage.find_bucket "my-bucket"
file = bucket.find_file "path/to/my-file.ext"
puts file.name

See Gcloud::Storage::File



132
133
134
135
136
137
138
139
140
# File 'lib/gcloud/storage/bucket.rb', line 132

def find_file path, options = {}
  ensure_connection!
  resp = connection.get_file name, path, options
  if resp.success?
    File.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#idObject

The ID of the bucket.



48
49
50
# File 'lib/gcloud/storage/bucket.rb', line 48

def id
  @gapi["id"]
end

#kindObject

The kind of item this is. For buckets, this is always storage#bucket.



42
43
44
# File 'lib/gcloud/storage/bucket.rb', line 42

def kind
  @gapi["kind"]
end

#locationObject

The location of the bucket. Object data for objects in the bucket resides in physical storage within this region. Defaults to US. See the developer’s guide for the authoritative list.

cloud.google.com/storage/docs/concepts-techniques



71
72
73
# File 'lib/gcloud/storage/bucket.rb', line 71

def location
  @gapi["location"]
end

#nameObject

The name of the bucket.



54
55
56
# File 'lib/gcloud/storage/bucket.rb', line 54

def name
  @gapi["name"]
end

#urlObject

The URI of this bucket.



60
61
62
# File 'lib/gcloud/storage/bucket.rb', line 60

def url
  @gapi["selfLink"]
end