porky_lib 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c98aaa41950874c7b37f159230db8356e526dec8f35a2a7d9c5fbe9d1f484a21
4
- data.tar.gz: 5997f378160b2b05cd176c44b25570374159cbaafa7d8f83e1eb8c52014d7ca6
3
+ metadata.gz: 99fe9eed54e08ab3c553aa2fb80256f6b7aaccac49fee918c1c12f713b93bef8
4
+ data.tar.gz: c3174d94c93b20e609b0058395893a5fa5a011e26df50acdc3727f4b7d9d73dd
5
5
  SHA512:
6
- metadata.gz: 2bbcd43825926a03d5382cefad9ff6053a83117aaf163a12bd6e5a0502d595ce4b2167be745e1417e4d019e88df9f1a8c6e0e2a9266c5273245534c34d83cac2
7
- data.tar.gz: 93dbfb9406f2773017fac22946eba097405945181cc3bf26762f222dff1c1a27538c04eec9006aecb626a201357df11fe7073bd4a023a39fc97e458caec8d13d
6
+ metadata.gz: 20fc71c1519a16e5950d98a8437f8c08df4fbec90ede3a12247f5569005632b790b1f8de3a845ef96736e4202cf50724c4316e0aad24a545cf5a19f683cde925
7
+ data.tar.gz: d22574f96074ea36500af793b730cab786f9939ee62050c3d800823c789334109de6ad7f2fcda1e12b4b020bee40a16ccad71947cb8bb72083a18ae1af21b802
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- porky_lib (0.7.0)
4
+ porky_lib (0.8.0)
5
5
  aws-sdk-kms
6
6
  aws-sdk-s3
7
7
  msgpack
data/README.md CHANGED
@@ -35,10 +35,14 @@ Something like the following should be included in an initializer in your Rails
35
35
  ```ruby
36
36
  # Use PorkyLib's AWS KMS mock client except in production, for example
37
37
  use_mock_client = !Rails.env.production?
38
+ max_file_size = 0 # max file size allowed, in bytes - defaults to 0
39
+ presign_url_expires_in = 300 # expiry time for presigned urls, in seconds - defaults to 300 (5 minutes)
38
40
  PorkyLib::Config.configure(aws_region: ENV[AWS_REGION],
39
41
  aws_key_id: ENV[AWS_KEY_ID],
40
42
  aws_key_secret: ENV[AWS_KEY_SECRET],
41
- aws_client_mock: use_mock_client)
43
+ aws_client_mock: use_mock_client,
44
+ max_file_size: max_file_size,
45
+ presign_url_expires_in: presign_url_expires_in)
42
46
  PorkyLib::Config.initialize_aws
43
47
  ```
44
48
 
@@ -152,6 +156,23 @@ file_data = PorkyLib::FileService.read(bucket_name, file_key)
152
156
  file_key = PorkyLib::FileService.write(file, bucket_name, key_id, options)
153
157
  ```
154
158
 
159
+ ### Generate S3 Presigned POST URL
160
+ To generate a new presigned POST url (used to upload files directly to AWS S3):
161
+ ```ruby
162
+ # Where bucket_name is the name of the S3 bucket to write to
163
+ # options is an optional parameter for specifying optional metadata about the file
164
+ # file_key is randomly generated, unless it's passed as a parameter in the options hash using 'file_name' as key
165
+ url, file_key = PorkyLib::Symmetric.instance.presigned_post_url(bucket_name, options)
166
+ ```
167
+
168
+ ### Generate S3 Presigned GET URL
169
+ To generate a new presigned GET url (used to download files directly from AWS S3):
170
+ ```ruby
171
+ # Where bucket_name is the name of the S3 bucket to read from
172
+ # file_key is the file identifier of the file/data that was written to S3.
173
+ url = PorkyLib::Symmetric.instance.presigned_get_url(bucket_name, file_key)
174
+ ```
175
+
155
176
  ## Development
156
177
 
157
178
  Development on this project should occur on separate feature branches and pull requests should be submitted. When submitting a
@@ -6,13 +6,15 @@ class PorkyLib::Config
6
6
  @aws_key_secret = ''
7
7
  @aws_client_mock = false
8
8
  @max_file_size = 0
9
+ @presign_url_expires_in = 300 # 5 minutes
9
10
 
10
11
  @config = {
11
12
  aws_region: @aws_region,
12
13
  aws_key_id: @aws_key_id,
13
14
  aws_key_secret: @aws_key_secret,
14
15
  aws_client_mock: @aws_client_mock,
15
- max_file_size: @max_file_size
16
+ max_file_size: @max_file_size,
17
+ presign_url_expires_in: @presign_url_expires_in
16
18
  }
17
19
 
18
20
  @allowed_config_keys = @config.keys
@@ -91,6 +91,27 @@ class PorkyLib::FileService
91
91
  tempfile.unlink
92
92
  end
93
93
 
94
+ def presigned_post_url(bucket_name, options = {})
95
+ file_name = options[:file_name] || SecureRandom.uuid
96
+ obj = s3.bucket(bucket_name).object(file_name)
97
+
98
+ presigned_url = obj.presigned_url(:put,
99
+ expires_in: presign_url_expires_in,
100
+ metadata: options[:metadata])
101
+ [presigned_url, file_name]
102
+ rescue Aws::Errors::ServiceError => e
103
+ raise FileServiceError, "PresignedPostUrl for #{file_name} from S3 bucket #{bucket_name} failed: #{e.message}"
104
+ end
105
+
106
+ def presigned_get_url(bucket_name, file_key)
107
+ obj = s3.bucket(bucket_name).object(file_key)
108
+
109
+ obj.presigned_url(:get,
110
+ expires_in: presign_url_expires_in)
111
+ rescue Aws::Errors::ServiceError => e
112
+ raise FileServiceError, "PresignedGetUrl for #{file_key} from S3 bucket #{bucket_name} failed: #{e.message}"
113
+ end
114
+
94
115
  private
95
116
 
96
117
  def input_invalid?(file, bucket_name, key_id)
@@ -171,6 +192,10 @@ class PorkyLib::FileService
171
192
  tempfile
172
193
  end
173
194
 
195
+ def presign_url_expires_in
196
+ PorkyLib::Config.config[:presign_url_expires_in]
197
+ end
198
+
174
199
  def s3_client
175
200
  @s3_client ||= Aws::S3::Client.new
176
201
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PorkyLib
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: porky_lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Fletcher
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-04 00:00:00.000000000 Z
11
+ date: 2019-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-kms