Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ You can build serverless applications faster and further simplify your developme

[Read the Development Guide](DEVELOPMENT_GUIDE.rst) for in-depth information on how to start making changes.

[Join the SAM developers channel (#samdev) on Slack](https://awssamopensource.splashthat.com/) to collaborate with fellow community members and the AWS SAM team.
[Join the SAM developers channel (#samdev) on Slack](https://join.slack.com/t/awsdevelopers/shared_invite/enQtMzg3NTc5OTM2MzcxLTdjYTdhYWE3OTQyYTU4Njk1ZWY4Y2ZjYjBhMTUxNGYzNDg5MWQ1ZTc5MTRlOGY0OTI4NTdlZTMwNmI5YTgwOGM/) to collaborate with fellow community members and the AWS SAM team.
27 changes: 27 additions & 0 deletions examples/2016-10-31/image_resize_golang/README.md
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`.

72 changes: 72 additions & 0 deletions examples/2016-10-31/image_resize_golang/main.go
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)
}
54 changes: 54 additions & 0 deletions examples/2016-10-31/image_resize_golang/template.yaml
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