forked from yunionio/cloudmux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3object.go
207 lines (184 loc) · 5.88 KB
/
s3object.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
// Copyright 2019 Yunion
//
// 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.
// Copyright 2019 Yunion
//
// 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 google
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"cloud.google.com/go/storage"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/cloudmux/pkg/cloudprovider"
)
type SBucketObjects struct {
Prefixes []string
Items []SObject
NextPageToken string
}
type SObject struct {
bucket *SBucket
Id string
Name string
SelfLink string
MediaLink string
Bucket string
Generation string
Metageneration string
ContentType string
ContentEncoding string
ContentDisposition string
ContentLanguage string
CacheControl string
StorageClass string
Size int64
Md5Hash string
Metadata map[string]string
Crc32c string
Etag string
TimeCreated time.Time
Updated time.Time
TimeStorageClassUpdated time.Time
}
func (region *SRegion) GetObjects(bucket string, prefix string, nextPageToken string, delimiter string, maxCount int) (*SBucketObjects, error) {
if maxCount <= 0 {
maxCount = 20
}
resource := fmt.Sprintf("b/%s/o", bucket)
params := map[string]string{"maxResults": fmt.Sprintf("%d", maxCount)}
if len(prefix) > 0 {
params["prefix"] = prefix
}
if len(delimiter) > 0 {
params["delimiter"] = delimiter
}
if len(nextPageToken) > 0 {
params["pageToken"] = nextPageToken
}
objs := SBucketObjects{}
resp, err := region.client.storageList(resource, params)
if err != nil {
return nil, errors.Wrap(err, "storageList")
}
err = resp.Unmarshal(&objs)
if err != nil {
return nil, errors.Wrap(err, "Unmarshal")
}
return &objs, nil
}
func (region *SRegion) ConvertAcl(acls []GCSAcl) cloudprovider.TBucketACLType {
ret := cloudprovider.ACLPrivate
for _, acl := range acls {
if acl.Entity == string(storage.AllUsers) {
if acl.Role == string(storage.RoleOwner) || acl.Role == string(storage.RoleWriter) {
ret = cloudprovider.ACLPublicReadWrite
}
if acl.Role == string(storage.RoleReader) && ret != cloudprovider.ACLPublicReadWrite {
ret = cloudprovider.ACLPublicRead
}
}
if !(ret == cloudprovider.ACLPublicRead || ret == cloudprovider.ACLPublicReadWrite) && acl.Entity == string(storage.AllAuthenticatedUsers) {
ret = cloudprovider.ACLAuthRead
}
}
return ret
}
func (o *SObject) GetAcl() cloudprovider.TBucketACLType {
if strings.HasSuffix(o.Name, "/") {
return cloudprovider.ACLPrivate
}
acls, err := o.bucket.region.GetObjectAcl(o.bucket.Name, o.Name)
if err != nil {
log.Errorf("failed to get object %s acls error: %v", o.Name, err)
return cloudprovider.ACLUnknown
}
return o.bucket.region.ConvertAcl(acls)
}
func (o *SObject) SetAcl(acl cloudprovider.TBucketACLType) error {
return o.bucket.region.SetObjectAcl(o.bucket.Name, o.Name, acl)
}
func (o *SObject) GetIBucket() cloudprovider.ICloudBucket {
return o.bucket
}
func (o *SObject) GetKey() string {
return o.Name
}
func (o *SObject) GetSizeBytes() int64 {
return o.Size
}
func (o *SObject) GetLastModified() time.Time {
return o.Updated
}
func (o *SObject) GetStorageClass() string {
return o.StorageClass
}
func (o *SObject) GetETag() string {
return o.Etag
}
func (o *SObject) GetMeta() http.Header {
meta := http.Header{}
for k, v := range o.Metadata {
meta.Set(k, v)
}
for k, v := range map[string]string{
cloudprovider.META_HEADER_CONTENT_TYPE: o.ContentType,
cloudprovider.META_HEADER_CONTENT_ENCODING: o.ContentEncoding,
cloudprovider.META_HEADER_CONTENT_DISPOSITION: o.ContentDisposition,
cloudprovider.META_HEADER_CONTENT_LANGUAGE: o.ContentLanguage,
cloudprovider.META_HEADER_CACHE_CONTROL: o.CacheControl,
} {
meta.Set(k, v)
}
return meta
}
func (region *SRegion) SetObjectMeta(bucket, object string, meta http.Header) error {
body := map[string]string{}
for k := range meta {
switch k {
case cloudprovider.META_HEADER_CONTENT_TYPE:
body["contentType"] = meta.Get(k)
case cloudprovider.META_HEADER_CONTENT_ENCODING:
body["contentEncoding"] = meta.Get(k)
case cloudprovider.META_HEADER_CONTENT_DISPOSITION:
body["contentDisposition"] = meta.Get(k)
case cloudprovider.META_HEADER_CONTENT_LANGUAGE:
body["contentLanguage"] = meta.Get(k)
case cloudprovider.META_HEADER_CACHE_CONTROL:
body["cacheControl"] = meta.Get(k)
default:
body[fmt.Sprintf("metadata.%s", k)] = meta.Get(k)
}
}
resource := fmt.Sprintf("b/%s/o/%s", bucket, url.PathEscape(object))
return region.StoragePut(resource, jsonutils.Marshal(body), nil)
}
func (o *SObject) SetMeta(ctx context.Context, meta http.Header) error {
return o.bucket.region.SetObjectMeta(o.bucket.Name, o.Name, meta)
}