lambda_convert 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -0
- data/lib/lambda_convert/cli.rb +17 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25c44e1fb6fa07cb8e1aaf5654100b73721aa0c6
|
4
|
+
data.tar.gz: ec7ba833db940fa8dea581cdcd37265e1cea6142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12347aac344bf05dd0e0ca8add187610d50d5adf81b962f0136a04c989c5a669db5e78f0ecd075ff5087f32c261c764783714c20304297407c6bfff7a1353aaf
|
7
|
+
data.tar.gz: fa4d46a0d15d393f455e20fcd45a2cd68c9ff4585052bb59727550cce5741be24c9be1f02f2f0cadd8abcbc8bbef8387adf46eb6904c59da8074d4a015e0eb5a
|
data/README.md
CHANGED
@@ -1,2 +1,29 @@
|
|
1
1
|
# lambda-convert
|
2
2
|
AWS Lambda powered drop-in replacement for ImageMagick convert command line tool
|
3
|
+
|
4
|
+
## Background
|
5
|
+
|
6
|
+
At Envoy, we have many image file uploaded by users and will be resized via `convert` (ImageMagick) command line tool. It works fine, the only problems are
|
7
|
+
|
8
|
+
### Dealing with big GIF image
|
9
|
+
|
10
|
+
When user upload a GIF image, to resize it, ImageMagick will need to load the all frames into memory. In that case, even the GIF image file is very small, could posiblly consume huge amount of memory. This brings big impact to our API server, and sometimes the uploading request fails due to this reason.
|
11
|
+
|
12
|
+
### Security concerns
|
13
|
+
|
14
|
+
Despite it's not really easy to perform, it still possible to leverage exploits of certain image file format loading code in ImageMagick.
|
15
|
+
|
16
|
+
## Solution
|
17
|
+
|
18
|
+
To eliminate the big image file uploading issue and the security risk, the idea here is to do image resizing on AWS Lambda instead of localhost. This command line tool is a drop-in replacement for `convert` command, except it upload the input image file to S3, does the resizing on AWS Lambda and finally down the result image back to localhost.
|
19
|
+
|
20
|
+
## Environment variables
|
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)
|
26
|
+
- **CONVERT_S3_BUCKET** - AWS S3 bucket. (required)
|
27
|
+
- **CONVERT_S3_KEY_PREFIX** - AWS S3 temporary file uploading prefix, default value is `_convert_tmp/`
|
28
|
+
- **CONVERT_LAMBDA_FUNCTION** - Name of the AWS Lambda function to invoke, default value is `image-convert-prod`
|
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.
|
data/lib/lambda_convert/cli.rb
CHANGED
@@ -4,6 +4,14 @@ require 'English'
|
|
4
4
|
|
5
5
|
require 'aws-sdk'
|
6
6
|
|
7
|
+
# find command path array matching given `cmd` name in $PATH
|
8
|
+
def find_cmd(cmd)
|
9
|
+
(ENV['PATH'].split(File::PATH_SEPARATOR).map do |path|
|
10
|
+
cmd_path = File.join(path, cmd)
|
11
|
+
cmd_path if File.executable?(cmd_path) && !File.directory?(cmd_path)
|
12
|
+
end).compact
|
13
|
+
end
|
14
|
+
|
7
15
|
def parse_input_path(path)
|
8
16
|
# convert command input path could be attached with selecting syntax, let's
|
9
17
|
# parse it and return them in an array of [filename, selecting syntax]
|
@@ -97,15 +105,19 @@ end
|
|
97
105
|
|
98
106
|
def local_convert
|
99
107
|
logger = Logger.new(STDERR)
|
108
|
+
|
100
109
|
env = ENV.to_h
|
101
|
-
#
|
102
|
-
|
103
|
-
|
110
|
+
# find the original convert bin path
|
111
|
+
original_convert = find_cmd('convert').find do |path|
|
112
|
+
# TODO: maybe we need a more robust way to determine whether is given
|
113
|
+
# convert path from us or someone else
|
114
|
+
File.dirname(path) != Gem.bindir && !path.include?('.rbenv/shims')
|
115
|
+
end
|
104
116
|
# we also put a CONVERT_RECURSIVE_FLAG to avoid somehow calling ourself again
|
105
117
|
# by mistake
|
106
118
|
env['CONVERT_RECURSIVE_FLAG'] = '1'
|
107
|
-
logger.info("Running local convert with args #{ARGV}")
|
108
|
-
system(env, *([
|
119
|
+
logger.info("Running local convert #{original_convert} with args #{ARGV}")
|
120
|
+
system(env, *([original_convert] + ARGV))
|
109
121
|
abort('Failed to run local convert') unless $CHILD_STATUS.success?
|
110
122
|
logger.info('Done')
|
111
123
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lambda_convert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fang-Pen Lin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|