-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge release/v1.10.0 into master
Merge pull request #843 from awslabs/release/v1.10.0
- Loading branch information
Showing
234 changed files
with
23,157 additions
and
1,851 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[flake8] | ||
max-line-length = 120 | ||
ignore = E126 | ||
ignore = E126 F821 W504 W605 |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# AWS::Serverless::S3 Event Code Example | ||
This example shows you how to get events of S3 bucket. When you upload an image in the source bucket it will resize that image and save in the destination bucket. | ||
###### Note | ||
Don't forget to chnage S3 bucket name, source bucket prefix. | ||
|
||
## Package and deploy template | ||
#### Build | ||
Run the following command to build binary of your code | ||
``` | ||
GOOS=linux go build -o main | ||
``` | ||
#### Package | ||
To package your application run the following command | ||
``` | ||
sam package --template-file template.yaml --output-template-file serverless-output.yaml --s3-bucket <YOUR_S3_BUCKET> | ||
``` | ||
It will validate the template, zip you applicaiton, upload to your S3 bucket and generates the output template. Replace the `<YOUR_S3_BUCKET>` with your bucket name. | ||
#### Deploy | ||
Run the following command replacing `<BUCKET_PREFIX>` with your desire prefix name to deploye your application | ||
``` | ||
sam deploy --template-file serverless-output.yaml --stack-name image-resizer --capabilities CAPABILITY_IAM --parameter-overrides BucketNamePrefix=<BUCKET_PREFIX> | ||
``` | ||
It will create necessary resources and link them according to the template using cloudformation. | ||
|
||
## Test | ||
Upload an image in `JPEG` format to the `SourceBucket` defined in the template and verify the same image with smaller size in `DestBucket`. | ||
|
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,72 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
"github.com/nfnt/resize" | ||
"image" | ||
"image/jpeg" | ||
"log" | ||
"os" | ||
) | ||
|
||
func Handler(event events.S3Event) (string, error) { | ||
dstBucket := os.Getenv("DestBucket") | ||
svc := s3.New(session.New()) | ||
downloader := s3manager.NewDownloaderWithClient(svc) | ||
uploader := s3manager.NewUploaderWithClient(svc) | ||
|
||
for _, record := range event.Records { | ||
srcBucket := record.S3.Bucket.Name | ||
srcKey := record.S3.Object.Key | ||
imgSize := record.S3.Object.Size | ||
|
||
file := make([]byte, imgSize) | ||
_, err := downloader.Download(aws.NewWriteAtBuffer(file), | ||
&s3.GetObjectInput{ | ||
Bucket: aws.String(srcBucket), | ||
Key: aws.String(srcKey), | ||
}) | ||
if err != nil { | ||
log.Fatalln("Donwload error: " + err.Error()) | ||
} | ||
|
||
reader := bytes.NewReader(file) | ||
img, _, err := image.Decode(reader) | ||
if err != nil { | ||
log.Fatalln("Decode error: " + err.Error()) | ||
} | ||
|
||
resizedImg := resize.Resize(300, 0, img, resize.Lanczos3) | ||
|
||
buf := new(bytes.Buffer) | ||
err = jpeg.Encode(buf, resizedImg, &jpeg.Options{ | ||
Quality: 50, | ||
}) | ||
if err != nil { | ||
log.Fatalln("Encode error: " + err.Error()) | ||
} | ||
|
||
imgBody := bytes.NewReader(buf.Bytes()) | ||
|
||
_, err = uploader.Upload(&s3manager.UploadInput{ | ||
Bucket: aws.String(dstBucket), | ||
Key: aws.String(srcKey), | ||
Body: imgBody, | ||
}) | ||
if err != nil { | ||
log.Fatalln("Upload error: " + err.Error()) | ||
} | ||
} | ||
|
||
return "Resize successful", nil | ||
} | ||
|
||
func main() { | ||
lambda.Start(Handler) | ||
} |
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,54 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
Description: A function is triggered off an upload to a bucket. It uploads a resized image to another bucket. | ||
|
||
Globals: | ||
Function: | ||
Timeout: 20 | ||
|
||
Parameters: | ||
BucketNamePrefix: | ||
Type: String | ||
Default: sam-example | ||
|
||
Resources: | ||
ImageProcessorFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
CodeUri: . | ||
Handler: main | ||
Runtime: go1.x | ||
Tracing: Active | ||
Policies: | ||
- S3ReadPolicy: | ||
BucketName: !Sub "${BucketNamePrefix}-source-bucket" | ||
- S3CrudPolicy: | ||
BucketName: !Sub "${BucketNamePrefix}-dest-bucket" | ||
Environment: | ||
Variables: | ||
DestBucket: !Sub "${BucketNamePrefix}-dest-bucket" | ||
Events: | ||
ImageUpload: | ||
Type: S3 | ||
Properties: | ||
Bucket: !Ref SourceBucket | ||
Events: s3:ObjectCreated:* | ||
|
||
SourceBucket: | ||
Type: AWS::S3::Bucket | ||
Properties: | ||
BucketName: !Sub "${BucketNamePrefix}-source-bucket" | ||
|
||
DestBucket: | ||
Type: AWS::S3::Bucket | ||
Properties: | ||
BucketName: !Sub "${BucketNamePrefix}-dest-bucket" | ||
|
||
Outputs: | ||
SourceBucket: | ||
Description: "S3 Bucket name that will trigger a Lambda function upon new objects insertion" | ||
Value: !Ref SourceBucket | ||
DestBucket: | ||
Description: "S3 Bucket name that will store a resized image" | ||
Value: !Ref DestBucket | ||
|
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
Oops, something went wrong.