Amazon S3无法调用API,因为没有这样的主机错误



我正试图在GoLang中构建AmazonS3客户端,但我在调用API时遇到了问题。我收到一个错误,上面写着";没有这样的主机";但我确信我提供的凭据是正确的。

定义一个结构来容纳客户端

// the Client struct holding the client itself as  well as the bucket.
type S3Client struct {
S3clientObject s3.S3
bucket string
}
// Initialize the client
func CreateS3Client() S3Client{
S3clientCreate := S3Client{S3clientObject: Connect(), bucket: GetS3Bucket()}
if (!CheckBuckets(S3clientCreate)) {
exitErrorf("Bucket does not exist, try again.")
}
return S3clientCreate
}

连接到存储桶

func Connect() s3.S3{
// Initialize a session

sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("myCredentials", "myCreds", ""),
Endpoint:    aws.String("myDomain"),
Region:      aws.String("myRegion"),
},
)
if err != nil {
exitErrorf("Unable to use credentials, %v", err)
}
// Create S3 service client
svc := s3.New(sess)
return *svc
}

在这一点上,我能够建立一个连接,并使用ListBuckets功能来接收所有bucket的列表(如下所示:https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.ListBuckets)

当我试图调用GetObject API时,它会告诉我找不到主机

// Gets an object from the bucket
func Get(client S3Client, key string) interface{} {

// golang does not support "default values" so I used a nil (same as null)
if (key == "") {
return nil
}
svc := client.S3clientObject
input := &s3.GetObjectInput{
Bucket: aws.String("myBucket"),
Key: aws.String("myPathKey"),
}
result, err := svc.GetObject(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchKey:
fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error())
case s3.ErrCodeInvalidObjectState:
fmt.Println(s3.ErrCodeInvalidObjectState, aerr.Error())
default:
fmt.Println(aerr.Error())  
}
} else {
fmt.Println(err.Error())
}
}
return result
}

返回:

dial tcp: lookup "hostname": no such host

我不知道为什么会发生这种情况,因为我可以成功地连接到bucket,并使用ListBuckets列出它们,但当使用另一个API调用时,它无法找到主机。我的代码有问题吗?还有其他我忘记的配置吗?

非常感谢任何帮助或指导,因为我对使用GoLang和S3有些陌生。

显然问题出在bucket名称上。我所做的一切都是为了解决这个问题/"在创建bucket时,它位于bucket名称前面,并且它起作用了。

最新更新