Skip to content

Commit 61fed52

Browse files
committed
all: oss: ListObjects should treat prefix as a dir
See: - helm/chartmuseum#1082 - helm/chartmuseum#794 Signed-off-by: scnace <[email protected]>
1 parent 50864e2 commit 61fed52

File tree

11 files changed

+81
-86
lines changed

11 files changed

+81
-86
lines changed

alibaba.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func NewAlibabaCloudOSSBackend(bucket string, prefix string, endpoint string, ss
5353
}
5454

5555
client, err := oss.New(endpoint, accessKeyId, accessKeySecret)
56-
5756
if err != nil {
5857
panic("Failed to create OSS client: " + err.Error())
5958
}
@@ -77,6 +76,7 @@ func (b AlibabaCloudOSSBackend) ListObjects(prefix string) ([]Object, error) {
7776
var objects []Object
7877

7978
prefix = pathutil.Join(b.Prefix, prefix)
79+
prefix = normalizePath(prefix)
8080
ossPrefix := oss.Prefix(prefix)
8181
marker := oss.Marker("")
8282
for {
@@ -113,7 +113,6 @@ func (b AlibabaCloudOSSBackend) GetObject(path string) (Object, error) {
113113
var content []byte
114114
key := pathutil.Join(b.Prefix, path)
115115
body, err := b.Bucket.GetObject(key)
116-
117116
if err != nil {
118117
return object, err
119118
}

amazon.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package storage
1818

1919
import (
2020
"bytes"
21+
"crypto/tls"
2122
"io/ioutil"
22-
pathutil "path"
23-
"strings"
2423
"net/http"
25-
"crypto/tls"
2624
"os"
25+
pathutil "path"
26+
"strings"
27+
2728
"github.com/aws/aws-sdk-go/aws"
2829
"github.com/aws/aws-sdk-go/aws/credentials"
2930
"github.com/aws/aws-sdk-go/aws/session"
@@ -134,6 +135,7 @@ func NewAmazonS3BackendWithCredentials(bucket string, prefix string, region stri
134135
func (b AmazonS3Backend) ListObjects(prefix string) ([]Object, error) {
135136
var objects []Object
136137
prefix = pathutil.Join(b.Prefix, prefix)
138+
prefix = normalizePath(prefix)
137139
s3Input := &s3.ListObjectsInput{
138140
Bucket: aws.String(b.Bucket),
139141
Prefix: aws.String(prefix),

baidu.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ func NewBaiDuBOSBackend(bucket string, prefix string, endpoint string) *BaiduBOS
5252
}
5353

5454
client, err := bos.NewClient(accessKeyId, accessKeySecret, endpoint)
55-
5655
if err != nil {
5756
panic("Failed to create BOS client: " + err.Error())
5857
}
@@ -67,44 +66,45 @@ func NewBaiDuBOSBackend(bucket string, prefix string, endpoint string) *BaiduBOS
6766

6867
// ListObjects lists all objects in Baidu Cloud BOS bucket, at prefix
6968
func (b BaiduBOSBackend) ListObjects(prefix string) ([]Object, error) {
70-
var objects []Object
71-
72-
prefix = pathutil.Join(b.Prefix, prefix)
73-
listObjectsArgs := &api.ListObjectsArgs{
74-
Prefix: prefix,
75-
Marker: "",
76-
MaxKeys: 1000,
77-
}
78-
for {
79-
lor, err := b.Client.ListObjects(b.Bucket, listObjectsArgs)
80-
if err != nil {
81-
return objects, err
82-
}
83-
84-
for _, obj := range lor.Contents {
85-
path := removePrefixFromObjectPath(prefix, obj.Key)
86-
if objectPathIsInvalid(path) {
87-
continue
88-
}
89-
lastModified, err := time.Parse(time.RFC3339, obj.LastModified)
90-
if err != nil {
91-
continue
92-
}
93-
object := Object{
94-
Path: path,
95-
Content: []byte{},
96-
LastModified: lastModified,
97-
}
98-
objects = append(objects, object)
99-
}
100-
if !lor.IsTruncated {
101-
break
102-
}
103-
listObjectsArgs.Prefix = lor.Prefix
104-
listObjectsArgs.Marker = lor.NextMarker
105-
}
106-
107-
return objects, nil
69+
var objects []Object
70+
71+
prefix = pathutil.Join(b.Prefix, prefix)
72+
prefix = normalizePath(prefix)
73+
listObjectsArgs := &api.ListObjectsArgs{
74+
Prefix: prefix,
75+
Marker: "",
76+
MaxKeys: 1000,
77+
}
78+
for {
79+
lor, err := b.Client.ListObjects(b.Bucket, listObjectsArgs)
80+
if err != nil {
81+
return objects, err
82+
}
83+
84+
for _, obj := range lor.Contents {
85+
path := removePrefixFromObjectPath(prefix, obj.Key)
86+
if objectPathIsInvalid(path) {
87+
continue
88+
}
89+
lastModified, err := time.Parse(time.RFC3339, obj.LastModified)
90+
if err != nil {
91+
continue
92+
}
93+
object := Object{
94+
Path: path,
95+
Content: []byte{},
96+
LastModified: lastModified,
97+
}
98+
objects = append(objects, object)
99+
}
100+
if !lor.IsTruncated {
101+
break
102+
}
103+
listObjectsArgs.Prefix = lor.Prefix
104+
listObjectsArgs.Marker = lor.NextMarker
105+
}
106+
107+
return objects, nil
108108
}
109109

110110
// GetObject retrieves an object from Baidu Cloud BOS bucket, at prefix

etcd.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@ package storage
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
pathutil "path"
78
"strconv"
89
"strings"
910
"sync"
1011
"time"
1112

12-
clientv3 "go.etcd.io/etcd/client/v3"
1313
"go.etcd.io/etcd/client/pkg/v3/transport"
14+
clientv3 "go.etcd.io/etcd/client/v3"
1415
)
1516

1617
const DefaultPrefix = "/chart_backend_bucket"
1718

1819
var (
1920
DefileDialTimeOut = "5s"
2021
TimeStampKey = "timestamp"
21-
ErrNotExistEndpoints = fmt.Errorf("endpoints cannot connect !")
22-
ErrNotExist = fmt.Errorf("not exist!")
22+
ErrNotExistEndpoints = errors.New("endpoints cannot connect !")
23+
ErrNotExist = errors.New("not exist!")
2324
)
2425

2526
type etcdOpts struct {
@@ -38,7 +39,7 @@ type etcdStorage struct {
3839
mu sync.RWMutex
3940
}
4041

41-
//connection cut off
42+
// connection cut off
4243
func isServerErr(err error) bool {
4344
if err != nil {
4445
if err == context.Canceled {
@@ -52,11 +53,9 @@ func isServerErr(err error) bool {
5253
return false
5354
}
5455

55-
//prapare {basepath} dir
56+
// prapare {basepath} dir
5657
func (e *etcdStorage) probe() error {
57-
var (
58-
err error
59-
)
58+
var err error
6059
ctx, cancel := context.WithCancel(e.ctx)
6160
_, err = e.c.Put(ctx, e.base, "")
6261
cancel()
@@ -100,13 +99,11 @@ func (e *etcdStorage) delTimeStamp(path string) error {
10099
return err
101100
}
102101

103-
//
104102
func (e *etcdStorage) ListObjects(prefix string) ([]Object, error) {
105-
var (
106-
objs []Object
107-
)
103+
var objs []Object
108104
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
109105
newpath := pathutil.Join(e.base, prefix)
106+
newpath = normalizePath(newpath)
110107
resps, err := e.c.Get(ctx, newpath, clientv3.WithPrefix())
111108
cancel()
112109
if err != nil {
@@ -118,7 +115,7 @@ func (e *etcdStorage) ListObjects(prefix string) ([]Object, error) {
118115
if objectPathIsInvalid(path) {
119116
continue
120117
}
121-
//TODO need optimizate
118+
// TODO need optimizate
122119
if strings.HasSuffix(path, TimeStampKey) {
123120
continue
124121
}
@@ -134,13 +131,10 @@ func (e *etcdStorage) ListObjects(prefix string) ([]Object, error) {
134131
}
135132
}
136133
return objs, nil
137-
138134
}
139135

140136
func (e *etcdStorage) GetObject(path string) (Object, error) {
141-
var (
142-
modifytime time.Time
143-
)
137+
var modifytime time.Time
144138
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
145139
newpath := pathutil.Join(e.base, path)
146140
resps, err := e.c.Get(ctx, newpath)
@@ -164,9 +158,7 @@ func (e *etcdStorage) GetObject(path string) (Object, error) {
164158
}
165159

166160
func (e *etcdStorage) PutObject(path string, content []byte) error {
167-
var (
168-
updatetime = time.Now()
169-
)
161+
updatetime := time.Now()
170162
ctx, cancel := context.WithTimeout(e.ctx, e.opts.dialtimeout)
171163
newpath := pathutil.Join(e.base, path)
172164
_, err := e.c.Put(ctx, newpath, string(content))
@@ -191,9 +183,7 @@ func (e *etcdStorage) DeleteObject(path string) error {
191183
}
192184

193185
func parseConf(endpoints string, cafile, certfile, keyfile string, dialtime time.Duration) clientv3.Config {
194-
var (
195-
es []string
196-
)
186+
var es []string
197187
if endpoints == "" {
198188
panic(ErrNotExistEndpoints)
199189
}
@@ -215,9 +205,7 @@ func parseConf(endpoints string, cafile, certfile, keyfile string, dialtime time
215205
}
216206

217207
func NewEtcdCSBackend(endpoints string, cafile, certfile, keyfile string, prefix string) Backend {
218-
var (
219-
basepath string
220-
)
208+
var basepath string
221209
DialTimeOut, _ := time.ParseDuration(DefileDialTimeOut)
222210
cli, err := clientv3.New(parseConf(endpoints, cafile, certfile, keyfile, DialTimeOut))
223211
if err != nil {
@@ -249,5 +237,4 @@ func NewEtcdCSBackend(endpoints string, cafile, certfile, keyfile string, prefix
249237
panic(err)
250238
}
251239
return e
252-
253240
}

google.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func NewGoogleCSBackend(bucket string, prefix string) *GoogleCSBackend {
5353
func (b GoogleCSBackend) ListObjects(prefix string) ([]Object, error) {
5454
var objects []Object
5555
prefix = pathutil.Join(b.Prefix, prefix)
56+
prefix = normalizePath(prefix)
5657
listQuery := &storage.Query{
5758
Prefix: prefix,
5859
}

microsoft.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ package storage
1919
import (
2020
"errors"
2121
"io/ioutil"
22+
"os"
2223
pathutil "path"
2324
"time"
2425

25-
"os"
26-
2726
microsoft_storage "github.com/Azure/azure-sdk-for-go/storage"
2827
)
2928

@@ -39,7 +38,6 @@ type MicrosoftBlobBackend struct {
3938

4039
// NewMicrosoftBlobBackend creates a new instance of MicrosoftBlobBackend
4140
func NewMicrosoftBlobBackend(container string, prefix string) *MicrosoftBlobBackend {
42-
4341
// From the Azure portal, get your storage account name and key and set environment variables.
4442
accountName, accountKey := os.Getenv("AZURE_STORAGE_ACCOUNT"), os.Getenv("AZURE_STORAGE_ACCESS_KEY")
4543
var serviceBaseURL, apiVersion string
@@ -79,6 +77,7 @@ func (b MicrosoftBlobBackend) ListObjects(prefix string) ([]Object, error) {
7977

8078
var params microsoft_storage.ListBlobsParameters
8179
prefix = pathutil.Join(b.Prefix, prefix)
80+
prefix = normalizePath(prefix)
8281
params.Prefix = prefix
8382

8483
for {

openstack.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (b OpenstackOSBackend) ListObjects(prefix string) ([]Object, error) {
225225
var objects []Object
226226

227227
prefix = pathutil.Join(b.Prefix, prefix)
228+
prefix = normalizePath(prefix)
228229
opts := &osObjects.ListOpts{
229230
Full: true,
230231
Prefix: prefix,

oracle.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ type OracleCSBackend struct {
4343

4444
// NewOracleCSBackend creates a new instance of OracleCSBackend
4545
func NewOracleCSBackend(bucket string, prefix string, region string, compartmentId string) *OracleCSBackend {
46-
4746
var config common.ConfigurationProvider
4847
var err error
4948

@@ -102,7 +101,6 @@ func NewOracleCSBackend(bucket string, prefix string, region string, compartment
102101
}
103102

104103
func createBucket(ctx context.Context, c objectstorage.ObjectStorageClient, namespace string, bucket string, compartmentId string) (string, error) {
105-
106104
// Create the bucket
107105
request := objectstorage.CreateBucketRequest{
108106
NamespaceName: &namespace,
@@ -115,7 +113,6 @@ func createBucket(ctx context.Context, c objectstorage.ObjectStorageClient, name
115113
_, err := c.CreateBucket(ctx, request)
116114

117115
return bucket, err
118-
119116
}
120117

121118
func getNamespace(ctx context.Context, c objectstorage.ObjectStorageClient) (string, error) {
@@ -132,6 +129,7 @@ func getNamespace(ctx context.Context, c objectstorage.ObjectStorageClient) (str
132129
func (b OracleCSBackend) ListObjects(prefix string) ([]Object, error) {
133130
var objects []Object
134131
prefix = pathutil.Join(b.Prefix, prefix)
132+
prefix = normalizePath(prefix)
135133

136134
request := objectstorage.ListObjectsRequest{
137135
NamespaceName: &b.Namespace,
@@ -181,14 +179,12 @@ func (b OracleCSBackend) GetObject(path string) (Object, error) {
181179
}
182180

183181
rc, err := b.Client.GetObject(b.Context, request)
184-
185182
if err != nil {
186183
return object, err
187184
}
188185

189186
object.LastModified = rc.LastModified.Time
190187
content, err := ioutil.ReadAll(rc.Content)
191-
192188
if err != nil {
193189
return object, err
194190
}
@@ -198,7 +194,6 @@ func (b OracleCSBackend) GetObject(path string) (Object, error) {
198194

199195
// PutObject uploads an object to OCI Object Storage bucket, at prefix
200196
func (b OracleCSBackend) PutObject(path string, content []byte) error {
201-
202197
objectname := pathutil.Join(b.Prefix, path)
203198
metadata := make(map[string]string)
204199
contentLen := int64(binary.Size(content))
@@ -219,7 +214,6 @@ func (b OracleCSBackend) PutObject(path string, content []byte) error {
219214

220215
// DeleteObject removes an object from OCI Object Storage bucket, at prefix
221216
func (b OracleCSBackend) DeleteObject(path string) error {
222-
223217
objectname := pathutil.Join(b.Prefix, path)
224218

225219
request := objectstorage.DeleteObjectRequest{

storage.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ func cleanPrefix(prefix string) string {
9797
return strings.Trim(prefix, "/")
9898
}
9999

100+
// normalizePath ensures that the path ends with a slash
101+
func normalizePath(path string) string {
102+
if path != "" && !strings.HasSuffix(path, "/") {
103+
path += "/"
104+
}
105+
return path
106+
}
107+
100108
func removePrefixFromObjectPath(prefix string, path string) string {
101109
if prefix == "" {
102110
return path

0 commit comments

Comments
 (0)