Class: Cliaws::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/cliaws/s3.rb

Defined Under Namespace

Classes: UnknownBucket

Instance Method Summary collapse

Constructor Details

#initialize(access_key_id, secret_access_key) ⇒ S3

Returns a new instance of S3.



8
9
10
# File 'lib/cliaws/s3.rb', line 8

def initialize(access_key_id, secret_access_key)
  @access_key_id, @secret_access_key = access_key_id, secret_access_key
end

Instance Method Details

#bucket(name, create = false) ⇒ Object



51
52
53
# File 'lib/cliaws/s3.rb', line 51

def bucket(name, create=false)
  s3.bucket(name, create)
end

#bucketsObject



55
56
57
# File 'lib/cliaws/s3.rb', line 55

def buckets
  s3.buckets
end

#get(s3_object, dest = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/cliaws/s3.rb', line 25

def get(s3_object, dest=nil)
  bucket, keyname = bucket_and_key_name(s3_object)
  if dest.nil? then
    bucket.get(keyname)
  else
    dest.write(bucket.get(keyname))
  end
end

#grant(name, permissions) ⇒ Object

permissions is a Hash of ID|EMAIL|URL to permissions. Cliaws.s3.grant(“my_awesome_bucket/some/key”, “[email protected]” => %w(read write))



77
78
79
80
81
82
# File 'lib/cliaws/s3.rb', line 77

def grant(name, permissions)
  bucket, thing = bucket_and_thing(name)
  permissions.each do |subject, perms|
    RightAws::S3::Grantee.new(thing, subject, perms.map {|perm| perm.upcase}, :apply)
  end
end

#grants(name) ⇒ Object



84
85
86
87
88
# File 'lib/cliaws/s3.rb', line 84

def grants(name)
  bucket, thing = bucket_and_thing(name)
  _, grantees = RightAws::S3::Grantee.owner_and_grantees(thing)
  grantees
end

#head(s3_object) ⇒ Object



34
35
36
37
38
39
# File 'lib/cliaws/s3.rb', line 34

def head(s3_object)
  bucket, keyname = bucket_and_key_name(s3_object)
  key = bucket.key(keyname, true)
  headers = key.headers
  headers.merge(key.meta_headers)
end

#list(glob) ⇒ Object



18
19
20
21
22
23
# File 'lib/cliaws/s3.rb', line 18

def list(glob)
  bucket, path = bucket_and_key_name(glob)
  options = Hash.new
  options["prefix"] = path unless path.nil? || path.empty?
  bucket.keys(options).map {|b| b.full_name}
end

#put(source, s3_object, create = true) ⇒ Object



41
42
43
44
# File 'lib/cliaws/s3.rb', line 41

def put(source, s3_object, create=true)
  bucket, keyname = bucket_and_key_name(s3_object, create)
  bucket.put(keyname, source)
end

#revoke(name, permissions) ⇒ Object

permissions is a Hash of ID|EMAIL|URL to permissions. Cliaws.s3.revoke(“my_awesome_bucket/some/key”, “[email protected]” => %w(read write)) Cliaws.s3.revoke(“my_awesome_bucket/some/key”, “[email protected]” => %w()) # Drops all grants for the user



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cliaws/s3.rb', line 62

def revoke(name, permissions)
  bucket, thing = bucket_and_thing(name)
  permissions.each do |subject, perms|
    grantee = RightAws::S3::Grantee.new(thing, subject, perms.map {|perm| perm.upcase}, :refresh)
    if perms.empty? then
      grantee.drop
    else
      grantee.revoke(*permissions)
      grantee.apply
    end
  end
end

#rm(s3_object) ⇒ Object



46
47
48
49
# File 'lib/cliaws/s3.rb', line 46

def rm(s3_object)
  bucket, keyname = bucket_and_key_name(s3_object)
  bucket.key(keyname).delete
end

#url(full_name) ⇒ Object



12
13
14
15
16
# File 'lib/cliaws/s3.rb', line 12

def url(full_name)
  bucket_name, path = full_name.split("/", 2)
  bucket = s3g.bucket(bucket_name, false)
  bucket.get(path)
end