forked from thenamehasbeentake/s3_multipart_example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
174 lines (156 loc) · 4.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bytes"
"fmt"
"io"
"math/rand"
"os"
"runtime"
"sync"
"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"
)
func main() {
svc := s3.New(session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("b2345678901234567890", "b234567890123456789012345678901234567890", ""),
Endpoint: aws.String("http://127.0.0.1:8000"),
//Endpoint: aws.String("http://127.0.0.1:8100"),
Region: aws.String("us-east-1"),
S3ForcePathStyle: aws.Bool(true),
})))
objectNameSuffix := ""
if len(os.Args) > 2 {
objectNameSuffix = os.Args[2]
}
//err := RetryUpload(svc, b01b-000000000000", "test-key"+objectNameSuffix)
var bucketName string
if len(os.Args) > 1 {
bucketName = os.Args[1]
} else {
bucketName = "test-bucketname"
}
err := RetryUpload(svc, bucketName, "test-key"+objectNameSuffix)
if err != nil {
fmt.Println(err.Error())
}
}
func RetryUpload(svc *s3.S3, bucket, key string) error {
var (
byteSize int = 101 * 1024 * 1024
completedParts []*s3.CompletedPart
)
buffer := generateRandomBytes(byteSize)
fmt.Println("start CreateMultipartUpload")
resp, err := svc.CreateMultipartUpload(&s3.CreateMultipartUploadInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
fmt.Println("failed to CreateMultipartUpload ")
return err
}
fmt.Println("success to CreateMultipartUpload")
fmt.Println("start UploadPart PartNumber 1")
// first update 8 MB
fileBytes := buffer[0 : 8*1024*1024]
uploadResult, err := svc.UploadPart(&s3.UploadPartInput{
Body: bytes.NewReader(fileBytes),
Bucket: &bucket,
Key: &key,
PartNumber: aws.Int64(int64(1)),
UploadId: &*resp.UploadId,
ContentLength: aws.Int64(int64(len(fileBytes))),
})
if err != nil {
fmt.Println("failed to UploadPart PartNumber 1")
return err
}
etag := *uploadResult.ETag
// fmt.Println("etag: %s\n", etag)
completedParts = append(completedParts, &s3.CompletedPart{
ETag: &etag,
PartNumber: aws.Int64(int64(1)),
})
fmt.Println("success to UploadPart PartNumber 1")
fileBytes2 := buffer[1*1024*1024:]
// dd if=/dev/urandom of=/tmp/test1.txt bs=1M count=20
f1, err := os.Open("/tmp/test1.txt")
if err != nil {
fmt.Println("failed to CompleteMultipartUpload")
return err
}
defer f1.Close()
// dd if=/dev/urandom of=/tmp/test2.txt bs=1M count=30
f2, err := os.Open("/tmp/test2.txt")
if err != nil {
fmt.Println("failed to CompleteMultipartUpload")
return err
}
defer f2.Close()
var wg sync.WaitGroup
wg.Add(1)
var f func(bytess io.ReadSeeker) = func(bytess io.ReadSeeker) {
var buf = make([]byte, 64)
var stk = buf[:runtime.Stack(buf, false)]
fmt.Println("start UploadPart PartNumber 2, goroutine id " + string(stk))
// second part
uploadResult2, err := svc.UploadPart(&s3.UploadPartInput{
Body: bytess,
Bucket: &bucket,
Key: &key,
PartNumber: aws.Int64(int64(2)),
UploadId: &*resp.UploadId,
ContentLength: aws.Int64(int64(100 * 1024 * 1024)),
})
if err != nil {
fmt.Println("failed to UploadPart PartNumber 2, goroutine id " + string(stk) + err.Error())
return
}
fmt.Println("success to UploadPart PartNumber 2, now append, goroutine id " + string(stk))
completedParts = append(completedParts, &s3.CompletedPart{
ETag: &*uploadResult2.ETag,
PartNumber: aws.Int64(int64(2)),
})
wg.Done()
}
go f(f1)
go f(f2)
go f(bytes.NewReader(fileBytes2))
wg.Wait()
fmt.Println("CompleteMultipartUpload start")
_, err = svc.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
Bucket: &bucket,
Key: &key,
UploadId: &*resp.UploadId,
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: completedParts,
},
})
if err != nil {
fmt.Println("failed to CompleteMultipartUpload")
return err
}
fmt.Println("CompleteMultipartUpload success ")
panic("success upload")
}
func generateRandomBytes(size int) []byte {
rndSrc := rand.NewSource(time.Now().UnixNano())
rng := rand.New(rndSrc)
random := make([]byte, size)
var (
err error
n = 0
retry = 3
i = 0
)
for ; i < retry && n != size; i++ {
n, err = io.ReadFull(rng, random)
}
if err != nil {
return nil
}
return random
}