lambda_convert 0.0.5 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ab417e94470f8f933b2d90b7010d3050cbdf608d
4
- data.tar.gz: c2748ee3e263683c0b7aadf00d4a2da2e5b5883d
2
+ SHA256:
3
+ metadata.gz: dae0a87a2bc2e45653d677a270f8ff84b605f4175a9250b5241d309673a52180
4
+ data.tar.gz: 7d809ea4f044bbd9eae311ea7909f3f129baf1d32bc47a31b6c073577dcfa79c
5
5
  SHA512:
6
- metadata.gz: 30120d3f06649b62c0bdf9b3bcf44ca68fbf4a4d6efcc7a14b198187f054ff86d2a101a7eb1fec24a4ec84726812ca26b2425649344a6a587681ec3ffda0b6b3
7
- data.tar.gz: 68052dd61c6afdc2da6d303105169eb302376b0d871d2ffcda3bdf2fdd8ccec4ad872578cb85fbcfb2651893b8b595c4a5047bf90e8ab53b145eaf77bfc5c403
6
+ metadata.gz: 799f4822b0010635000df3f6256908fccaef8faf966d9ed30088b1388e5f2e0d97b75d716c09f67a8040161f9f65a5c33535f00fb64b2ca824c05c42b45662fc
7
+ data.tar.gz: 6517f828ceddd7a726acb56c321ccd281ea1eafa9c3f9a52df67a1a1e77f348ad1632276e3776d886362c88f670bb1149eede818b72949bccbc7a6c4ad11afc1
data/README.md CHANGED
@@ -19,11 +19,15 @@ To eliminate the big image file uploading issue and the security risk, the idea
19
19
 
20
20
  ## Environment variables
21
21
 
22
- - **CONVERT_S3_REGION** - AWS region for S3, default value will be reading from `AWS_REGION` if this environment variable is not set. (required)
23
- - **CONVERT_LAMBDA_REGION** - AWS region for Lambda, default value will be reading from `AWS_REGION` if this environment variable is not set. (required)
24
- - **CONVERT_ACCESS_KEY** - AWS access key, default value will be reading from `AWS_ACCESS_KEY_ID` if this environment variable is not set. (required)
25
- - **CONVERT_SECRET_ACCESS_KEY** - AWS secret key, default value will be reading from `AWS_SECRET_ACCESS_KEY` if this environment variable is not set. (required)
22
+ - **CONVERT_S3_REGION** - AWS region for S3, default value will be read from `AWS_REGION` if this environment variable is not set.
23
+ - **CONVERT_LAMBDA_REGION** - AWS region for Lambda, default value will be read from `AWS_REGION` if this environment variable is not set.
24
+ - **CONVERT_ACCESS_KEY** - AWS access key, default value will follow standard `aws-sdk` credential lookup sequence
25
+ - **CONVERT_SECRET_ACCESS_KEY** - AWS secret key, default value will follow standard `aws-sdk` credential lookup sequence
26
26
  - **CONVERT_S3_BUCKET** - AWS S3 bucket. (required)
27
27
  - **CONVERT_S3_KEY_PREFIX** - AWS S3 temporary file uploading prefix, default value is `_convert_tmp/`
28
28
  - **CONVERT_LAMBDA_FUNCTION** - Name of the AWS Lambda function to invoke, default value is `image-convert-prod`
29
29
  - **CONVERT_DISABLE_FALLBACK** - By default, this command line tool fallbacks to local `convert` command if remote operation fails. Set this value to 1 to disable the fallback behavior.
30
+
31
+ ## The AWS Lambda function
32
+
33
+ The AWS Lambda function for running ImageMagick can be found here at https://github.com/envoy/envoy-convert
@@ -1,103 +1,110 @@
1
- require 'rubygems'
2
1
  require 'json'
2
+ require 'securerandom'
3
3
  require 'English'
4
4
 
5
- require 'aws-sdk'
5
+ require 'aws-sdk-lambda'
6
+ require 'aws-sdk-s3'
7
+ require 'forwardable'
6
8
 
7
9
  module LambdaConvert
8
10
  # `convert` command line tool implementation
9
11
  module CLI
10
- class <<self
12
+ class << self
13
+ extend Forwardable
14
+ def_delegator :LambdaConvert, :config
11
15
  attr_accessor :logger
