我正在尝试按照AWS lambda go和s3示例创建一个函数,该函数将从s3下载文件,调整文件大小并上传回s3。
当我构建函数并在本地计算机上运行它时,一切正常,当我以lambda形式运行时,我得到“”打开/tmp/images/users/original/5bba5c0f01e2f_dcadcc7139454bf6dba4cbd01a7c92f8.jpeg:无此类文件或目录” < / p>
这是我的代码
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
在本地运行时,一切正常,但是在lambda get上运行时,出现错误
答案 0 :(得分:0)
好的,所以这段代码先运行
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)
在程序当前所在的位置创建一个文件夹
然后执行此操作
item := "/tmp"/folder_name + item_name
fmt.Printf("Creating file %q \n", item)
file, err := os.OpenFile(item, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
exitErrorf("Unable to open file %q", err)
}
在本地运行时,它将使文件夹位于您所在的目录中
大概在某些时候,您也在/ tmp中创建了这些文件夹
在lambda中会发生相同的事情,但是lambda函数必须在/ tmp目录中不在的其他地方运行
因此找不到路径的一部分并且程序失败
要解决此问题,请
folder_name := "images/users/original/"
成为
folder_name := "/tmp/images/users/original/"
并删除“ / tmp” +行