如何将AWS SDK v2 for Go与DigitalOcean Spaces配合使用?

eyh26e7m  于 2022-12-07  发布在  Go
关注(0)|答案(1)|浏览(224)

我正在尝试使用AWS v2 SDK for Go列出DigitalOcean Spaces上给定桶中的所有对象。他们的文档gives examples说明了如何使用v1 SDK来完成此操作,但我的应用使用v2。我知道从技术上讲,我可以同时使用这两种工具,但如果可能的话,我宁愿不使用。
这是我目前得到的结果:

package main

import (
    "context"
    "fmt"
    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/s3"
)

func main() {

    customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
        return aws.Endpoint{
            URL: "https://sfo2.digitaloceanspaces.com",
        }, nil
    })
    cfg, err := config.LoadDefaultConfig(
        context.TODO(),
        config.WithRegion("us-east-1"),
        config.WithEndpointResolverWithOptions(customResolver),
        config.WithCredentialsProvider(aws.AnonymousCredentials{}),
    )
    if err != nil {
        fmt.Println(err)
    }

    s3Client := s3.NewFromConfig(cfg)

    var continuationToken *string
    continuationToken = nil

    for {
        output, err := s3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
            Bucket:            aws.String("stats"),
            ContinuationToken: continuationToken},
        )
        if err != nil {
            fmt.Println(err)
        }

        for _, obj := range output.Contents {
            fmt.Println(obj)
        }

        if output.IsTruncated == false {
            break
        }

        continuationToken = output.ContinuationToken
    }
}

这是我得到的错误:

operation error S3: ListObjectsV2, https response error StatusCode: 400, RequestID: tx0000000000000051339d4-00620701db-2174fe1c-sfo2a, HostID: 2174fe1c-sfo2a-sfo, api error InvalidArgument: UnknownError

这个错误似乎表明我的请求有问题,但我不知道是什么问题。

6qftjkof

6qftjkof1#

对于分页,我认为您需要通过这样的分页函数来完成

// Create the Paginator for the ListObjectsV2 operation.
    p := s3.NewListObjectsV2Paginator(client, params, func(o *s3.ListObjectsV2PaginatorOptions) {
        if v := int32(maxKeys); v != 0 {
            o.Limit = v
        }
    })

下面是一个完整的工作示例,我正在使用它从数字海洋空间桶中读取数据

package s3

import (
    "context"
    "os"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/credentials"
    "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
    "github.com/aws/aws-sdk-go-v2/service/s3"
)

func read(ctx context.Context) error {
    // Define the parameters for the session you want to create.

    spacesKey := os.Getenv("SPACES_KEY")
    spacesSecret := os.Getenv("SPACES_SECRET")

    creds := credentials.NewStaticCredentialsProvider(spacesKey, spacesSecret, "")

    customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
        return aws.Endpoint{
            URL: "https://sfo3.digitaloceanspaces.com",
        }, nil
    })
    cfg, err := config.LoadDefaultConfig(ctx,
        config.WithRegion("us-east-1"),
        config.WithCredentialsProvider(creds),
        config.WithEndpointResolverWithOptions(customResolver))
    if err != nil {
        return err
    }
    // Create an Amazon S3 service client
    awsS3Client := s3.NewFromConfig(cfg)
    input := &s3.GetObjectInput{
        Bucket: aws.String("zeus-fyi"),
        Key:    aws.String("test.txt"),
    }
    downloader := manager.NewDownloader(awsS3Client)
    newFile, err := os.Create("./local-test.txt")
    if err != nil {
        return err
    }
    defer newFile.Close()
    _, err = downloader.Download(ctx, newFile, input)
    if err != nil {
        return err
    }
    return err
}

相关问题