Created
February 8, 2021 09:39
-
-
Save vlastv/add9f6951b5fa0c6b88cb9a664573f33 to your computer and use it in GitHub Desktop.
Sample upload S3
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 ( | |
"bytes" | |
"context" | |
"crypto/rand" | |
"fmt" | |
"log" | |
"time" | |
"github.com/aws/aws-sdk-go-v2/aws" | |
"github.com/aws/aws-sdk-go-v2/credentials" | |
"github.com/aws/aws-sdk-go-v2/service/s3" | |
) | |
func main() { | |
bucket := aws.String("test") | |
size := 1024 * 1024 | |
cli := s3.New(s3.Options{ | |
EndpointResolver: s3.EndpointResolverFromURL("https://s3.selcdn.ru"), | |
Region: "ru-1a", | |
UsePathStyle: true, | |
Credentials: credentials.NewStaticCredentialsProvider("135815_test", "hH[4m*#,5s", ""), | |
Retryer: aws.NopRetryer{}, | |
}) | |
b := make([]byte, size) | |
i := 0 | |
for range time.Tick(1 * time.Second) { | |
i++ | |
key := fmt.Sprintf("%d.txt", i) | |
fmt.Println(key) | |
rand.Read(b) | |
for { | |
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Minute) | |
_, err := cli.PutObject(ctx, &s3.PutObjectInput{ | |
Bucket: bucket, | |
Key: aws.String(key), | |
Body: bytes.NewReader(b), | |
}) | |
cancel() | |
if err != nil { | |
log.Println(err) | |
time.Sleep(2 * time.Minute) | |
} | |
if err == nil { | |
break | |
} | |
} | |
out, err := cli.HeadObject(context.TODO(), &s3.HeadObjectInput{ | |
Bucket: bucket, | |
Key: aws.String(key), | |
}) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
if out.ContentLength != int64(size) { | |
log.Println(key, out.ContentLength, time.Now()) | |
} | |
if i == 20 { | |
break | |
} | |
} | |
} |
Author
vlastv
commented
Feb 8, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment