使用AWS S3时,此服务缺少端点配置。

huangapple go评论65阅读模式
英文:

Endpoint configuration missing for this service while using aws S3

问题

错误:在使用Golang与AWS S3时缺少端点配置

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云上无法正常工作。

英文:

Error : Endpoint configuration missing for this service while using aws S3 with 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),
		},
	},
)

What should be value of Endpoint, if we connecting to S3 from golang?
where can I find the endpoint in AWS S3?

I tried locally with S3 emulator, it's working fine, but it's not working with S3 aws cloud.

答案1

得分: 1

根据SDK文档中的定义,Endpoint是一个可选的端点URL(仅限主机名或完全限定URI),用于覆盖客户端的默认生成端点。将其设置为nil""的值以使用默认生成的端点。

这意味着你可以像这样指定端点:

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() {
	// 使用自定义的端点URL创建一个新的AWS会话
	sess := session.Must(session.NewSession(&aws.Config{
		Region:   aws.String("us-west-2"),
		Endpoint: aws.String("https://s3-us-west-2.amazonaws.com"),
	}))

	// 使用自定义端点创建一个Amazon S3客户端
	svc := s3.New(sess)

	// 列出S3账户中的所有存储桶
	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))
	}
}

以上代码展示了如何使用自定义的端点URL创建一个AWS会话,并使用该自定义端点创建一个Amazon S3客户端。然后,它列出了S3账户中的所有存储桶。

英文:

Definition of Endpoint as per the sdk documentation - 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

This essentially means you can specify the endpoint just like this

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))
	}
}

huangapple
  • 本文由 发表于 2023年4月17日 00:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定