-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.go
289 lines (228 loc) · 6.18 KB
/
test1.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"fmt"
"log"
"math"
"net/url"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
//part = 1
//BUCKET = "prefectresults"
// Merge_Array []*s3.UploadPartCopyOutput
client *s3.S3
)
const (
Threshold = (200 * 1024 * 1024)
Bucket = "prefectresults"
)
type object struct {
key string
start_bytes int64
end_bytes int64
}
var (
metadata []object
part = 1
Merged_Array []*s3.UploadPartCopyOutput
k = 1
ptr1Array []*s3.CompletedPart
)
func copy(uploadid string) {
part = 1
Merged_Array = nil
t := strconv.Itoa(k)
for _, met := range metadata {
//iterate in metadata array and create parts
to_copy := "bytes=" + strconv.Itoa(int(met.start_bytes)-1) + "-" + strconv.Itoa(int(met.end_bytes-1))
foo, err := client.UploadPartCopy(&s3.UploadPartCopyInput{
Bucket: aws.String(Bucket),
CopySource: aws.String(url.QueryEscape(Bucket + "/" + met.key)),
CopySourceRange: aws.String(to_copy),
PartNumber: aws.Int64(int64(part)),
Key: aws.String("newkey" + t),
UploadId: &uploadid,
})
if err != nil {
log.Println(Bucket + "/balraj/output/" + met.key)
log.Println(err, "copy upload")
}
Merged_Array = append(Merged_Array, foo)
part++
}
//log.Println("merged array", Merged_Array)
for i, cp := range Merged_Array {
temp := s3.CompletedPart{
ETag: cp.CopyPartResult.ETag,
PartNumber: aws.Int64(int64(i + 1)),
}
ptr1Array = append(ptr1Array, &temp)
}
complete_upload(uploadid, ptr1Array)
}
func create_multi_upload() *s3.CreateMultipartUploadOutput {
t := strconv.Itoa(k)
output, err := client.CreateMultipartUpload(&s3.CreateMultipartUploadInput{
Bucket: aws.String(Bucket),
// change key everytime this function called
Key: aws.String("newkey" + t),
})
if err != nil {
panic(err.Error())
}
return output
}
func complete_upload(UploadId string, parts []*s3.CompletedPart) {
//log.Println("parts", parts)
t := strconv.Itoa(k)
final, err := client.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
Bucket: aws.String(Bucket),
Key: aws.String("newkey" + t),
UploadId: aws.String(UploadId),
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: parts,
},
})
if err != nil {
log.Print(final)
log.Print(err)
}
log.Printf("uploaded key: %v", *final.Key)
ptr1Array = nil
k++
}
func upload_small_parts(m object) {
log.Printf("found part less than 5 mb at index")
//initiate this separate part to upload
t := strconv.Itoa(k)
output := create_multi_upload()
to_copy := "bytes=" + strconv.Itoa(int(m.start_bytes)-1) + "-" + strconv.Itoa(int(m.end_bytes-1))
foo, err := client.UploadPartCopy(&s3.UploadPartCopyInput{
Bucket: aws.String(Bucket),
CopySource: aws.String(url.QueryEscape(Bucket + "/" + m.key)),
CopySourceRange: aws.String(to_copy),
PartNumber: aws.Int64(1),
Key: aws.String("newkey" + t),
UploadId: output.UploadId,
})
if err != nil {
log.Println(Bucket + "/balraj/output/" + m.key)
log.Println(err, "copy upload")
}
temp := s3.CompletedPart{
ETag: foo.CopyPartResult.ETag,
PartNumber: aws.Int64(1),
}
complete_upload(*output.UploadId, []*s3.CompletedPart{&temp})
//after uploading remove the part from metadata array
fmt.Println("after removing small parts", metadata)
}
func Merge() {
FileSize := 0
list, err := client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(Bucket),
Prefix: aws.String("balraj/output/"),
})
times := 0
for _, obj := range list.Contents {
log.Println("taking...")
log.Println(*obj.Size, *obj.Key)
if int(*obj.Size)+FileSize < Threshold {
temp := object{
key: *obj.Key,
start_bytes: 1,
end_bytes: *obj.Size,
}
metadata = append(metadata, temp)
log.Println("added .....", temp)
FileSize += int(*obj.Size)
log.Println("****** -- ", FileSize)
if FileSize == Threshold {
log.Println("threshold reached....so initiating copy")
FileSize = 0
output := create_multi_upload()
copy(*output.UploadId)
log.Println(metadata)
//resetting the array
metadata = nil
}
} else {
log.Println("else exec")
remaining := *obj.Size
prevend := 0
end := 0
taken := 0 // 50 60 ----- 100
for {
maxAllowed := Threshold - FileSize
log.Println("Maxallowed is", maxAllowed)
if remaining > int64(maxAllowed) {
end = prevend + maxAllowed
} else {
end = int(*obj.Size)
}
temp := object{
key: *obj.Key,
start_bytes: int64(prevend + 1),
end_bytes: int64(end),
}
if temp.end_bytes-temp.start_bytes < (5 * 1024 * 1024) {
upload_small_parts(temp)
} else {
metadata = append(metadata, temp)
}
log.Println("added .....", temp)
taken = int(temp.end_bytes-temp.start_bytes) + 1
FileSize += taken
log.Println("****** -- ", FileSize)
if FileSize == Threshold {
log.Println("threshold reached....merged 200 mb")
FileSize = 0
//before uploading upload parts less than 5 mb
output := create_multi_upload()
log.Println("metadata ", metadata)
copy(*output.UploadId)
metadata = nil
log.Println("resetting metadata ")
log.Println("contens of metadata", metadata)
}
remaining = int64(math.Abs(float64(remaining) - float64(taken)))
if remaining == 0 {
log.Println("break")
break
}
log.Println("remaining", remaining)
//end = int(remaining)
prevend = end
log.Println("------")
times++
// if times > 5 {
// return
// }
}
}
if err != nil {
log.Println(err)
}
///at last iteration copy whatever that is in metadata..that will definitely will be under threshold
// output := create_multi_upload()
// copy(*output.UploadId)
}
log.Println("after for looop exec ", metadata)
}
func main() {
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String("us-west-2"),
},
Profile: "preprod",
SharedConfigState: session.SharedConfigEnable,
})
client = s3.New(sess)
if err != nil {
log.Println(err)
}
Merge()
}