12
- end
13
-
14
- def self.lambda_convert
15
- s3_region = ENV['CONVERT_S3_REGION'] || ENV['AWS_REGION']
16
- lambda_region = ENV['CONVERT_LAMBDA_REGION'] || ENV['AWS_REGION']
17
- aws_credentials = Aws::Credentials.new(
18
- ENV['CONVERT_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID'],
19
- ENV['CONVERT_SECRET_ACCESS_KEY'] || ENV['AWS_SECRET_ACCESS_KEY']
20
- )
21
- s3_bucket = ENV['CONVERT_S3_BUCKET']
22
- s3_key_prefix = ENV['CONVERT_S3_KEY_PREFIX'] || '_convert_tmp/'
23
- lambda_function = ENV['CONVERT_LAMBDA_FUNCTION'] || 'image-convert-prod'
24
16
 
25
- s3 = Aws::S3::Client.new(
26
- region: s3_region,
27
- credentials: aws_credentials
28
- )
29
- aws_lambda = Aws::Lambda::Client.new(
30
- region: lambda_region,
31
- credentials: aws_credentials
32
- )
33
-
34
- abort('Invalid arguments') if ARGV.count < 2
17
+ def aws_credentials
18
+ if config.access_key && config.secret_key
19
+ Aws::Credentials.new(config.access_key, config.secret_key)
20
+ end
21
+ end
35
22
 
36
- input_file, input_selecting = LambdaConvert::Utils.parse_input_path(
37
- ARGV[0]
38
- )
39
- # Notice: there is also special output file syntax for convert command,
40
- # but we are not supporting them now, as we probably won't use it
41
- output_file = ARGV[-1]
23
+ def s3_client
24
+ @s3_client ||= Aws::S3::Client.new(
25
+ region: config.s3_region,
26
+ credentials: aws_credentials
27
+ )
28
+ end
42
29
 
43
- input_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
44
- output_key = "#{s3_key_prefix}#{SecureRandom.uuid}"
30
+ def lambda_client
31
+ @aws_lambda ||= Aws::Lambda::Client.new(
32
+ region: config.lambda_region,
33
+ credentials: aws_credentials
34
+ )
35
+ end
36
+ end
45
37
 
46
- logger.info("Uploading file to s3://#{s3_bucket}/#{input_key}")
38
+ def self.upload_file(input_file, input_key)
39
+ logger.info("Uploading file to s3://#{config.s3_bucket}/#{input_key}")
47
40
  File.open(input_file, 'rb') do |file|
48
- s3.put_object(bucket: s3_bucket, key: input_key, body: file)
41
+ s3_client.put_object(bucket: config.s3_bucket, key: input_key, body: file)
49
42
  end
43
+ end
44
+
45
+ def self.invoke_lambda(input_key, input_selecting, args, output_key)
50
46
  source = '{source}'
51
47
  source += "[#{input_selecting}]" unless input_selecting.nil?
52
48
  instruction = {
53
49
  schema: 'envoy-convert-instruction',
54
50
  original: input_key,
55
- bucket: s3_bucket,
56
- write_options: {
57
- acl: 'private'
58
- },
51
+ bucket: config.s3_bucket,
52
+ write_options: {},
59
53
  key: output_key,
60
- args: [source] + ARGV[1..-2] + ['{dest}']
54
+ args: [source] + args + ['{dest}']
61
55
  }
62
56
  logger.info("Invoking lambda with instruction #{instruction}")
63
57
 
