Last active
December 15, 2017 16:42
-
-
Save mettledrum/9f34915c038f87ecb5f289236df3bb49 to your computer and use it in GitHub Desktop.
go aws sdk -> s3rver docker; just morphed the README.md example a lil' bit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"time" | |
"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" | |
) | |
// Usage: | |
// # Upload myfile.txt to myBucket/myKey. Must complete within 10 minutes or will fail | |
// go run main.go -b mybucket -k myKey -d 10m < myFile.txt | |
func main() { | |
var bucket, key string | |
var timeout time.Duration | |
flag.StringVar(&bucket, "b", "", "Bucket name.") | |
flag.StringVar(&key, "k", "", "Object key name.") | |
flag.DurationVar(&timeout, "d", 0, "Upload timeout.") | |
flag.Parse() | |
// docker run -d --name s3server -p 8000:8000 scality/s3server:mem-latest | |
// The default access key is accessKey1 with a secret key of verySecretKey1. | |
sess := session.New(&aws.Config{ | |
Credentials: credentials.NewStaticCredentials("accessKey1", "verySecretKey1", ""), | |
Endpoint: aws.String("http://0.0.0.0:8000"), | |
Region: aws.String("us-east-1"), | |
S3ForcePathStyle: aws.Bool(true), | |
}) | |
svc := s3.New(sess) | |
_, err := svc.CreateBucket(&s3.CreateBucketInput{ | |
Bucket: aws.String(bucket), | |
}) | |
if err != nil { | |
panic(err) | |
} | |
// wedge context in so there's impatience | |
ctx := context.Background() | |
cancelFn := func() { | |
log.Print("¡context timed out!") | |
} | |
if timeout > 0 { | |
ctx, cancelFn = context.WithTimeout(ctx, timeout) | |
} | |
defer cancelFn() | |
_, err = svc.PutObjectWithContext(ctx, &s3.PutObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: aws.String(key), | |
Body: os.Stdin, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
getObjectOutput, err := svc.GetObject(&s3.GetObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: aws.String(key), | |
}) | |
if err != nil { | |
panic(err) | |
} | |
out := map[string]interface{}{} | |
err = json.NewDecoder(getObjectOutput.Body).Decode(&out) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("successfully uploaded file to %s/%s\n", bucket, key) | |
fmt.Printf("got it back too: %v\n", out) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"hi":"mom"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment