Class: QuickAndRuby::S3::WebIndexer
- Inherits:
-
Object
- Object
- QuickAndRuby::S3::WebIndexer
- Defined in:
- lib/quick_and_ruby/s3/web_indexer.rb
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#endpoint_url ⇒ Object
readonly
Returns the value of attribute endpoint_url.
-
#index_template ⇒ Object
readonly
Returns the value of attribute index_template.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Instance Method Summary collapse
- #index(*paths, nested: false) ⇒ Object
- #index_directory(path, nested: false) ⇒ Object
-
#initialize(bucket:, endpoint_url:, access_key:, secret_key:, region: 'us-east-1', index_template: WebIndexRenderer.new, **extra) ⇒ WebIndexer
constructor
A new instance of WebIndexer.
- #list_directory(path, recursive: false) ⇒ Object
- #s3_client ⇒ Object
Constructor Details
#initialize(bucket:, endpoint_url:, access_key:, secret_key:, region: 'us-east-1', index_template: WebIndexRenderer.new, **extra) ⇒ WebIndexer
Returns a new instance of WebIndexer.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 13 def initialize(bucket:, endpoint_url:, access_key:, secret_key:, region: 'us-east-1', index_template: WebIndexRenderer.new, **extra) @bucket = bucket @endpoint_url = endpoint_url @access_key = access_key @secret_key = secret_key @region = region @index_template = index_template @extra = extra end |
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
11 12 13 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 11 def access_key @access_key end |
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
11 12 13 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 11 def bucket @bucket end |
#endpoint_url ⇒ Object (readonly)
Returns the value of attribute endpoint_url.
11 12 13 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 11 def endpoint_url @endpoint_url end |
#index_template ⇒ Object (readonly)
Returns the value of attribute index_template.
11 12 13 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 11 def index_template @index_template end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
11 12 13 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 11 def region @region end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
11 12 13 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 11 def secret_key @secret_key end |
Instance Method Details
#index(*paths, nested: false) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 24 def index(*paths, nested: false) paths.append('/') if paths.empty? status = paths.inject(0) { |status, path| status + index_directory(path, nested: nested) } status = 1 if status != 0 status end |
#index_directory(path, nested: false) ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 82 def index_directory(path, nested: false) files = list_directory(path, recursive: nested) index_content = index_template.generate( path: path, files: files ) s3_client.put_object(bucket: bucket, key: "#{path}/index.html", body: index_content, acl: 'public-read', content_type: 'text/html') 0 end |
#list_directory(path, recursive: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 32 def list_directory(path, recursive: false) objects = [] continuation_token = nil prefix = path.end_with?('/') ? path : "#{path}/" loop do resp = s3_client.list_objects_v2( bucket: bucket, prefix: prefix, delimiter: (recursive ? nil : '/'), continuation_token: continuation_token ) resp.common_prefixes&.each do |cp| next unless cp.prefix.start_with?(prefix) relativepath = cp.prefix.delete_prefix(prefix) objects.append( { filename: relativepath.chomp('/').split('/').last, filename_relative_path: relativepath.to_s, filename_realpath: "/#{bucket}/#{cp.prefix}", last_modified: nil, size: nil, is_directory: true } ) end resp.contents.each do |object| next if object.key == File.join(prefix, 'index.html') relative_key = object.key.sub(prefix, '') next if !recursive && relative_key.inlcude?('/') && !relative_key.empty? objects.append( { filename: relative_key, filename_relative_path: relative_key, filename_realpath: "/#{bucket}/#{object.key}", last_modified: object.last_modified, size: object.size, is_directory: false } ) end continuation_token = resp.next_continuation_token break unless continuation_token end objects end |
#s3_client ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/quick_and_ruby/s3/web_indexer.rb', line 93 def s3_client @s3_client ||= Aws::S3::Client.new( access_key_id: access_key, secret_access_key: secret_key, endpoint: endpoint_url, region: region, force_path_style: true, ssl_verify_peer: false ) end |