Я пытаюсь следовать AWS lambda go и примерам s3, чтобы создать функцию, которая будет загружать файл из s3, изменять его размер и загружать обратно в s3.
Когда я собираю функцию и запускаю ее на локальном компьютере, все работает нормально, когда я запускаю ее как лямбду, я получаю "" open /tmp/images/users/original/5bba5c0f01e2f_dcadcc7139454bf6dba4cbd01a7c92f8.jpeg: нет такого файла иликаталог "
Вот мой код
package main
import (
"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/aws/aws-lambda-go/lambda"
"github.com/nfnt/resize"
"image/jpeg"
"fmt"
"os"
)
func HandleRequest() (string, error) {
return runResize("5bba5c0f01e2f_dcadcc7139454bf6dba4cbd01a7c92f8.jpeg"), nil
}
func main() {
lambda.Start(HandleRequest)
}
func runResize(item_name string) {
bucket := "bucket"
folder_name := "images/users/original/"
folder_name_220x220 := "images/users/220x220/"
folder_name_250x250 := "images/users/250x250/"
folder_name_500x500 := "images/users/500x500/"
folder_name_large := "images/users/large/"
createFolder(folder_name)
createFolder(folder_name_220x220)
createFolder(folder_name_250x250)
createFolder(folder_name_500x500)
createFolder(folder_name_large)
item := folder_name + item_name
fmt.Printf("Creating file %q \n", item)
file, err := os.Create(item)
if err != nil {
exitErrorf("Unable to open file %q", err)
}
defer file.Close()
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
exitErrorf("Unable to download item %q, %v", item, err)
}
if err != nil {
exitErrorf("Unable to download item %q, %v", item, err)
}
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
exitErrorf("Unable to download item %q, %v", item, err)
}
file.Close()
m := resize.Thumbnail(1500, 1500, img, resize.Lanczos3)
out, err := os.OpenFile(folder_name_large + item_name, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
exitErrorf("Unable to resize item %q, %v", item, err)
}
defer out.Close()
jpeg.Encode(out, m, nil)
m = resize.Thumbnail(500, 500, img, resize.Lanczos3)
out, err = os.OpenFile(folder_name_500x500 + item_name, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
exitErrorf("Unable to resize item %q, %v", item, err)
}
defer out.Close()
jpeg.Encode(out, m, nil)
m = resize.Thumbnail(250, 250, img, resize.Lanczos3)
out, err = os.OpenFile(folder_name_250x250 + item_name, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
exitErrorf("Unable to resize item %q, %v", item, err)
}
defer out.Close()
jpeg.Encode(out, m, nil)
m = resize.Thumbnail(220, 220, img, resize.Lanczos3)
out, err = os.OpenFile(folder_name_220x220 + item_name, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
exitErrorf("Unable to resize item %q, %v", item, err)
}
defer out.Close()
jpeg.Encode(out, m, nil)
uploadFile(folder_name_220x220 + item_name, bucket)
uploadFile(folder_name_250x250 + item_name, bucket)
uploadFile(folder_name_500x500 + item_name, bucket)
uploadFile(folder_name_large + item_name, bucket)
fmt.Println("Processed", file.Name(), numBytes, "bytes")
}
func createFolder(folder_name string) {
if _, err := os.Stat(folder_name); os.IsNotExist(err) {
os.MkdirAll(folder_name, os.ModePerm)
fmt.Printf("Folder created %q \n", folder_name)
}
}
func uploadFile(filename string, bucket string){
file, err := os.Open(filename)
if err != nil {
exitErrorf("Unable to open file %q, %v", err)
}
defer file.Close()
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
uploader := s3manager.NewUploader(sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
Body: file,
ACL: aws.String("public-read"),
})
if err != nil {
// Print the error and exit.
exitErrorf("Unable to upload %q to %q, %v", filename, bucket, err)
}
fmt.Printf("Successfully uploaded %q to %q\n", filename, bucket)
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
Вот как я строю
GOOS=linux go build -v -ldflags '-d -s -w' -a -tags netgo -installsuffix netgo -o resize && zip deployment.zip resize
Когда я работаю локально, все работает нормально, но при работе на лямбдаполучить, я получаю ошибку