亚马逊 S3 由于没有此主机错误而无法调用 API。

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

Amazon S3 cannot call API because of no such host error

问题

我正在尝试使用GoLang构建一个Amazon S3客户端,但在进行API调用时遇到了问题。我收到一个错误,提示"no such host",但我确定我提供的凭据是正确的。

定义一个结构体来保存客户端

// 定义一个包含客户端本身和存储桶的Client结构体
type S3Client struct {
    S3clientObject s3.S3
    bucket string
}

// 初始化客户端
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{
    // 初始化会话
    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)
    }
    // 创建S3服务客户端
    svc := s3.New(sess)
    return *svc
}

到目前为止,我能够建立连接并使用ListBuckets功能接收到所有存储桶的列表(类似于https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.ListBuckets)。

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

// 从存储桶获取对象
func Get(client S3Client, key string) interface{} {
    // Go语言不支持"默认值",所以我使用了nil(与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

我无法弄清楚为什么会发生这种情况,因为我能够成功连接到存储桶,并使用ListBuckets列出它们,但在使用另一个API调用时,它无法找到主机。我的代码有什么问题吗?还有其他我忘记的配置吗?

非常感谢您的帮助和指导,因为我对使用GoLang和S3还比较新。

英文:

I am attempting to build an Amazon S3 client in GoLang but I am having trouble making API calls. I'm receiving an error that says "no such host" but I am positive the credentials I'm providing are correct.

Defining a struct to hold the client

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

Connecting to the bucket

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
}

At this point, I am able to establish a connection and use the ListBuckets functionality to receive a list of all the buckets (like this: https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#S3.ListBuckets)

When I try to call the GetObject API, it tells me it cannot find the host

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

This returns:

dial tcp: lookup "hostname": no such host

I cannot figure out why this is happening, because I am able to successfully make a connection to the bucket, and list them out using ListBuckets, but when using another API call, it fails to find the host. Is there something wrong with my code? Is there another configuration that I forgot about?

Any help or guidance is greatly appreciated as I'm somewhat new to using GoLang and S3.

答案1

得分: 4

显然问题出在存储桶名称上。我解决这个问题的方法是在创建存储桶时在名称前面加上“/”,然后问题就解决了。

英文:

Apparently the issue was with the bucket name. All I did to resolve this was put a "/" in front of the bucket name when creating it and it worked.

huangapple
  • 本文由 发表于 2021年6月8日 04:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/67878564.html
匿名

发表评论

匿名网友

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

确定