-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3_copy _channels
72 lines (57 loc) · 1.36 KB
/
s3_copy _channels
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
package main
import (
"flag"
"fmt"
"net/url"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var wg sync.WaitGroup
func copy(sess *session.Session, other string, c chan string, i int) {
defer wg.Done()
svc := s3.New(sess)
for {
key := <-c
if key != "" {
_, err := svc.CopyObject(&s3.CopyObjectInput{Bucket: aws.String(other),
CopySource: aws.String(url.PathEscape("taskawsbucket-hosting" + "/" + key)), Key: aws.String(key)})
fmt.Printf("Worker %v copying %s \n", i, key)
if err != nil {
fmt.Print("error")
}
} else {
break
}
}
}
func test(ch chan string) {
fmt.Print(<-ch)
}
func main() {
source := flag.String("s", "", "BUCKET NAME")
profile := flag.String("p", "default", "PROFILE NAME")
det := flag.String("d", "", "DESTINATION BUCKET")
sess, err := session.NewSessionWithOptions(session.Options{
Profile: *profile,
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
fmt.Print("error")
}
ch := make(chan string)
svc := s3.New(sess)
//get s3 items to resp
resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(*source)})
//creates 10 workers
for i := 0; i < 10; i++ {
wg.Add(1)
go copy(sess, *det, ch, i)
}
for _, item := range resp.Contents {
ch <- *item.Key
}
close(ch)
wg.Wait()
}