gpt4 book ai didi

amazon-web-services - 打开没有这样的文件或目录

转载 作者:IT王子 更新时间:2023-10-29 02:20:15 24 4
gpt4 key购买 nike

我正在尝试按照 AWS lambda go 和 s3 示例创建一个函数,该函数将从 s3 下载文件并调整其大小并上传回 s3。

当我构建函数并在本地机器上运行它时,一切正常,当我以 lambda 运行它时,我得到“”open/tmp/images/users/original/5bba5c0f01e2f_dcadcc7139454bf6dba4cbd01a7c92f8.jpeg: no such file or directory”

这是我的代码

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 上运行时,我得到错误

最佳答案

好的,所以这段代码先运行

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"+ 行

关于amazon-web-services - 打开没有这样的文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52828413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com