Skip to content

Commit

Permalink
add rule to event s3:putFile, destination files each folder by resolu…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
jechav committed Apr 23, 2018
1 parent a92b342 commit 360f336
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 75 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 6 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

- [serverless](https://serverless.com/blog/framework-example-golang-lambda-support/)

### Start

- update variables in config.json

### build
> Make sure you're in your ```${GOPATH}/src``` directory, then run:
Expand All @@ -19,34 +23,12 @@ make
sls deploy
```

### Use

Path: /upload
Method: POST
Paramethers:

```php
'file' => $file,
's3_store' => [
'key' => config('filesystems.disks.s3.key'),
'secret' => config('filesystems.disks.s3.secret'),
'bucket' => config('filesystems.disks.s3.bucket'),
'region' => config('filesystems.disks.s3.region'),
'path' => /img/,
'acl' => 'public_read',
'headers' => [
"Cache-Control" => "max-age=630700000000",
"Expires" => 2535321600, //year 2050
],
],
```

### Todo

[ ] - endpoint to upload image to s3
[ ] - - save image to s3
[x] - - save image to s3
[ ] - - save metadata (image, user, s3 destination credentials)
[x] - lambda function to resize image on s3
[ ] - lambda function save results to s3 destination storaged
[x] - lambda function save results to s3 destination storaged
[ ] - control and register user requests (API keys)

6 changes: 2 additions & 4 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"SOURCE_S3_BUCKET_NAME": "{unique upload bucket name}",
"DEST_S3_BUCKET_NAME": "{unique upload bucket name}",
"SOURCE_S3_BUCKET_NAME": "leviathan-o",
"DEST_S3_BUCKET_NAME": "leviathan-d"
}


61 changes: 15 additions & 46 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import (
"image/jpeg"
"strings"
"strconv"
"path"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
// "github.com/nfnt/resize"
//"gopkg.in/h2non/bimg.v1"
"github.com/nfnt/resize"

//"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws"
//"github.com/aws/aws-sdk-go/aws/awsutil"
//"github.com/aws/aws-sdk-go/aws/credentials"
//"github.com/aws/aws-sdk-go/service/s3"
//"github.com/aws/aws-sdk-go/aws/session"
"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"
Expand Down Expand Up @@ -47,7 +44,7 @@ var sizes = [17]string {

func Handler(ctx context.Context, s3Event events.S3Event) {

destBucket := "leviathan-det"
destBucket := getEnv("DEST_S3_BUCKET_NAME", "leviathan-det")
bucket := s3Event.Records[0].S3.Bucket.Name
item := s3Event.Records[0].S3.Object.Key

Expand Down Expand Up @@ -87,7 +84,7 @@ func Handler(ctx context.Context, s3Event events.S3Event) {
size := strings.Split(v, "x")
w, _ := strconv.ParseUint(size[0], 10, 64)
//h, _ := strconv.ParseUint(size[1], 10, 64)
fmt.Println("%d. resizing to %s => %T, %T", i, v, w, img)
fmt.Println("%d. resizing to %s => %T", i, v, w)

// resize to especific size using Nearest-neighbor interpolation
// and preserve aspect ratio
Expand All @@ -97,7 +94,9 @@ func Handler(ctx context.Context, s3Event events.S3Event) {
file := bytes.NewBuffer(buffer);
jpeg.Encode(file, m, nil)

filename := item + "resized_" + v + ".jpg"

_, filename := path.Split(item)
filename = "/" + v + "/" + filename

_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(destBucket),
Expand Down Expand Up @@ -127,43 +126,6 @@ func Handler(ctx context.Context, s3Event events.S3Event) {
*svc := s3.New(session.New(), cfg)
*/


/*
*
* // open "test.jpg"
* file, err := os.Open("test.jpg")
* if err != nil {
* log.Fatal(err)
* }
*
* // decode jpeg into image.Image
* img, err := jpeg.Decode(file)
* if err != nil {
* log.Fatal(err)
* }
* file.Close()
*
* for i, v := range sizes {
* //get the width and height
* size := strings.Split(v, "x")
* w, _ := strconv.ParseUint(size[0], 10, 64)
* //h, _ := strconv.ParseUint(size[1], 10, 64)
* fmt.Printf("%d. resizing to %s \n", i, v)
*
* // resize to especific size using Nearest-neighbor interpolation
* // and preserve aspect ratio
* m := resize.Resize(uint(w), 0, img, resize.NearestNeighbor)
*
* out, err := os.Create("test_resized_" + v + ".jpg")
* if err != nil {
* log.Fatal(err)
* }
* defer out.Close()
*
* // write new image to file
* jpeg.Encode(out, m, nil)
* }
*/
}

func main() {
Expand All @@ -174,3 +136,10 @@ func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
2 changes: 2 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ functions:
- s3:
bucket: ${self:custom.config.SOURCE_S3_BUCKET_NAME}
event: s3:ObjectCreated:*
rules:
- prefix: original/

resources:
Resources:
Expand Down

0 comments on commit 360f336

Please sign in to comment.