尝试使用Go(lang)中的goamz/s3包将文件字节上传到S3 AWS。

我的代码是:

var (
    awsAuth aws.Auth
    region aws.Region
    connection s3.S3
    bucket *s3.Bucket
)
func init() {

    // Set up the AWS S3 Connection config.
    awsAuth = aws.Auth{
        AccessKey: os.Getenv("ACCESS_KEY"), // change this to yours
        SecretKey: os.Getenv("SECRET_KEY"),
    }
    fmt.Println("AWS: ", awsAuth)
    region := aws.EUWest
    connection := s3.New(awsAuth, region)

    bucket = connection.Bucket(os.Getenv("S3_BUCKET")) // change this your bucket name
}
func uploadToS3(filename string, byteArray *[]byte) error {

    var err error
    //should check if file already uploaded, encrypted by this password
    fmt.Printf("[uploadToS3] starting upload of %s\n", filename)

    err = bucket.Put(filename, *byteArray, "text/plain; charset=utf-8", s3.PublicRead)
    if err != nil {
        fmt.Printf("[uploadToS3] error uploading: %s\n", err)
        return err
    } else {
        fmt.Printf("[uploadToS3] done uploading %s\n", filename)
        return nil
    }

    return nil // redundancy
}

我得到错误
[uploadToS3] error uploading: Put /filename: unsupported protocol scheme ""
从这里读取:unsupported protocol scheme while creating ec2 Security group它认为这是因为该区域不正确,但是如您所见,我正在init()函数中进行设置,所以我不知道它还有什么可能

任何想法表示赞赏

最佳答案

对于可能有相同问题的任何人。如果未设置存储桶名称,则会收到错误消息。看起来很愚蠢,但是我在docker内部运行它,却忘了告诉docker保存存储桶名称的环境变量是什么。因此,当我在终端上执行echo $S3_BUCKET时,我得到了存储桶名称,因此认为一切都很好。但是我没有在docker容器中进行测试。愚弄我

10-07 19:27
查看更多