首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用AWS、S3和goamz实现Golang中不支持的协议方案

用AWS、S3和goamz实现Golang中不支持的协议方案
EN

Stack Overflow用户
提问于 2017-01-02 23:15:46
回答 2查看 6.8K关注 0票数 3

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

运行以下代码时:

代码语言:javascript
运行
复制
    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 ""

创建ec2安全组时不支持的协议方案来看,原因似乎是一个不正确的区域,但事实并非如此,因为它是在init()中设置的。

任何想法都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-03 11:29:06

任何可能有同样问题的人。如果未设置桶名,则会出现错误。看起来很蠢,但我在码头里运行了这个,忘了告诉对接者环境变量是什么,它保留了桶名。因此,当我在我的终端上做echo $S3_BUCKET时,我得到了桶名,因此我认为一切都很好。不过,我并没有在码头集装箱里测试这个。愚弄我。

票数 2
EN

Stack Overflow用户

发布于 2022-11-11 15:51:17

在设置AWS配置以允许我指定AWS端点时,V2遇到了这个问题(在集成测试期间,我将服务指向本地堆栈)。

我通过使用自定义端点解析器来解析它。

代码语言:javascript
运行
复制
conf, err := config.LoadDefaultConfig(
        ctx,
        config.WithRegion(region),
        config.WithRetryMaxAttempts(3),
        config.WithEndpointResolver(aws.EndpointResolverFunc(func(_, region string) (aws.Endpoint, error) {
            if internal.AwsEndpoint != "" {
                return aws.Endpoint{
                    PartitionID:   "aws",
                    URL:           internal.AwsEndpoint,
                    SigningRegion: region,
                }, nil
            }
            // returning EndpointNotFoundError will allow the service to fallback to it's default resolution
            return aws.Endpoint{}, &aws.EndpointNotFoundError{}
        })),
    )
...
client := s3.NewFromConfig(conf, func(o *s3.Options) {
    o.UsePathStyle = true
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41434410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档