Created
June 15, 2019 12:43
-
-
Save xvzf/6f08d7ad22b23ca3b83710d7b75ea5b7 to your computer and use it in GitHub Desktop.
S3 go parallelize
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 ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"strings" | |
"sync" | |
"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" | |
) | |
const ( | |
endpoint = "http://localhost:30098" | |
) | |
func newBool() *bool { | |
b := true | |
return &b | |
} | |
func main() { | |
var wg sync.WaitGroup | |
// Credentials: &credentials.NewStaticCredentials("K279UGQBCW1RM3G1IITH", "s11DBCiqv9hnqoJ9drpEAQJkkBO2EP0Gv7u6MgLf", ""), | |
c := s3.New(session.New(&aws.Config{ | |
Credentials: credentials.NewStaticCredentials("K279UGQBCW1RM3G1IITH", "s11DBCiqv9hnqoJ9drpEAQJkkBO2EP0Gv7u6MgLf", ""), | |
Endpoint: aws.String("http://localhost:30098"), | |
Region: aws.String("us-east-1"), // Somehow this is needed | |
DisableSSL: aws.Bool(false), S3ForcePathStyle: aws.Bool(true), })) | |
_, err := c.CreateBucket(&s3.CreateBucketInput{ | |
Bucket: aws.String("hellofromgolang"), | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for k, v := range map[string]string{ | |
"key1": "content 1", | |
"key2": "content 2", | |
"key3": "content 3", | |
} { | |
wg.Add(1) | |
go func(key, content string) { | |
defer wg.Done() | |
_, err = c.PutObject(&s3.PutObjectInput{ | |
Bucket: aws.String("hellofromgolang"), | |
Key: aws.String(key), | |
Body: strings.NewReader(content), | |
ACL: aws.String("public-read"), | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
data, err := c.GetObject(&s3.GetObjectInput{ | |
Bucket: aws.String("hellofromgolang"), | |
Key: aws.String(key), | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
b, _ := ioutil.ReadAll(data.Body) | |
fmt.Println(string(b)) | |
}(k, v) | |
} | |
wg.Wait() | |
} |
thanks for the hint @sheldoncoupeheure. These are credentials for a temporary in memory s3 mockup service so no worries :)
okay, I didn't know that was possible 😄 I guess I learned something today
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey so I was searching for some credentials.NewStaticCredentials usage examples and I stumbled onto this gist; beware, your credentials are still visible (are they working ? idk)