dryeyhe0266 2016-12-01 00:36
浏览 46
已采纳

Golang S3没有找到文件并且服务器在上载图像时出现了恐慌

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("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")

}

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

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

1条回答 默认 最新

  • douzhan1963 2016-12-01 00:46
    关注

    @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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路