forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
282 lines (231 loc) · 7.01 KB
/
storage.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
// Package storage implements operations for s3 and fs.
package storage
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/kmiku7/s5cmd/log"
"github.com/kmiku7/s5cmd/storage/url"
"github.com/kmiku7/s5cmd/strutil"
)
var (
// ErrNoObjectFound indicates there are no objects found from a given directory.
ErrNoObjectFound = fmt.Errorf("no object found")
)
// ErrGivenObjectNotFound indicates a specified object is not found.
type ErrGivenObjectNotFound struct {
ObjectAbsPath string
}
func (e *ErrGivenObjectNotFound) Error() string {
return fmt.Sprintf("given object %v not found", e.ObjectAbsPath)
}
// Storage is an interface for storage operations that is common
// to local filesystem and remote object storage.
type Storage interface {
// Stat returns the Object structure describing object. If src is not
// found, ErrGivenObjectNotFound is returned.
Stat(ctx context.Context, src *url.URL) (*Object, error)
// List the objects and directories/prefixes in the src.
List(ctx context.Context, src *url.URL, followSymlinks bool) <-chan *Object
// Delete deletes the given src.
Delete(ctx context.Context, src *url.URL) error
// MultiDelete deletes all items returned from given urls in batches.
MultiDelete(ctx context.Context, urls <-chan *url.URL) <-chan *Object
// Copy src to dst, optionally setting the given metadata. Src and dst
// arguments are of the same type. If src is a remote type, server side
// copying will be used.
Copy(ctx context.Context, src, dst *url.URL, metadata Metadata) error
}
func NewLocalClient(opts Options) *Filesystem {
return &Filesystem{dryRun: opts.DryRun}
}
func NewRemoteClient(ctx context.Context, url *url.URL, opts Options) (*S3, error) {
newOpts := Options{
MaxRetries: opts.MaxRetries,
Endpoint: opts.Endpoint,
NoVerifySSL: opts.NoVerifySSL,
DryRun: opts.DryRun,
NoSignRequest: opts.NoSignRequest,
UseListObjectsV1: opts.UseListObjectsV1,
RequestPayer: opts.RequestPayer,
Profile: opts.Profile,
CredentialFile: opts.CredentialFile,
LogLevel: opts.LogLevel,
bucket: url.Bucket,
region: opts.region,
NoSuchUploadRetryCount: opts.NoSuchUploadRetryCount,
}
return newS3Storage(ctx, newOpts)
}
func NewClient(ctx context.Context, url *url.URL, opts Options) (Storage, error) {
if url.IsRemote() {
return NewRemoteClient(ctx, url, opts)
}
return NewLocalClient(opts), nil
}
// Options stores configuration for storage.
type Options struct {
MaxRetries int
NoSuchUploadRetryCount int
Endpoint string
NoVerifySSL bool
DryRun bool
NoSignRequest bool
UseListObjectsV1 bool
LogLevel log.LogLevel
RequestPayer string
Profile string
CredentialFile string
bucket string
region string
}
func (o *Options) SetRegion(region string) {
o.region = region
}
// Object is a generic type which contains metadata for storage items.
type Object struct {
URL *url.URL `json:"key,omitempty"`
Etag string `json:"etag,omitempty"`
ModTime *time.Time `json:"last_modified,omitempty"`
Type ObjectType `json:"type,omitempty"`
Size int64 `json:"size,omitempty"`
StorageClass StorageClass `json:"storage_class,omitempty"`
Err error `json:"error,omitempty"`
retryID string
}
// String returns the string representation of Object.
func (o *Object) String() string {
return o.URL.String()
}
// JSON returns the JSON representation of Object.
func (o *Object) JSON() string {
return strutil.JSON(o)
}
// ObjectType is the type of Object.
type ObjectType struct {
mode os.FileMode
}
// String returns the string representation of ObjectType.
func (o ObjectType) String() string {
switch mode := o.mode; {
case mode.IsRegular():
return "file"
case mode.IsDir():
return "directory"
case mode&os.ModeSymlink != 0:
return "symlink"
}
return ""
}
// MarshalJSON returns the stringer of ObjectType as a marshalled json.
func (o ObjectType) MarshalJSON() ([]byte, error) {
return json.Marshal(o.String())
}
// IsDir checks if the object is a directory.
func (o ObjectType) IsDir() bool {
return o.mode.IsDir()
}
// IsSymlink checks if the object is a symbolic link.
func (o ObjectType) IsSymlink() bool {
return o.mode&os.ModeSymlink != 0
}
// ShouldProcessUrl returns true if follow symlinks is enabled.
// If follow symlinks is disabled we should not process the url.
// (this check is needed only for local files)
func ShouldProcessUrl(url *url.URL, followSymlinks bool) bool {
if followSymlinks {
return true
}
if url.IsRemote() {
return true
}
fi, err := os.Lstat(url.Absolute())
if err != nil {
return false
}
// do not process symlinks
return fi.Mode()&os.ModeSymlink == 0
}
// dateFormat is a constant time template for the bucket.
const dateFormat = "2006/01/02 15:04:05"
// Bucket is a container for storage objects.
type Bucket struct {
CreationDate time.Time `json:"created_at"`
Name string `json:"name"`
}
// String returns the string representation of Bucket.
func (b Bucket) String() string {
return fmt.Sprintf("%s s3://%s", b.CreationDate.Format(dateFormat), b.Name)
}
// JSON returns the JSON representation of Bucket.
func (b Bucket) JSON() string {
return strutil.JSON(b)
}
// StorageClass represents the storage used to store an object.
type StorageClass string
func (s StorageClass) IsGlacier() bool {
return s == "GLACIER"
}
type Metadata map[string]string
// NewMetadata will return an empty metadata object.
func NewMetadata() Metadata {
return Metadata{}
}
func (m Metadata) ACL() string {
return m["ACL"]
}
func (m Metadata) SetACL(acl string) Metadata {
m["ACL"] = acl
return m
}
func (m Metadata) CacheControl() string {
return m["CacheControl"]
}
func (m Metadata) SetCacheControl(cacheControl string) Metadata {
m["CacheControl"] = cacheControl
return m
}
func (m Metadata) Expires() string {
return m["Expires"]
}
func (m Metadata) SetExpires(expires string) Metadata {
m["Expires"] = expires
return m
}
func (m Metadata) StorageClass() string {
return m["StorageClass"]
}
func (m Metadata) SetStorageClass(class string) Metadata {
m["StorageClass"] = class
return m
}
func (m Metadata) ContentType() string {
return m["ContentType"]
}
func (m Metadata) SetContentType(contentType string) Metadata {
m["ContentType"] = contentType
return m
}
func (m Metadata) SSE() string {
return m["EncryptionMethod"]
}
func (m Metadata) SetSSE(sse string) Metadata {
m["EncryptionMethod"] = sse
return m
}
func (m Metadata) SSEKeyID() string {
return m["EncryptionKeyID"]
}
func (m Metadata) SetSSEKeyID(kid string) Metadata {
m["EncryptionKeyID"] = kid
return m
}
func (m Metadata) ContentEncoding() string {
return m["ContentEncoding"]
}
func (m Metadata) SetContentEncoding(contentEncoding string) Metadata {
m["ContentEncoding"] = contentEncoding
return m
}