-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement presigned url method in the Bucket object
An handy method to generate a presigned url to download CSV files from the bucket.
- Loading branch information
Showing
7 changed files
with
103 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
lib/defra_ruby/aws/services/concerns/has_aws_bucket_configuration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module DefraRuby | ||
module Aws | ||
module HasAwsBucketConfiguration | ||
def s3_bucket | ||
s3.bucket(bucket.bucket_name) | ||
end | ||
|
||
def s3 | ||
::Aws::S3::Resource.new( | ||
region: bucket.region, | ||
credentials: aws_credentials | ||
) | ||
end | ||
|
||
def aws_credentials | ||
::Aws::Credentials.new(bucket.access_key_id, bucket.secret_access_key) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module DefraRuby | ||
module Aws | ||
class PresignedUrlService | ||
include HasAwsBucketConfiguration | ||
|
||
def self.run(bucket, file_name) | ||
new(bucket, file_name).run | ||
end | ||
|
||
def initialize(bucket, file_name) | ||
@bucket = bucket | ||
@file_name = file_name | ||
end | ||
|
||
def run | ||
s3_bucket.object(file_name).presigned_url( | ||
:get, | ||
expires_in: 20 * 60, # 20 minutes in seconds | ||
secure: true, | ||
response_content_type: "text/csv", | ||
response_content_disposition: "attachment; filename=#{file_name}" | ||
) | ||
end | ||
|
||
private | ||
|
||
attr_reader :bucket, :file_name | ||
end | ||
end | ||
end |
32 changes: 32 additions & 0 deletions
32
spec/lib/defra_ruby/aws/services/presigned_url_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module DefraRuby | ||
module Aws | ||
RSpec.describe PresignedUrlService do | ||
describe ".run" do | ||
let(:configs) do | ||
{ | ||
name: "test", | ||
credentials: { | ||
access_key_id: "123", | ||
secret_access_key: "Secret" | ||
} | ||
} | ||
end | ||
let(:bucket) { Bucket.new(configs) } | ||
let(:presigned_url) { described_class.run(bucket, "testfile.csv") } | ||
|
||
it "returns a presigned url for a given file in the bucket" do | ||
expect(presigned_url).to include("https://test.s3.eu-west-1.amazonaws.com/testfile.csv") | ||
expect(presigned_url).to include("response-content-disposition=attachment") | ||
expect(presigned_url).to include("response-content-type=text%2Fcsv") | ||
expect(presigned_url).to include("Amz-Expires") | ||
expect(presigned_url).to include("Amz-Credential") | ||
expect(presigned_url).to include("Amz-Signature") | ||
end | ||
end | ||
end | ||
end | ||
end |