Module: S3Helper

Includes:
REXML
Defined in:
lib/s3_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.set_acl_private(acl_doc) ⇒ Object

sets an ACL to private



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/s3_helper.rb', line 48

def self.set_acl_private(acl_doc)
      # create Document
      doc = Document.new(acl_doc)

      # get AccessControlList node
      acl_node = XPath.first(doc, '//AccessControlList')

      # delete existing 'AllUsers' Grantee
      acl_node.delete_element "//Grant[descendant::URI[text()='http://acs.amazonaws.com/groups/global/AllUsers']]"

      return doc.to_s
end

.set_acl_public_read(acl_doc) ⇒ Object

sets an ACL to public-read



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/s3_helper.rb', line 16

def self.set_acl_public_read(acl_doc)
      # create Document
      doc = Document.new(acl_doc)

      # get AccessControlList node
      acl_node = XPath.first(doc, '//AccessControlList')

      # delete existing 'AllUsers' Grantee
      acl_node.delete_element "//Grant[descendant::URI[text()='http://acs.amazonaws.com/groups/global/AllUsers']]"

      # create a new READ grant node
      grant_node = Element.new('Grant')
      grantee = Element.new('Grantee')
      grantee.attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'
      grantee.attributes['xsi:type'] = 'Group'

      uri = Element.new('URI')
      uri << Text.new('http://acs.amazonaws.com/groups/global/AllUsers')
      grantee.add_element(uri)
      grant_node.add_element(grantee)
      
      perm = Element.new('Permission')
      perm << Text.new('READ')
      grant_node.add_element(perm)

      # attach the new READ grant node
      acl_node.add_element(grant_node)

      return doc.to_s
end

.set_acl_public_read_write(acl_doc) ⇒ Object

sets an ACL to public-read-write



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/s3_helper.rb', line 62

def self.set_acl_public_read_write(acl_doc)
      # create Document
      doc = Document.new(acl_doc)

      # get AccessControlList node
      acl_node = XPath.first(doc, '//AccessControlList')

      # delete existing 'AllUsers' Grantee
      acl_node.delete_element "//Grant[descendant::URI[text()='http://acs.amazonaws.com/groups/global/AllUsers']]"

      # create a new READ grant node
      grant_node = Element.new('Grant')
      grantee = Element.new('Grantee')
      grantee.attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'
      grantee.attributes['xsi:type'] = 'Group'

      uri = Element.new('URI')
      uri << Text.new('http://acs.amazonaws.com/groups/global/AllUsers')
      grantee.add_element(uri)
      grant_node.add_element(grantee)
      
      perm = Element.new('Permission')
      perm << Text.new('READ')
      grant_node.add_element(perm)

      # attach the new grant node
      acl_node.add_element(grant_node)

      # create a new WRITE grant node
      grant_node = Element.new('Grant')
      grantee = Element.new('Grantee')
      grantee.attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance'
      grantee.attributes['xsi:type'] = 'Group'

      uri = Element.new('URI')
      uri << Text.new('http://acs.amazonaws.com/groups/global/AllUsers')
      grantee.add_element(uri)
      grant_node.add_element(grantee)
      
      perm = Element.new('Permission')
      perm << Text.new('WRITE')
      grant_node.add_element(perm)

      # attach the new grant tree
      acl_node.add_element(grant_node)

      return doc.to_s
end

Instance Method Details

returns public URL for key



9
10
11
12
13
# File 'lib/s3_helper.rb', line 9

def public_link(bucket_name, key='')
  url = File.join('http://', S3::DEFAULT_HOST, bucket_name, key)
  str = link_to(key, url)
  str
end