64
- resp = aws_lambda.invoke(
65
- function_name: lambda_function,
58
+ resp = lambda_client.invoke(
59
+ function_name: config.lambda_function,
66
60
  invocation_type: 'RequestResponse',
67
61
  payload: JSON.dump(instruction)
68
62
  )
69
63
  logger.info("Get response of invoke #{resp}")
70
64
  raise 'Failed to run convert on Lambda' if resp.status_code != 200
65
+ end
71
66
 
67
+ def self.download_file(output_key, output_file)
72
68
  logger.info(
73
- "Downloading file from s3://#{s3_bucket}/#{output_key} to " \
69
+ "Downloading file from s3://#{config.s3_bucket}/#{output_key} to " \
74
70
  "#{output_file}"
75
71
  )
76
- s3.get_object(
72
+ s3_client.get_object(
77
73
  response_target: output_file,
78
- bucket: s3_bucket,
74
+ bucket: config.s3_bucket,
79
75
  key: output_key
80
76
  )
77
+ end
78
+
79
+ def self.delete_files(keys)
80
+ logger.info("Delete files #{keys} from #{config.s3_bucket}")
81
+ s3_client.delete_objects(
82
+ bucket: config.s3_bucket,
83
+ delete: {
84
+ objects: keys.map { |key| { key: key } },
85
+ quiet: true
86
+ }
87
+ )
88
+ end
89
+
90
+ def self.lambda_convert
91
+ input_file, input_selecting = LambdaConvert::Utils.parse_input_path(
92
+ ARGV[0]
93
+ )
94
+ # Notice: there is also special output file syntax for convert command,
95
+ # but we are not supporting them now, as we probably won't use it
96
+ output_file = ARGV[-1]
97
+ input_key = File.join config.s3_key_prefix.to_s, SecureRandom.uuid
98
+ output_key = File.join config.s3_key_prefix.to_s, SecureRandom.uuid
99
+
100
+ upload_file(input_file, input_key)
101
+ invoke_lambda(input_key, input_selecting, ARGV[1..-2], output_key)
102
+ download_file(output_key, output_file)
103
+
81
104
  logger.info('Done')
82
105
  ensure
83
- if !s3.nil? && !input_key.nil? && !output_key.nil?
84
- logger.info(
85
- "Delete files #{input_key} and #{output_key} from #{s3_bucket}"
86
- )
87
- s3.delete_objects(
88
- bucket: s3_bucket,
89
- delete: {
90
- objects: [
91
- {
92
- key: input_key
93
- },
94
- {
95
- key: output_key
96
- }
97
- ],
98
- quiet: true
99
- }
100
- )
106
+ if !input_key.nil? && !output_key.nil?
107
+ delete_files([input_key, output_key])
101
108
  end
102
109
  end
103
110
 
@@ -117,9 +124,9 @@ module LambdaConvert
117
124
 
118
125
  def self.main
119
126
  abort('Recursive call') if ENV['CONVERT_RECURSIVE_FLAG'] == '1'
127
+ abort('Invalid arguments') if ARGV.count < 2
120
128
  lambda_convert
121
129
  rescue StandardError => e
122
-
123
130
  logger.warn("Failed to convert via lambda, error=#{e}")
124
131
  fallback_disabled = (ENV['CONVERT_DISABLE_FALLBACK'].to_i != 0) || false
125
132
  if fallback_disabled
@@ -0,0 +1,22 @@
1
+ module LambdaConvert
2
+ class Config
3
+ attr_accessor :access_key,
4
+ :lambda_function,
5
+ :lambda_region,
6
+ :s3_bucket,
7
+ :s3_key_prefix,
8
+ :s3_region,
9
+ :secret_key
10
+
11
+ # Set some defaults
12
+ def initialize
13
+ @access_key = ??
14
+ @secret_key = ??
15
+ @lambda_function = "image-convert-dev"
16
+ @lambda_region = "us-east-1"
17
+ @s3_region = "us-east-1"
18
+ @s3_bucket = "envoy-development-staging-2"
19
+ @s3_key_prefix = "_convert_tmp"
20
+ end
21
+ end
22
+ end
@@ -1,2 +1,13 @@
1
1
  require 'lambda_convert/cli'
2
+ require 'lambda_convert/config'
2
3
  require 'lambda_convert/utils'
4
+
5
+ module LambdaConvert
6
+ def self.config
7
+ @config ||= Config.new
8
+ end
9
+
10
+ def self.configure(&_block)
11
+ yield config
12
+ end
13
+ end
metadata CHANGED
@@ -1,48 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lambda_convert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fang-Pen Lin
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2017-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: aws-sdk
14
+ name: aws-sdk-lambda
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.6'
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.6'
27
- description:
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-s3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description:
28
42
  email: fang@envoy.com
29
43
  executables:
30
44
  - convert
31
45
  extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
34
- - "./lib/.DS_Store"
35
48
  - "./lib/lambda_convert.rb"
36
49
  - "./lib/lambda_convert/cli.rb"
50
+ - "./lib/lambda_convert/config.rb"
37
51
  - "./lib/lambda_convert/utils.rb"
38
52
  - LICENSE
39
53
  - README.md
40
54
  - bin/convert
41
- homepage:
55
+ homepage:
42
56
  licenses:
43
57
  - MIT
44
58
  metadata: {}
45
- post_install_message:
59
+ post_install_message:
46
60
  rdoc_options: []
47
61
  require_paths:
48
62
  - lib
@@ -57,11 +71,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
71
  - !ruby/object:Gem::Version
58
72
  version: '0'
59
73
  requirements: []
60
- rubyforge_project:
61
- rubygems_version: 2.4.5.1
62
- signing_key:
74
+ rubygems_version: 3.4.22
75
+ signing_key:
63
76
  specification_version: 4
64
77
  summary: AWS Lambda powered drop-in replacement for ImageMagick convert command line
65
78
  tool
66
79
  test_files: []
67
- has_rdoc:
data/lib/.DS_Store DELETED
Binary file