2

I am trying to connect my local MinIO instance running in docker. I have written a simple Go program to do the work. See following:

package main

import (
    "bytes"
    "context"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/credentials"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3"
    "github.com/aws/aws-sdk-go/service/s3/s3manager"
    "log"
)

func main() {

    endpoint := "localhost:9000"
    accessKeyID := "1mVyDeEcrRU7qQ7h"
    secretAccessKey := "vNi9SNfcHNxfRDETSvkeFLO210mzu7ee"
    useSSL := false

    awsSession := session.Must(session.NewSession(&aws.Config{
        Region: aws.String("ap-south-1"),
        Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
            AccessKeyID:     accessKeyID,
            SecretAccessKey: secretAccessKey,
            SessionToken:    "",
        }),
        Endpoint:   aws.String(endpoint),
        DisableSSL: aws.Bool(!useSSL),
    }))

    ctx := context.Background()

    file, err := downloadFileFromS3(ctx, awsSession, "mybucket", "data.txt")
    if err != nil {
        log.Fatal(err)
    }

    reader := bytes.NewReader(file)
    println(reader)
}

func downloadFileFromS3(ctx context.Context, sess *session.Session, bucketName string, fileName string) ([]byte, error) {
    downloader := s3manager.NewDownloader(sess)

    buff := &aws.WriteAtBuffer{}
    _, err := downloader.DownloadWithContext(ctx, buff,
        &s3.GetObjectInput{
            Bucket: aws.String(bucketName),
            Key:    aws.String(fileName),
        })
    if err != nil {
        return nil, err
    }

    return buff.Bytes(), nil
}

But this fails to do the job. Check error below:

GOROOT=/usr/local/go #gosetup
GOPATH=/Users/kuldeep/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/29/zkqqp9_d0fscymp5ph4rscl40000gp/T/GoLand/___2go_build_minio_client_example minio-client-example #gosetup
/private/var/folders/29/zkqqp9_d0fscymp5ph4rscl40000gp/T/GoLand/___2go_build_minio_client_example
2023/01/16 22:37:52 RequestError: send request failed
caused by: Get "http://mybucket.localhost:9000/data.txt": dial tcp: lookup mybucket.localhost: no such host

Process finished with the exit code 1

What should I do to make it work?

Kuldeep Yadav
  • 1,664
  • 5
  • 23
  • 41
  • 1
    Does this answer your question? [Use path-style amazon aws sdk go](https://stackoverflow.com/questions/57346922/use-path-style-amazon-aws-sdk-go) – Anon Coward Jan 16 '23 at 17:19
  • «mybucket.localhost: no such host» contains a crisp and precise statement of the problem. What have you tried to understand and fix it? – kostix Jan 16 '23 at 17:37
  • 1
    @kostix I added a record in `/etc/hosts` to redirect all requests for mybucket.localhost to localhost. But now I get error "NoSuchBucket". – Kuldeep Yadav Jan 17 '23 at 05:18
  • Also, I tried postman to hit a GET request to the specified url, but get NoSuchBucket error. Check curl `curl --location --request GET 'http://mybucket.localhost:9000/data.txt' \ --header 'X-Amz-Content-Sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855' \ --header 'X-Amz-Date: 20230116T152118Z' \ --header 'Authorization: AWS4-HMAC-SHA256 Credential=1mVyDeEcrRU7qQ7h/20230116/ap-south-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=051e3600ef367d03abdc5e224a9e395b4ed99158f6ed0c7f4fa78d0cb7b90695'` – Kuldeep Yadav Jan 17 '23 at 05:20
  • @AnonCoward yes the solution worked for me if I change the aws config object as mentioned in the solution. But I am using MinIO to write and run integration tests, so I am not comfortable to amend the production code for sake of running tests. The solution mentioned will be my last resort. – Kuldeep Yadav Jan 17 '23 at 05:23
  • That would require more setup. Subdomains are required on virtual host style buckets. You can't have subdomains on localhost. Further, minio doesn't support virtual host style buckets without MINIO_DOMAIN set, and proper CNAME setup for the subdomains. – Anon Coward Jan 17 '23 at 05:38
  • Similar question with same answer here: https://stackoverflow.com/q/74732461/901597 – Joe Bowbeer May 24 '23 at 00:28

0 Answers0