-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
Static website hosting | ||
```xml | ||
<RoutingRules> | ||
<RoutingRule> | ||
<Condition> | ||
<KeyPrefixEquals/> | ||
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals> | ||
</Condition> | ||
<Redirect> | ||
<Protocol>https</Protocol> | ||
<HostName>__DOMAIN__</HostName> | ||
<ReplaceKeyPrefixWith>__PATH_TO_LAMBDA__?path=</ReplaceKeyPrefixWith> | ||
<HttpRedirectCode>307</HttpRedirectCode> | ||
</Redirect> | ||
</RoutingRule> | ||
</RoutingRules> | ||
``` | ||
|
||
|
||
Bucket policy | ||
```json | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "AddPerm", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": "s3:GetObject", | ||
"Resource": "arn:aws:s3:::__BUCKET_NAME__/*" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
|
||
Role policy document | ||
```json | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents" | ||
], | ||
"Resource": "arn:aws:logs:*:*:*" | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": "s3:PutObject", | ||
"Resource": "arn:aws:s3:::__BUCKET_NAME__/*" | ||
} | ||
] | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict' | ||
|
||
|
||
const AWS = require('aws-sdk') | ||
const S3 = new AWS.S3({signatureVersion: 'v4'}); | ||
const Sharp = require('sharp'); | ||
const Pattern = new RegExp("(.*/)?(.*)/(.*)"); | ||
|
||
// parameters | ||
const BUCKET = process.env.BUCKET; | ||
const URL = process.env.URL; | ||
|
||
|
||
exports.handler = function(event, context, callback) { | ||
var path = event.queryStringParameters.path; | ||
var parts = Pattern.exec(path); | ||
var dir = parts[1] || ''; | ||
var options = parts[2].split('_'); | ||
var filename = parts[3]; | ||
|
||
|
||
var sizes = options[0].split("x"); | ||
var func = options.length > 1 ? options[1] : null; | ||
|
||
var contentType; | ||
S3.getObject({Bucket: BUCKET, Key: dir + filename}) | ||
.promise() | ||
.then(data => { | ||
contentType = data.ContentType; | ||
var img = Sharp(data.Body) | ||
.resize( | ||
sizes[0] === 'AUTO' ? null : parseInt(sizes[0]), | ||
sizes[1] === 'AUTO' ? null : parseInt(sizes[1])); | ||
|
||
switch (func){ | ||
case 'max': img = img.max(); break; | ||
case 'min': img = img.min(); break; | ||
case null: break; | ||
default: | ||
callback(null, { | ||
statusCode: 400, | ||
body: `Unknown func parameter "${func}"\n` + | ||
'For query ".../150x150_func", "_func" must be either empty or "_min" or "_max"', | ||
headers: {"Content-Type": "text/plain"} | ||
}) | ||
return new Promise(() => {}); | ||
} | ||
|
||
return img.withoutEnlargement().toBuffer(); | ||
}) | ||
.then(result => | ||
S3.putObject({ | ||
Body: result, | ||
Bucket: BUCKET, | ||
ContentType: contentType, | ||
Key: path | ||
}).promise() | ||
) | ||
.then(() => | ||
callback(null, { | ||
statusCode: 301, | ||
headers: {"Location" : `${URL}/${path}`} | ||
}) | ||
) | ||
.catch(e => { | ||
callback(null, { | ||
statusCode: e.statusCode || 400, | ||
body: 'Exception: ' + e.message, | ||
headers: {"Content-Type": "text/plain"} | ||
}) | ||
}); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.