johannesboyne / gofakes3

A simple fake AWS S3 object storage (used for local test-runs against AWS S3 APIs)
MIT License
361 stars 84 forks source link

Add example for aws-sdk-go-v2 #68

Closed johannesboyne closed 2 years ago

johannesboyne commented 2 years ago

Next to the explanation, one could add a simple test-case to start tracking whether the general v2 access works.

E.g., like this

package gofakes3_test

import (
    "bytes"
    "context"
    "crypto/tls"
    "net/http"
    "testing"

    "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"
    "github.com/johannesboyne/gofakes3"
    "github.com/johannesboyne/gofakes3/backend/s3mem"
)

func TestAWSV2Example(t *testing.T) {
    ts := newTestServer(t,
        withBackend(&backendWithUnimplementedPaging{s3mem.New()}),
        withFakerOptions(gofakes3.WithUnimplementedPageError()),
    )
    defer ts.Close()

    cfg, _ := config.LoadDefaultConfig(
        context.TODO(),
        config.WithSharedConfigProfile("test"),
        config.WithHTTPClient(
            &http.Client{
                Transport: &http.Transport{
                    TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
                },
            },
        ),
        config.WithEndpointResolver(
            aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
                return aws.Endpoint{URL: ts.server.URL}, nil
            }),
        ),
    )

    // Create an Amazon S3 service client
    client := s3.NewFromConfig(cfg, func(o *s3.Options) {
        o.UsePathStyle = true
    })

    ts.OKAll(client.PutObject(context.TODO(), &s3.PutObjectInput{
        Bucket: aws.String(defaultBucket),
        Key:    aws.String("ObjectKey"),
        Body:   bytes.NewReader([]byte(`{"test": "foo"}`)),
        Metadata: map[string]string{
            "Key": "MetadataValue",
        },
    }))
}
sonarcloud[bot] commented 2 years ago

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

johannesboyne commented 2 years ago

@shabbyrobe what's your view, is adding a v2 test-case helpful? It seems it takes a bit to complete - e.g., 2-3secs.