Class: ElFinderS3::S3Connector
- Inherits:
-
Object
- Object
- ElFinderS3::S3Connector
- Defined in:
- lib/el_finder_s3/s3_connector.rb
Instance Method Summary collapse
- #exist?(pathname) ⇒ Boolean
- #get(filename) ⇒ Object
-
#initialize(server) ⇒ S3Connector
constructor
A new instance of S3Connector.
- #ls_la(pathname) ⇒ Object
- #mkdir(folder_name) ⇒ Object
- #size(pathname) ⇒ 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
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/el_finder_s3/s3_connector.rb', line 53 def exist?(pathname) query = { bucket: @bucket_name, key: pathname.file? ? pathname.to_file_prefix_s : pathname.to_prefix_s } begin @s3_client.head_object(query) true rescue Aws::S3::Errors::NotFound false end end |
#get(filename) ⇒ Object
91 92 93 94 95 |
# File 'lib/el_finder_s3/s3_connector.rb', line 91 def get(filename) response = @s3_client.get_object(bucket: @bucket_name, key: filename) return nil unless !response.nil? response.body end |
#ls_la(pathname) ⇒ Object
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 46 47 48 49 50 |
# File 'lib/el_finder_s3/s3_connector.rb', line 19 def ls_la(pathname) 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 = { :folders => [], :files => [] } #Files response.contents.each { |e| if e.key != prefix e.key = e.key.gsub(prefix, '') result[:files].push(e[:key]) end } #Folders response.common_prefixes.each { |f| if f.prefix != '' && f.prefix != prefix && f.prefix != '/' f.prefix = f.prefix.split('/').last result[:folders].push(f[:prefix]) end } return result end |
#mkdir(folder_name) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/el_finder_s3/s3_connector.rb', line 66 def mkdir(folder_name) begin @s3_client.put_object(bucket: @bucket_name, key: folder_name) true rescue false end end |
#size(pathname) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/el_finder_s3/s3_connector.rb', line 97 def size(pathname) query = { bucket: @bucket_name, key: pathname.file? ? pathname.to_file_prefix_s : pathname.to_prefix_s } begin head = @s3_client.head_object(query) head[:content_length] rescue Aws::S3::Errors::NotFound 0 end end |
#store(filename, content) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/el_finder_s3/s3_connector.rb', line 84 def store(filename, content) if content.is_a?(MiniMagick::Image) @s3_client.put_object(bucket: @bucket_name, key: filename, body: content.to_blob, acl: 'public-read') elsif @s3_client.put_object(bucket: @bucket_name, key: filename, body: content, acl: 'public-read') end end |
#touch(filename) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/el_finder_s3/s3_connector.rb', line 75 def touch(filename) begin @s3_client.put_object(bucket: @bucket_name, key: filename) true rescue false end end |