Created
December 1, 2020 16:26
-
-
Save mhart/98211344a1faab93fcdc8cc345b204c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func main() { | |
os.Chdir("/tmp") // Change working directory to /tmp so we can write files | |
if len(os.Args) > 1 && os.Args[1] == "--test" { | |
pdf2png() | |
} else { | |
lambda.Start(handler) | |
} | |
} | |
func handler(ctx context.Context, s3Event events.S3Event) ([]string, error) { | |
sess := session.Must(session.NewSession()) | |
bucket := s3Event.Records[0].S3.Bucket.Name | |
key := s3Event.Records[0].S3.Object.Key | |
if !strings.HasSuffix(strings.ToLower(key), ".pdf") { | |
return nil, fmt.Errorf("Key does not look like a PDF file: %s", key) | |
} | |
convertKey := "converted/" + strings.TrimPrefix(key[:len(key)-4], "upload/") // trim ".pdf" off the end | |
fmt.Println("Received S3 event, Bucket: ", bucket, ", Key: ", key, "Converting to: ", convertKey) | |
// Will download the S3 object locally to /tmp/input.pdf | |
if err := downloadPdf(sess, bucket, key); err != nil { | |
return nil, err | |
} | |
// Convert to PNGs | |
if err := pdf2png(); err != nil { | |
return nil, err | |
} | |
// Upload all PNGs from /tmp/output to our converted/ key prefix | |
return uploadPngs(sess, bucket, convertKey) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment