Skip to content

Instantly share code, notes, and snippets.

@theanalyst
Created January 10, 2017 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theanalyst/45732de392237ac3e01c26bc4ff9d19f to your computer and use it in GitHub Desktop.
Save theanalyst/45732de392237ac3e01c26bc4ff9d19f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"strings"
"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"
)
func main() {
bucket := aws.String("foobar")
key := aws.String("testobject")
logLevel := aws.LogDebugWithHTTPBody | aws.LogDebugWithRequestErrors | aws.LogDebugWithRequestRetries
// Configure to use Minio Server
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials("0555b35654ad1656d804", "h7GhxuBLTrlhVUyxSPUKUV8r/2EI4ngqJxD7iBdBYLhwluN30JaT3Q==", ""),
Endpoint: aws.String("localhost:8000/"),
Region: aws.String("us-east-1"),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(false),
LogLevel: &logLevel,
}
newSession := session.New(s3Config)
s3Client := s3.New(newSession)
var params *s3.ListBucketsInput
res, err := s3Client.ListBuckets(params)
if err != nil {
// Message from an error.
fmt.Println(err.Error())
//return
} else {
fmt.Println(res)
}
cparams := &s3.CreateBucketInput{
Bucket: bucket, // Required
}
// Create a new bucket using the CreateBucket call.
_, err = s3Client.CreateBucket(cparams)
if err != nil {
// Message from an error.
fmt.Println(err.Error())
return
}
// Upload a new object "testobject" with the string "Hello World!" to our "newbucket".
_, err = s3Client.PutObject(&s3.PutObjectInput{
Body: strings.NewReader("Hello from Minio!!"),
Bucket: bucket,
Key: key,
})
if err != nil {
fmt.Printf("Failed to upload data to %s/%s, %s\n", *bucket, *key, err.Error())
return
}
fmt.Printf("Successfully created bucket %s and uploaded data with key %s\n", *bucket, *key)
// Retrieve our "testobject" from our "newbucket" and store it locally in "testobject_local".
file, err := os.Create("testobject_local")
if err != nil {
fmt.Println("Failed to create file", err)
return
}
defer file.Close()
downloader := s3manager.NewDownloader(newSession)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: bucket,
Key: key,
})
if err != nil {
fmt.Println("Failed to download file", err)
return
}
fmt.Println("Downloaded file", file.Name(), numBytes, "bytes")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment