Class: ElFinderS3::S3Connector
- Inherits:
-
Object
- Object
- ElFinderS3::S3Connector
- Defined in:
- lib/el_finder_s3/s3_connector.rb
Instance Method Summary collapse
- #exist?(pathname) ⇒ Boolean
-
#initialize(server) ⇒ S3Connector
constructor
A new instance of S3Connector.
- #ls_la(pathname, with_directory) ⇒ Object
- #mkdir(folder_name) ⇒ Object
- #store(filename, content) ⇒ Object
- #touch(filename) ⇒ Object
Constructor Details
#initialize(server) ⇒ S3Connector
Returns a new instance of S3Connector.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/el_finder_s3/s3_connector.rb', line 8 def initialize(server) Aws.config.update( { region: server[:region], credentials: Aws::Credentials.new(server[:access_key_id], server[:secret_access_key]) } ) @bucket_name = server[:bucket_name] @s3_client = Aws::S3::Client.new end |
Instance Method Details
#exist?(pathname) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/el_finder_s3/s3_connector.rb', line 54 def exist?(pathname) query = { bucket: @bucket_name, key: pathname.to_prefix_s } begin @s3_client.head_object(query) true rescue Aws::S3::Errors::NotFound false end end |
#ls_la(pathname, with_directory) ⇒ Object
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 46 47 48 49 50 51 |
# File 'lib/el_finder_s3/s3_connector.rb', line 20 def ls_la(pathname, with_directory) prefix = pathname.to_prefix_s query = { bucket: @bucket_name, delimiter: '/', encoding_type: 'url', max_keys: 100, prefix: prefix } response = @s3_client.list_objects(query) result = [] #Files response.contents.each { |e| if e.key != prefix e.key = e.key.gsub(prefix, '') if with_directory result.push(pathname.fullpath + ::ElFinderS3::S3Pathname.new(self, e)) else result.push(::ElFinderS3::S3Pathname.new(self, e)) end end } #Folders response.common_prefixes.each { |f| if f.prefix != '' && f.prefix != prefix && f.prefix != '/' f.prefix = f.prefix.split('/').last result.push(pathname.fullpath + ::ElFinderS3::S3Pathname.new(self, f)) end } return result end |
#mkdir(folder_name) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/el_finder_s3/s3_connector.rb', line 67 def mkdir(folder_name) begin @s3_client.put_object(bucket: @bucket_name, key: folder_name) true rescue false end end |
#store(filename, content) ⇒ Object
85 86 87 |
# File 'lib/el_finder_s3/s3_connector.rb', line 85 def store(filename, content) @s3_client.put_object(bucket: @bucket_name, key: filename, body: content, acl: 'public-read') end |
#touch(filename) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/el_finder_s3/s3_connector.rb', line 76 def touch(filename) begin @s3_client.put_object(bucket: @bucket_name, key: filename) true rescue false end end |