2010-10-04 14:02:08 -07:00
|
|
|
require 'fog/core/model'
|
2011-08-24 13:50:42 -05:00
|
|
|
require 'fog/aws/models/storage/files'
|
2012-01-26 10:06:40 -05:00
|
|
|
require 'fog/aws/models/storage/versions'
|
2010-03-13 13:37:24 -08:00
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
module Fog
|
2011-06-15 14:26:43 -07:00
|
|
|
module Storage
|
|
|
|
class AWS
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
class Directory < Fog::Model
|
2012-05-02 11:19:46 -04:00
|
|
|
VALID_ACLS = ['private', 'public-read', 'public-read-write', 'authenticated-read']
|
|
|
|
|
|
|
|
attr_reader :acl
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2010-09-07 11:30:02 -07:00
|
|
|
identity :key, :aliases => ['Name', 'name']
|
2009-10-23 10:27:23 -07:00
|
|
|
|
2013-04-12 17:37:01 +01:00
|
|
|
attribute :creation_date, :aliases => 'CreationDate', :type => 'time'
|
|
|
|
attribute :location, :aliases => 'LocationConstraint', :type => 'string'
|
2009-08-04 10:51:54 -07:00
|
|
|
|
2010-10-29 16:38:17 -07:00
|
|
|
def acl=(new_acl)
|
2012-05-03 17:42:52 -05:00
|
|
|
unless VALID_ACLS.include?(new_acl)
|
2012-05-02 11:19:46 -04:00
|
|
|
raise ArgumentError.new("acl must be one of [#{VALID_ACLS.join(', ')}]")
|
|
|
|
else
|
|
|
|
@acl = new_acl
|
2010-10-29 16:38:17 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-30 14:14:11 -07:00
|
|
|
def destroy
|
2010-05-02 19:59:15 -07:00
|
|
|
requires :key
|
2012-12-22 23:30:12 +00:00
|
|
|
service.delete_bucket(key)
|
2009-08-27 20:47:01 -07:00
|
|
|
true
|
2009-11-08 12:16:52 -08:00
|
|
|
rescue Excon::Errors::NotFound
|
2009-08-31 22:40:07 -07:00
|
|
|
false
|
2009-08-03 22:50:52 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def location
|
2013-04-12 17:37:01 +01:00
|
|
|
@location ||= (bucket_location || self.service.region)
|
2009-08-03 22:50:52 -07:00
|
|
|
end
|
|
|
|
|
2013-04-12 17:37:01 +01:00
|
|
|
# NOTE: you can't change the region once the bucket is created
|
2009-10-23 09:42:51 -07:00
|
|
|
def location=(new_location)
|
2013-04-12 17:37:01 +01:00
|
|
|
@location = new_location
|
2009-10-23 09:42:51 -07:00
|
|
|
end
|
|
|
|
|
2010-01-14 20:44:39 -08:00
|
|
|
def files
|
2012-12-22 23:30:12 +00:00
|
|
|
@files ||= Fog::Storage::AWS::Files.new(:directory => self, :service => service)
|
2009-08-12 22:48:17 -07:00
|
|
|
end
|
|
|
|
|
2009-08-03 22:50:52 -07:00
|
|
|
def payer
|
2010-05-02 19:59:15 -07:00
|
|
|
requires :key
|
2012-12-22 23:30:12 +00:00
|
|
|
data = service.get_request_payment(key)
|
2009-09-02 20:17:53 -07:00
|
|
|
data.body['Payer']
|
2009-08-03 22:50:52 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def payer=(new_payer)
|
2010-05-02 19:59:15 -07:00
|
|
|
requires :key
|
2012-12-22 23:30:12 +00:00
|
|
|
service.put_request_payment(key, new_payer)
|
2009-08-03 22:54:34 -07:00
|
|
|
@payer = new_payer
|
2009-08-03 22:50:52 -07:00
|
|
|
end
|
|
|
|
|
2011-12-29 14:47:50 -05:00
|
|
|
def versioning?
|
|
|
|
requires :key
|
2012-12-22 23:30:12 +00:00
|
|
|
data = service.get_bucket_versioning(key)
|
2011-12-29 14:47:50 -05:00
|
|
|
data.body['VersioningConfiguration']['Status'] == 'Enabled'
|
|
|
|
end
|
|
|
|
|
|
|
|
def versioning=(new_versioning)
|
|
|
|
requires :key
|
2012-12-22 23:30:12 +00:00
|
|
|
service.put_bucket_versioning(key, new_versioning ? 'Enabled' : 'Suspended')
|
2011-12-29 14:47:50 -05:00
|
|
|
end
|
|
|
|
|
2012-01-26 10:06:40 -05:00
|
|
|
def versions
|
2012-12-22 23:30:12 +00:00
|
|
|
@versions ||= Fog::Storage::AWS::Versions.new(:directory => self, :service => service)
|
2012-01-26 10:06:40 -05:00
|
|
|
end
|
|
|
|
|
2010-11-05 11:37:12 -07:00
|
|
|
def public=(new_public)
|
2012-05-02 11:19:46 -04:00
|
|
|
self.acl = new_public ? 'public-read' : 'private'
|
2010-11-05 11:37:12 -07:00
|
|
|
new_public
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_url
|
|
|
|
requires :key
|
2012-12-22 23:30:12 +00:00
|
|
|
if service.get_bucket_acl(key).body['AccessControlList'].detect {|grant| grant['Grantee']['URI'] == 'http://acs.amazonaws.com/groups/global/AllUsers' && grant['Permission'] == 'READ'}
|
2012-12-05 21:12:25 +01:00
|
|
|
if key.to_s =~ Fog::AWS::COMPLIANT_BUCKET_NAMES
|
2010-11-05 11:37:12 -07:00
|
|
|
"https://#{key}.s3.amazonaws.com"
|
|
|
|
else
|
|
|
|
"https://s3.amazonaws.com/#{key}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-03 22:50:52 -07:00
|
|
|
def save
|
2010-05-02 19:59:15 -07:00
|
|
|
requires :key
|
2012-05-02 11:19:46 -04:00
|
|
|
|
2009-08-03 22:50:52 -07:00
|
|
|
options = {}
|
2012-05-02 11:19:46 -04:00
|
|
|
|
|
|
|
options['x-amz-acl'] = acl if acl
|
|
|
|
|
2013-04-12 17:37:01 +01:00
|
|
|
# http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUT.html
|
|
|
|
# Ignore the default region us-east-1
|
|
|
|
if !persisted? && location != DEFAULT_REGION
|
2012-05-28 15:36:54 -05:00
|
|
|
options['LocationConstraint'] = location
|
|
|
|
end
|
2012-05-02 11:19:46 -04:00
|
|
|
|
2012-12-22 23:30:12 +00:00
|
|
|
service.put_bucket(key, options)
|
2013-04-12 19:18:42 +01:00
|
|
|
attributes[:is_persisted] = true
|
2012-05-02 11:19:46 -04:00
|
|
|
|
2009-08-27 20:47:01 -07:00
|
|
|
true
|
2009-08-03 22:50:52 -07:00
|
|
|
end
|
2009-08-02 16:37:54 -07:00
|
|
|
|
2013-04-12 19:18:42 +01:00
|
|
|
def persisted?
|
|
|
|
# is_persisted is true in case of directories.get or after #save
|
|
|
|
# creation_date is set in case of directories.all
|
|
|
|
attributes[:is_persisted] || !!attributes[:creation_date]
|
|
|
|
end
|
|
|
|
|
2012-05-02 11:19:46 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def bucket_location
|
2013-04-12 17:37:01 +01:00
|
|
|
requires :key
|
|
|
|
return nil unless persisted?
|
2012-12-22 23:30:12 +00:00
|
|
|
data = service.get_bucket_location(key)
|
2012-05-02 11:19:46 -04:00
|
|
|
data.body['LocationConstraint']
|
|
|
|
end
|
|
|
|
|
2009-08-02 16:37:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|