英文:
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))
	}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论