Created
March 31, 2019 19:49
-
-
Save praetoriansentry/2410a6d483f2dfa65128f2bbc6660e30 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"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/s3manager" | |
"github.com/google/uuid" | |
"log" | |
) | |
func Handler(request map[string]interface{}) (string, error) { | |
if len(request) != 240 { | |
log.Println("wrong number of items") | |
return fmt.Sprintf("Unexpected length of arguments: %d", len(request)), errors.New("Error: wrong number of args") | |
} | |
jsonBod, err := json.Marshal(request) | |
if err != nil { | |
log.Println(err.Error()) | |
return "can't create json response", errors.New("Error: can't create json response") | |
} | |
sess := session.Must(session.NewSession()) | |
uploader := s3manager.NewUploader(sess) | |
u := uuid.New() | |
key := fmt.Sprintf("responses/%s", u.String()) | |
_, ierr := uploader.Upload(&s3manager.UploadInput{ | |
Bucket: aws.String("hexaco"), | |
Key: aws.String(key), | |
Body: bytes.NewReader(jsonBod), | |
}) | |
if ierr != nil { | |
log.Printf("There was an issue uploading to s3: %s", ierr.Error()) | |
return "Unable to save response", errors.New("Error: cant save response") | |
} | |
return u.String(), nil | |
} | |
func main() { | |
lambda.Start(Handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment