forked from aws-solutions/data-transfer-hub-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
164 lines (133 loc) · 3.66 KB
/
common.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
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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.
*/
package dth
import (
"encoding/json"
"fmt"
"log"
"net/url"
"strconv"
"strings"
)
var (
// KB is 1024 Bytes
KB int = 1 << 10
// MB is 1024 KB
MB int = 1 << 20
)
// Source is an interface represents a type of cloud storage services
// type Source interface {
// GetEndpointURL()
// }
// Object represents an object to be replicated.
type Object struct {
Key string `json:"key"`
Size int64 `json:"size"`
Sequencer string `json:"sequencer,omitempty"`
}
// S3Event represents a basic structure of a S3 Event Message
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-content-structure.html for more details
type S3Event struct {
Records []struct {
EventSource, AwsRegion, EventTime, EventName string
S3 struct {
Object `json:"object"`
}
}
}
// Part represents a part for multipart upload
type Part struct {
partNumber int
etag *string
}
// Metadata info of object
type Metadata struct {
// ContentType
ContentType *string
// ContentLanguage
ContentLanguage *string
// ContentEncoding
ContentEncoding *string
// CacheControl
CacheControl *string
// Custom metadata to store with the object in S3.
// Map keys will be normalized to lower-case.
Metadata map[string]string
}
// Helper function to convert Object into Json string
func (o *Object) toString() *string {
// log.Printf("Convert %v to string", o)
obj, err := json.Marshal(*o)
if err != nil {
log.Printf("Unable to convert object to json string - %s", err)
return nil
}
str := string(obj)
// msgStr := fmt.Sprintf(`{"key":"%s","size":%d,"version":"%s"}`, m.Key, m.Size, m.Version)
return &str
}
// Helper function to create Object base on Json string
func newObject(str *string) (o *Object) {
o = new(Object)
err := json.Unmarshal([]byte(*str), o)
if err != nil {
log.Printf("Unable to convert string to object - %s", err.Error())
return nil
}
// log.Printf("Key: %s, Size: %d\n", m.Key, m.Size)
return
}
// Helper function to create S3Event base on Json string
func newS3Event(str *string) (e *S3Event) {
e = new(S3Event)
err := json.Unmarshal([]byte(*str), e)
if err != nil {
log.Printf("Unable to convert string to S3 Event - %s", err.Error())
return nil
}
// log.Printf("Key: %s, Size: %d\n", m.Key, m.Size)
return
}
func getHex(str *string) int64 {
i64, _ := strconv.ParseInt(*str, 16, 64)
return i64
}
func unescape(str *string) string {
output, _ := url.QueryUnescape(*str)
return output
}
func escape(str *string) string {
return url.QueryEscape(*str)
}
func removePrefix(key, prefix *string) *string {
if prefix == nil || *prefix == "" || *prefix == "/" {
return key
}
delimiter := ""
if !strings.HasSuffix(*prefix, "/") {
delimiter = "/"
}
newkey := strings.Replace(*key, *prefix+delimiter, "", 1)
return &newkey
}
func appendPrefix(key, prefix *string) *string {
if prefix == nil || *prefix == "" || *prefix == "/" {
return key
}
delimiter := ""
if !strings.HasSuffix(*prefix, "/") {
delimiter = "/"
}
newkey := fmt.Sprintf("%s%s%s", *prefix, delimiter, *key)
return &newkey
}