Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minio example #244

Merged
merged 1 commit into from Dec 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions examples/Minio.md
Expand Up @@ -12,7 +12,7 @@ options := &dockertest.RunOptions{
Tag: "latest",
Cmd: []string{"server", "/data"},
PortBindings: map[dc.Port][]dc.PortBinding{
"9000": []dc.PortBinding{{HostPort: "9000"}},
"9000/tcp": []dc.PortBinding{{HostPort: "9000"}},
},
Env: []string{"MINIO_ACCESS_KEY=MYACCESSKEY", "MINIO_SECRET_KEY=MYSECRETKEY"},
}
Expand All @@ -23,20 +23,43 @@ if err != nil {
}

endpoint := fmt.Sprintf("localhost:%s", resource.GetPort("9000/tcp"))
// or you could use the following, because we mapped the port 9000 to the port 9000 on the host
// endpoint := "localhost:9000"

// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
// the minio client does not do service discovery for you (i.e. it does not check if connection can be established), so we have to use the health check
if err := pool.Retry(func() error {
minioClient, err := minio.New(endpoint, "MYACCESSKEY", "MYSECRETKEY", true)
url := fmt.Sprintf("http://%s/minio/health/live", endpoint)
resp, err := http.Get(url)
if err != nil {
log.Println("Failed to create minio client:", err)
return err
}
log.Printf("%#v\n", minioClient) // minioClient is now set up
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code not OK")
}
return nil
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}

// now we can instantiate minio client
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4("MYACCESSKEY", "MYSECRETKEY", ""),
Secure: false,
})
if err != nil {
log.Println("Failed to create minio client:", err)
return err
}
log.Printf("%#v\n", minioClient) // minioClient is now set up

// now we can use the client, for example, to list the buckets
buckets, err := minioClient.ListBuckets(context.Background())
if err != nil {
log.Fatalf("error while listing buckets: %v", err)
}
fmt.Printf("buckets: %+v", buckets)

// When you're done, kill and remove the container
if err = pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
Expand Down