Class: Sndacs::Bucket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Proxies, Parser
Defined in:
lib/sndacs/bucket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse_all_buckets_result, #parse_all_objects_result, #parse_copy_object_result, #parse_error, #parse_is_truncated, #parse_location_constraint, #rexml_document

Instance Attribute Details

#location(reload = false) ⇒ Object

Returns location of the bucket, e.g. “huabei-1”



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

def location
  @location
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/sndacs/bucket.rb', line 8

def name
  @name
end

#serviceObject

Returns the value of attribute service.



8
9
10
# File 'lib/sndacs/bucket.rb', line 8

def service
  @service
end

Instance Method Details

#==(other) ⇒ Object

Compares the bucket with other bucket. Returns true if the names of the buckets are the same, and both have the same locations and services (see Service equality)



16
17
18
# File 'lib/sndacs/bucket.rb', line 16

def ==(other)
  self.name == other.name and self.location == other.location and self.service == other.service
end

#delete_bucket_policyObject

Delete the policy of the given bucket exception if the bucket do not have a bucket policy



77
78
79
80
# File 'lib/sndacs/bucket.rb', line 77

def delete_bucket_policy()
    bucket_request(:delete,:subresource=>"policy")
    true
end

#destroy(force = false) ⇒ Object

Destroys given bucket. Raises an Sndacs::Error::BucketNotEmpty exception if the bucket is not empty. You can destroy non-empty bucket passing true (to force destroy)



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sndacs/bucket.rb', line 94

def destroy(force = false)
  delete_bucket

  true
rescue Error::BucketNotEmpty
  if force
    objects.destroy_all

    retry
  else
    raise
  end
end

#exists?Boolean

Similar to retrieve, but catches Sndacs::Error::NoSuchBucket exceptions and returns false instead.

Returns:

  • (Boolean)


110
111
112
113
114
115
116
# File 'lib/sndacs/bucket.rb', line 110

def exists?
  retrieve

  true
rescue Error::NoSuchBucket
  false
end

#get_bucket_policyObject

Get the bucket policy of the given bucket exception if the bucket do not have a bucket policy



84
85
86
87
88
# File 'lib/sndacs/bucket.rb', line 84

def get_bucket_policy()
    response = bucket_request(:get,:subresource=>"policy")
    #puts response.body
    response.body
end

#host(public_accessible = false) ⇒ Object

Returns host name of the bucket according (see #vhost? method)



127
128
129
# File 'lib/sndacs/bucket.rb', line 127

def host(public_accessible = false)
  vhost? ? "#@name.#{region_host(public_accessible)}" : region_host(public_accessible)
end

#inspectObject

:nodoc:



149
150
151
# File 'lib/sndacs/bucket.rb', line 149

def inspect #:nodoc:
  "#<#{self.class}:#{name}>"
end

#object(key) ⇒ Object

Returns the object with the given key. Does not check whether the object exists. But also does not issue any HTTP requests, so it’s much faster than objects.find



145
146
147
# File 'lib/sndacs/bucket.rb', line 145

def object(key)
  Object.send(:new, self, :key => key)
end

#objectsObject

Returns the objects in the bucket and caches the result



138
139
140
# File 'lib/sndacs/bucket.rb', line 138

def objects
  Proxy.new(lambda { list_bucket }, :owner => self, :extend => ObjectsExtension)
end

#path_prefixObject

Returns path prefix for non VHOST bucket. Path prefix is used instead of VHOST name, e.g. “bucket_name/”



133
134
135
# File 'lib/sndacs/bucket.rb', line 133

def path_prefix
  vhost? ? "" : "#@name/"
end

#put_bucket_policy(bucket_policy) ⇒ Object

Set bucket policy for the given bucket



66
67
68
69
70
71
72
73
# File 'lib/sndacs/bucket.rb', line 66

def put_bucket_policy(bucket_policy)
    if bucket_policy && bucket_policy.is_a?(String) && bucket_policy.strip != ''
         bucket_request(:put,:body => bucket_policy,:subresource=>"policy")
         true
    else  
        false
    end
end

#retrieveObject

Retrieves the bucket information from the server. Raises an Sndacs::Error exception if the bucket doesn’t exist or you don’t have access to it, etc.



23
24
25
26
27
# File 'lib/sndacs/bucket.rb', line 23

def retrieve
  bucket_headers

  self
end

#save(options = { :location => 'huabei-1'}) ⇒ Object

Saves the newly built bucket. Raises Sndacs::Error::BucketAlreadyExists exception if the bucket already exists.

Options

  • :location - location of the bucket (huabei-1 or huadong-1)

  • Any other options are passed through to Connection#request



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sndacs/bucket.rb', line 43

def save(options = { :location => 'huabei-1'})
  if options
    if options.is_a?(String) && options.strip != ''
      options = {:location => options.strip}
    end

    if options.is_a?(Hash) && !options.has_key?(:location)
      options.merge!(:location => location)
    end
    
    if options.is_a?(Hash) && options.has_key?(:location)
       @location = options[:location]
    end
  else
    options = {:location => location}
  end

  create_bucket_configuration(options)

  true
end

#vhost?Boolean

Returns true if the name of the bucket can be used like VHOST name. If the bucket contains characters like underscore it can’t be used as VHOST (e.g. bucket_name.storage.grandcloud.cn)

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/sndacs/bucket.rb', line 121

def vhost?
  #"#@name.#{region_host}" =~ /\A#{URI::REGEXP::PATTERN::HOSTNAME}\Z/
  false
end