Class: BraveSearch::Storage::S3
- Inherits:
-
Object
- Object
- BraveSearch::Storage::S3
- Defined in:
- lib/brave_search/storage/s3.rb
Instance Method Summary collapse
- #download(url, key:) ⇒ Object
-
#initialize(bucket:, access_key: nil, secret_key: nil, endpoint: nil, region: "us-east-1") ⇒ S3
constructor
A new instance of S3.
- #upload(content, key:) ⇒ Object
Constructor Details
#initialize(bucket:, access_key: nil, secret_key: nil, endpoint: nil, region: "us-east-1") ⇒ S3
Returns a new instance of S3.
9 10 11 12 13 14 15 |
# File 'lib/brave_search/storage/s3.rb', line 9 def initialize(bucket:, access_key: nil, secret_key: nil, endpoint: nil, region: "us-east-1") @bucket = bucket @access_key = access_key || ENV.fetch("AWS_ACCESS_KEY_ID", nil) @secret_key = secret_key || ENV.fetch("AWS_SECRET_ACCESS_KEY", nil) @endpoint = endpoint || "https://s3.#{region}.amazonaws.com" @region = region end |
Instance Method Details
#download(url, key:) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/brave_search/storage/s3.rb', line 26 def download(url, key:) uri = URI(url) response = Net::HTTP.get_response(uri) raise "Download failed: #{response.code}" unless response.code == "200" upload_result = upload(response.body, key: key) { key: key, url: upload_result[:url], size: response.body.bytesize, original_url: url } end |
#upload(content, key:) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/brave_search/storage/s3.rb', line 17 def upload(content, key:) # For now, simulate upload { key: key, url: "#{@endpoint}/#{@bucket}/#{key}", size: content.bytesize } end |