forked from awsdocs/aws-doc-sdk-examples
/
UploadDirectory.go
142 lines (119 loc) · 3.96 KB
/
UploadDirectory.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
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
// snippet-start:[s3.go.upload_directory]
package main
// snippet-start:[s3.go.upload_directory.imports]
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
// snippet-end:[s3.go.upload_directory.imports]
// DirectoryIterator represents an iterator of a specified directory
type DirectoryIterator struct {
filePaths []string
bucket string
next struct {
path string
f *os.File
}
err error
}
// const exitError = 1
// UploadDirectory uploads the files in a directory to a bucket
// Inputs:
// sess is the current session, which provides configuration for the SDK's service clients
// bucket is the name of the bucket
// path is the path to the directory to upload
// Output:
// If success, nil
// Otherwise, an error from the call to UploadWithIterator
func UploadDirectory(sess *session.Session, bucket *string, path *string) error {
di := NewDirectoryIterator(bucket, path)
uploader := s3manager.NewUploader(sess)
err := uploader.UploadWithIterator(aws.BackgroundContext(), di)
if err != nil {
return err
}
return nil
}
// NewDirectoryIterator builds a new DirectoryIterator
func NewDirectoryIterator(bucket *string, dir *string) s3manager.BatchUploadIterator {
var paths []string
filepath.Walk(*dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
paths = append(paths, path)
}
return nil
})
return &DirectoryIterator{
filePaths: paths,
bucket: *bucket,
}
}
// Next returns whether next file exists
func (di *DirectoryIterator) Next() bool {
if len(di.filePaths) == 0 {
di.next.f = nil
return false
}
f, err := os.Open(di.filePaths[0])
di.err = err
di.next.f = f
di.next.path = di.filePaths[0]
di.filePaths = di.filePaths[1:]
return true && di.Err() == nil
}
// Err returns error of DirectoryIterator
func (di *DirectoryIterator) Err() error {
return di.err
}
// UploadObject uploads a file
func (di *DirectoryIterator) UploadObject() s3manager.BatchUploadObject {
f := di.next.f
return s3manager.BatchUploadObject{
Object: &s3manager.UploadInput{
Bucket: &di.bucket,
Key: &di.next.path,
Body: f,
},
After: func() error {
return f.Close()
},
}
}
func main() {
// snippet-start:[s3.go.upload_directory.args]
bucket := flag.String("b", "", "The name of the bucket")
directory := flag.String("d", "", "The directory to upload")
flag.Parse()
if *bucket == "" || *directory == "" {
fmt.Println("You must supply the bucket name (-b BUCKET) and directory path (-d DIRECTORY)")
return
}
// snippet-end:[s3.go.upload_directory.args]
// snippet-start:[s3.go.upload_directory.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[s3.go.upload_directory.session]
err := UploadDirectory(sess, bucket, directory)
if err != nil {
fmt.Println("Got an error uploading directory " + *directory + " to bucket " + *bucket)
return
}
fmt.Println("Uploaded directory " + *directory + " to bucket " + *bucket)
}
// snippet-end:[s3.go.upload_directory]