Go语言 使用aws S3时此服务缺少端点配置

h43kikqp  于 2023-04-18  发布在  Go
关注(0)|答案(1)|浏览(172)

错误:使用AWS S3与Golang时此服务的端点配置丢失

session, err := session.NewSessionWithOptions(
    session.Options{
        Profile: "default",
        Config: aws.Config{
            Credentials:      credentials.NewStaticCredentials("XXXXXXXXXXXXXXXX", "XXXXXXXXXXX", ""),
            Region:           aws.String(AWS_S3_REGION),
            Endpoint:         aws.String("XXXXXXX"),
// ---------^^^^^^^^
            DisableSSL:       aws.Bool(false),
            S3ForcePathStyle: aws.Bool(true),
        },
    },
)

如果我们从golang连接到S3,Endpoint的价值应该是什么?在AWS S3中哪里可以找到端点?
我在本地尝试了S3模拟器,它工作正常,但它不适用于S3 AWS云。

zz2j4svz

zz2j4svz1#

根据sdk文档定义端点- AWS Go SDK

// An optional endpoint URL (hostname only or fully qualified URI)
// that overrides the default generated endpoint for a client. Set this
// to `nil` or the value to `""` to use the default generated endpoint.
//
// Note: You must still provide a `Region` value when specifying an
// endpoint for a client.
Endpoint *string

这实际上意味着您可以像这样指定端点

package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
)

func main() {
    // Create a new AWS session with a custom endpoint URL
    sess := session.Must(session.NewSession(&aws.Config{
        Region:   aws.String("us-west-2"),
        Endpoint: aws.String("https://s3-us-west-2.amazonaws.com"),
    }))

    // Create an Amazon S3 client using the custom endpoint
    svc := s3.New(sess)

    // List all the buckets in the S3 account
    resp, err := svc.ListBuckets(nil)
    if err != nil {
        fmt.Println("Error listing buckets:", err)
        return
    }

    fmt.Println("Buckets:")
    for _, bucket := range resp.Buckets {
        fmt.Printf("* %s created on %s\n",
            aws.StringValue(bucket.Name), aws.TimeValue(bucket.CreationDate))
    }
}

相关问题