How can I use the AWS SDK v2 for Go with DigitalOcean Spaces?

Viewed 371

I'm trying to use the AWS v2 SDK for Go to list all objects in a given bucket on DigitalOcean Spaces. Their documentation gives examples of how to use the v1 SDK to do this, but my app uses v2. I know I could technically use both, but I'd rather not if possible.

Here's what I've got so far:

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

This is the error I'm getting:

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

The error seems to indicate there's something wrong with my request but I don't know what.

0 Answers
Related