dqjmq28248 2016-03-25 17:04
浏览 214
已采纳

使用s3manager golang上传文件

I'm trying to download a few files from my website and upload them to amazons3.

Here is my code:

import (
    "log"
    "net/http"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3/s3manager"
)

func main() {
    // Make a GET request to an image to be downloaded and uploaded to s3
    resp, err := http.Get("http://my-site.com/image.jpg")
    if err != nil {
        log.Fatal(err)
    }

    // Get the AWS credentials
    creds := credentials.NewSharedCredentials("/Users/name/.aws/credentials", "default")

    // Set up config
    config := &aws.Config{
        Region:      aws.String("US Standard"),
        Credentials: creds,
    }

    // Set up a new AWS session
    sess := session.New(config)

    // Set up a new s3manager client
    uploader := s3manager.NewUploader(sess)

    result, err := uploader.Upload(&s3manager.UploadInput{
        Body:   resp.Body,
        Bucket: aws.String("mimi-movies"),
        Key:    aws.String("mimi.jpg"),
    })
    if err != nil {
        log.Fatalln("Failed to upload", err)
    }

    log.Println("Successfully uploaded to", result.Location)
}

I keep getting a run time error of a nil pointer dereference the error doesn't help much but for some reason I think it might have to do with ssl but here is the entire error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x236ffe]

goroutine 1 [running]:
panic(0x4743c0, 0xc82000a0e0)
    /usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
github.com/aws/aws-sdk-go/service/s3.validateSSERequiresSSL(0xc820066380)
    /Users/name/work/src/github.com/aws/aws-sdk-go/service/s3/sse.go:15 +0x17e
github.com/aws/aws-sdk-go/aws/request.(*HandlerList).Run(0xc820066478, 0xc820066380)
    /Users/name/work/src/github.com/aws/aws-sdk-go/aws/request/handlers.go:136 +0xc3
github.com/aws/aws-sdk-go/aws/request.(*Request).Build(0xc820066380, 0x0, 0x0)
    /Users/name/work/src/github.com/aws/aws-sdk-go/aws/request/request.go:189 +0x69
github.com/aws/aws-sdk-go/aws/request.(*Request).Sign(0xc820066380, 0x0, 0x0)
    /Users/name/work/src/github.com/aws/aws-sdk-go/aws/request/request.go:210 +0x31
github.com/aws/aws-sdk-go/aws/request.(*Request).Send(0xc820066380, 0x0, 0x0)
    /Users/name/work/src/github.com/aws/aws-sdk-go/aws/request/request.go:261 +0x6f4
github.com/aws/aws-sdk-go/service/s3/s3manager.(*uploader).singlePart(0xc8201520f0, 0x8bb980, 0xc82014a030, 0xc82000a140, 0x0, 0x0)
    /Users/name/work/src/github.com/aws/aws-sdk-go/service/s3/s3manager/upload.go:449 +0x1fc
github.com/aws/aws-sdk-go/service/s3/s3manager.(*uploader).upload(0xc8201520f0, 0xc8201520f0, 0x0, 0x0)
    /Users/name/work/src/github.com/aws/aws-sdk-go/service/s3/s3manager/upload.go:355 +0x563
github.com/aws/aws-sdk-go/service/s3/s3manager.Uploader.Upload(0x500000, 0x5, 0x0, 0x2710, 0xe00240, 0xc820156018, 0xc82016e000, 0x0, 0x0, 0x0, ...)
    /Users/name/work/src/github.com/aws/aws-sdk-go/service/s3/s3manager/upload.go:329 +0x15a
main.main()
    /Users/name/work/src/gitlab.com/user/project/test/main.go:39 +0x59f
exit status 2
  • 写回答

3条回答 默认 最新

  • dongman2721 2016-03-25 20:01
    关注

    There is a bug on the aws-go-sdk but the solution is to change the region to "us-east-1"

    // Set up config
    config := &aws.Config{
        Region:      aws.String("us-east-1"),
        Credentials: creds,
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