Golang在获取S3文件时找不到文件,并在上传图像时导致服务器崩溃。

huangapple go评论89阅读模式
英文:

Golang s3 getting no file found and server panic on uploading image

问题

你好,首先我是新手使用Golang,并尝试将照片上传到我的S3存储桶,但每次尝试时都会出现错误。我得到的错误是:

打开文件错误:打开55fc14631c00004800082775.jpeg时出错:没有该文件或目录2016/11/30 19:28:10 http: 在[::1]:58502上服务时发生恐慌:运行时错误:无效的内存地址或空指针解引用
goroutine 5 [running]

我的存储桶权限已设置为对所有人可读可写,所以问题可能出在这段代码中。我正在按照这个教程进行操作:https://medium.com/@questhenkart/s3-image-uploads-via-aws-sdk-with-golang-63422857c548#.ahda1pgly,并尝试上传一个公共图片,地址在这里:http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/55fc14631c00004800082775.jpeg

我的当前代码如下:

func UploadProfile(w http.ResponseWriter) {
    creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key, token)
    _, err := creds.Get()
    if err != nil {
        fmt.Printf("bad credentials: %s", err)
    }

    config := &aws.Config{
        Region:          aws.String("us-west-1"),
        Endpoint:        aws.String("s3.amazonaws.com"),
        S3ForcePathStyle: aws.Bool(true),
        Credentials:     creds,
    }

    svc := s3.New(session.New(), config)
    file, err := os.Open("55fc14631c00004800082775.jpeg")
    if err != nil {
        fmt.Printf("err opening file: %s", err)
    }

    defer file.Close()

    fileInfo, _ := file.Stat()
    var size int64 = fileInfo.Size()
    buffer := make([]byte, size)

    file.Read(buffer)
    fileBytes := bytes.NewReader(buffer)
    fileType := http.DetectContentType(buffer)

    path := "http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/" + file.Name()
    params := &s3.PutObjectInput{
        Bucket:        aws.String("MyBucket"),
        Key:           aws.String(path),
        Body:          fileBytes,
        ContentLength: aws.Int64(size),
        ContentType:   aws.String(fileType),
    }
    resp, err := svc.PutObject(params)
    if err != nil {
        fmt.Printf("bad response: %s", err)
    }
    fmt.Printf("response %s", awsutil.StringValue(resp))

    fmt.Fprintf(w, "done")
}

我不知道为什么这不起作用,因为我的凭据和存储桶都是正确的。此外,上面的图片地址对应的图片是有效的,所以不知道为什么文件是空的。

<img height="200" width="200" src="http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/55fc14631c00004800082775.jpeg"/>

以上是要翻译的内容。

英文:

Hello first of i am new to Golang and am trying to upload a photo to my s3 Bucket however I am getting an error every time I try, the error I get is this

err opening file: open 55fc14631c00004800082775.jpeg: no such file or directory2016/11/30 19:28:10 http: panic serving [::1]:58502: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]

My buckets permission are set to read and write for everyone so it must be something in this code is that wrong

and I am following this tutorial https://medium.com/@questhenkart/s3-image-uploads-via-aws-sdk-with-golang-63422857c548#.ahda1pgly and I am trying to upload a public image found here http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/55fc14631c00004800082775.jpeg

my current code is this below

func UploadProfile(w http.ResponseWriter) {
creds := credentials.NewStaticCredentials(aws_access_key_id, aws_secret_access_key,token)
_, err := creds.Get()
if err != nil {
fmt.Printf(&quot;bad credentials: %s&quot;, err)
}
config := &amp;aws.Config{
Region          :aws.String(&quot;us-west-1&quot;),
Endpoint        :aws.String(&quot;s3.amazonaws.com&quot;),
S3ForcePathStyle:aws.Bool(true),
Credentials     :creds,
}
svc := s3.New(session.New(), config)
file, err := os.Open(&quot;55fc14631c00004800082775.jpeg&quot;)
if err != nil {
fmt.Printf(&quot;err opening file: %s&quot;, err)
}
defer file.Close()
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
file.Read(buffer)
fileBytes := bytes.NewReader(buffer)
fileType := http.DetectContentType(buffer)
path := &quot;http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/&quot; + file.Name()
params := &amp;s3.PutObjectInput{
Bucket: aws.String(&quot;MyBucket&quot;),
Key: aws.String(path),
Body: fileBytes,
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
}
resp, err := svc.PutObject(params)
if err != nil {
fmt.Printf(&quot;bad response: %s&quot;, err)
}
fmt.Printf(&quot;response %s&quot;, awsutil.StringValue(resp))
fmt.Fprintf(w,&quot;done&quot;)
}

I do not know why this is not working as my Credentials and buckets are correct . In addition the IMG address works for the image above so do not know why the file is nil

&lt;img height=&quot;200&quot; width=&quot;200&quot; src=&quot;http://img.huffingtonpost.com/asset/,scalefit_950_800_noupscale/55fc14631c00004800082775.jpeg&quot;/&gt;

答案1

得分: 2

@user1591668,

你的错误是“err opening file: open 55fc14631c00004800082775.jpeg: no such file or directory”。听起来与AWS的SDK无关。

请检查你的二进制文件是否实际上在与图像文件相同的目录中(“工作目录”),或者为了测试这个假设,你可以尝试使用图像的绝对路径。

祝好,
Dennis

英文:

@user1591668,

Your error is "err opening file: open 55fc14631c00004800082775.jpeg: no such file or directory". Sounds like not related to AWS's SDK.

Check that your binary actually starts in the same directory where your image file is ("working directory") or, to test this assumption, you can try with an absolute path to the image.

Cheers,
Dennis

huangapple
  • 本文由 发表于 2016年12月1日 08:36:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/40900722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定