-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathobject.go
226 lines (212 loc) · 5.77 KB
/
object.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
package s3client
import (
"fmt"
"io"
"net/http"
"strconv"
"github.com/webrtcn/s3client/models"
)
//Object Object
type Object struct {
client *Client
bucketName string
}
//NewUploads Initiated multipart uploads
func (o *Object) NewUploads(objName string) *Uploads {
uploader := &Uploads{
client: o.client,
bucketName: o.bucketName,
objectName: objName,
}
return uploader
}
//GetObjectOption download file optioin
type GetObjectOption struct {
Range *Range //The range of the object to retrieve.
IfModifiedSince string //Gets only if modified since the timestamp.
IfUnmodifiedSince string //Gets only if not modified since the timestamp.
IfMatchTag string //Gets only if object ETag matches ETag.
IfNotMatchTag string //Gets only if object ETag matches ETag.
}
//Range The range of the object to retrieve.
type Range struct {
Begin int64
End int64
}
//CopyObjectOption Copy Object Option
type CopyObjectOption struct {
Perm *models.ACL //A canned ACL.
CopyIfModifiedSince string //Copies only if modified since the timestamp.
CopyIfUnmodifiedSince string //Copies only if unmodified since the timestamp.
CopyIfMatchTag string //Copies only if object ETag matches ETag.
CopyIfNotMatchTag string //Copies only if object ETag doesn’t match.
}
//Create upload object to bucket
func (o *Object) Create(name, contentMd5, contentType string, contentLength int64, body io.ReadCloser, perm models.ACL) (err error) {
header := http.Header{
"x-amz-acl": {string(perm)},
"Content-Length": {strconv.FormatInt(contentLength, 10)},
"Content-MD5": {contentMd5},
"Content-Type": {contentType},
}
req := &request{
method: put,
bucket: o.bucketName,
object: name,
headers: header,
playload: body,
}
err = o.client.do(req, nil, nil)
return
}
//Get get object from bucket with name
func (o *Object) Get(name string, option *GetObjectOption) (*http.Response, error) {
header := http.Header{}
if option != nil {
if option.Range != nil {
var value string
if option.Range.End == 0 {
value = fmt.Sprintf("bytes=%d-", option.Range.Begin)
} else {
value = fmt.Sprintf("bytes=%d-%d", option.Range.Begin, option.Range.End)
}
header.Add("Range", value)
}
if len(option.IfModifiedSince) > 0 {
header.Add("If-Modified-Since", option.IfModifiedSince)
}
if len(option.IfUnmodifiedSince) > 0 {
header.Add("If-Unmodified-Since", option.IfUnmodifiedSince)
}
if len(option.IfMatchTag) > 0 {
header.Add("If-Match", option.IfMatchTag)
}
if len(option.IfNotMatchTag) > 0 {
header.Add("If-None-Match", option.IfNotMatchTag)
}
}
req := &request{
method: get,
bucket: o.bucketName,
object: name,
headers: header,
}
var response *http.Response
err := o.client.do(req, nil, func(e *http.Response) {
response = e
})
return response, err
}
//Copy object
func (o *Object) Copy(sourceBucket, sourceObject, destObject string, option *CopyObjectOption) (*models.CopyObjectResult, error) {
req := &request{
method: put,
bucket: o.bucketName,
object: destObject,
headers: http.Header{
"x-amz-copy-source": {fmt.Sprintf("%s/%s", sourceBucket, sourceObject)},
},
}
if option != nil {
if option.Perm != nil {
req.headers.Add("x-amz-acl", string(*option.Perm))
}
if len(option.CopyIfMatchTag) > 0 {
req.headers.Add("x-amz-copy-if-match", option.CopyIfMatchTag)
}
if len(option.CopyIfModifiedSince) > 0 {
req.headers.Add("x-amz-copy-if-modified-since", option.CopyIfModifiedSince)
}
if len(option.CopyIfNotMatchTag) > 0 {
req.headers.Add("x-amz-copy-if-none-match", option.CopyIfNotMatchTag)
}
if len(option.CopyIfUnmodifiedSince) > 0 {
req.headers.Add("x-amz-copy-if-unmodified-since", option.CopyIfUnmodifiedSince)
}
}
value := &models.CopyObjectResult{}
err := o.client.do(req, value, nil)
return value, err
}
//Remove object from bucket
func (o *Object) Remove(name string) error {
req := &request{
method: delete,
bucket: o.bucketName,
object: name,
}
return o.client.do(req, nil, nil)
}
//GetHeader get object header information
func (o *Object) GetHeader(name string, option *GetObjectOption) (*http.Response, error) {
header := http.Header{}
if option != nil {
if option.Range != nil {
var value string
if option.Range.End == 0 {
value = fmt.Sprintf("bytes=%d-", option.Range.Begin)
} else {
value = fmt.Sprintf("bytes=%d-%d", option.Range.Begin, option.Range.End)
}
header.Add("Range", value)
}
if len(option.IfModifiedSince) > 0 {
header.Add("If-Modified-Since", option.IfModifiedSince)
}
if len(option.IfUnmodifiedSince) > 0 {
header.Add("If-Unmodified-Since", option.IfUnmodifiedSince)
}
if len(option.IfMatchTag) > 0 {
header.Add("If-Match", option.IfMatchTag)
}
if len(option.IfNotMatchTag) > 0 {
header.Add("If-None-Match", option.IfNotMatchTag)
}
}
req := &request{
method: head,
bucket: o.bucketName,
object: name,
headers: header,
}
var response *http.Response
err := o.client.do(req, nil, func(e *http.Response) {
response = e
})
return response, err
}
//GetACL get acl of the object
func (o *Object) GetACL(name string) (*models.AccessControlPolicy, error) {
req := &request{
method: get,
bucket: o.bucketName,
object: name,
suffixs: []suffix{
suffix{
key: "acl",
flag: true,
},
},
}
policy := &models.AccessControlPolicy{}
err := o.client.do(req, policy, nil)
return policy, err
}
//SetACL set acl of the object with header
func (o *Object) SetACL(name string, perm models.ACL) error {
req := &request{
method: put,
bucket: o.bucketName,
object: name,
suffixs: []suffix{
suffix{
key: "acl",
flag: true,
},
},
headers: http.Header{
"x-amz-acl": {string(perm)},
},
}
return o.client.do(req, nil, nil)
}