Skip to content

Commit 9de39f7

Browse files
committed
enhancement as proposed from review
1 parent 2bfbdec commit 9de39f7

File tree

3 files changed

+101
-28
lines changed

3 files changed

+101
-28
lines changed

alioss/client/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func (client *AliBlobstore) Delete(object string) error {
4242
}
4343

4444
func (client *AliBlobstore) DeleteRecursive(prefix string) error {
45-
4645
return client.storageClient.DeleteRecursive(prefix)
4746
}
4847

@@ -86,16 +85,13 @@ func (client *AliBlobstore) List(prefix string) ([]string, error) {
8685
}
8786

8887
func (client *AliBlobstore) Copy(srcBlob string, dstBlob string) error {
89-
9088
return client.storageClient.Copy(srcBlob, dstBlob)
9189
}
9290

9391
func (client *AliBlobstore) Properties(dest string) error {
94-
9592
return client.storageClient.Properties(dest)
9693
}
9794

9895
func (client *AliBlobstore) EnsureBucketExists() error {
99-
10096
return client.storageClient.EnsureBucketExists()
10197
}

alioss/client/storage_client.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type StorageClient interface {
3636
) error
3737

3838
DeleteRecursive(
39-
dest string,
39+
objects string,
4040
) error
4141

4242
Exists(
@@ -54,11 +54,11 @@ type StorageClient interface {
5454
) (string, error)
5555

5656
List(
57-
prefix string,
57+
bucketPrefix string,
5858
) ([]string, error)
5959

6060
Properties(
61-
dest string,
61+
object string,
6262
) error
6363

6464
EnsureBucketExists() error
@@ -137,13 +137,13 @@ func (dsc DefaultStorageClient) Download(
137137
}
138138

139139
func (dsc DefaultStorageClient) Copy(
140-
srcObject string,
141-
destObject string,
140+
sourceObject string,
141+
destinationObject string,
142142
) error {
143-
log.Printf("Copying object from %s to %s", srcObject, destObject)
143+
log.Printf("Copying object from %s to %s", sourceObject, destinationObject)
144144

145-
if _, err := dsc.bucket.CopyObject(srcObject, destObject); err != nil {
146-
return fmt.Errorf("failed to copy object from %s to %s: %w", srcObject, destObject, err)
145+
if _, err := dsc.bucket.CopyObject(sourceObject, destinationObject); err != nil {
146+
return fmt.Errorf("failed to copy object from %s to %s: %w", sourceObject, destinationObject, err)
147147
}
148148

149149
return nil
@@ -168,11 +168,11 @@ func (dsc DefaultStorageClient) Delete(
168168
}
169169

170170
func (dsc DefaultStorageClient) DeleteRecursive(
171-
prefix string,
171+
objectPrefix string,
172172
) error {
173-
if prefix != "" {
173+
if objectPrefix != "" {
174174
log.Printf("Deleting all objects in bucket %s with prefix '%s'\n",
175-
dsc.storageConfig.BucketName, prefix)
175+
dsc.storageConfig.BucketName, objectPrefix)
176176
} else {
177177
log.Printf("Deleting all objects in bucket %s\n",
178178
dsc.storageConfig.BucketName)
@@ -182,8 +182,8 @@ func (dsc DefaultStorageClient) DeleteRecursive(
182182

183183
for {
184184
var listOptions []oss.Option
185-
if prefix != "" {
186-
listOptions = append(listOptions, oss.Prefix(prefix))
185+
if objectPrefix != "" {
186+
listOptions = append(listOptions, oss.Prefix(objectPrefix))
187187
}
188188
if marker != "" {
189189
listOptions = append(listOptions, oss.Marker(marker))
@@ -326,20 +326,20 @@ type BlobProperties struct {
326326
}
327327

328328
func (dsc DefaultStorageClient) Properties(
329-
dest string,
329+
bucketObject string,
330330
) error {
331331
log.Printf("Getting properties for object %s/%s\n",
332-
dsc.storageConfig.BucketName, dest)
332+
dsc.storageConfig.BucketName, bucketObject)
333333

334-
meta, err := dsc.bucket.GetObjectDetailedMeta(dest)
334+
meta, err := dsc.bucket.GetObjectDetailedMeta(bucketObject)
335335
if err != nil {
336336
var ossErr oss.ServiceError
337337
if errors.As(err, &ossErr) && ossErr.StatusCode == 404 {
338338
fmt.Println(`{}`)
339339
return nil
340340
}
341341

342-
return fmt.Errorf("failed to get properties for object %s: %w", dest, err)
342+
return fmt.Errorf("failed to get properties for object %s: %w", bucketObject, err)
343343
}
344344

345345
eTag := meta.Get("ETag")

alioss/integration/general_ali_test.go

Lines changed: 84 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package integration_test
22

33
import (
44
"bytes"
5+
"fmt"
56

67
"os"
78

9+
"github.com/aliyun/aliyun-oss-go-sdk/oss"
810
"github.com/cloudfoundry/storage-cli/alioss/config"
911
"github.com/cloudfoundry/storage-cli/alioss/integration"
1012

@@ -209,13 +211,16 @@ var _ = Describe("General testing for all Ali regions", func() {
209211
destBlob := blobName + "-dest"
210212

211213
defer func() {
212-
cliSession, err := integration.RunCli(cliPath, configPath, "delete", srcBlob)
213-
Expect(err).ToNot(HaveOccurred())
214-
Expect(cliSession.ExitCode()).To(BeZero())
215-
216-
cliSession, err = integration.RunCli(cliPath, configPath, "delete", destBlob)
217-
Expect(err).ToNot(HaveOccurred())
218-
Expect(cliSession.ExitCode()).To(BeZero())
214+
for _, b := range []string{srcBlob, destBlob} {
215+
cliSession, err := integration.RunCli(cliPath, configPath, "delete", b)
216+
if err != nil {
217+
GinkgoWriter.Printf("cleanup: error deleting %s: %v\n", b, err)
218+
continue
219+
}
220+
if cliSession.ExitCode() != 0 && cliSession.ExitCode() != 3 {
221+
GinkgoWriter.Printf("cleanup: delete %s exited with code %d\n", b, cliSession.ExitCode())
222+
}
223+
}
219224
}()
220225

221226
contentFile = integration.MakeContentFile("copied content")
@@ -346,9 +351,81 @@ var _ = Describe("General testing for all Ali regions", func() {
346351
Expect(output).To(ContainSubstring(blob2))
347352
Expect(output).NotTo(ContainSubstring(otherBlob))
348353
})
354+
355+
It("lists all blobs across multiple pages", func() {
356+
prefix := integration.GenerateRandomString()
357+
const totalObjects = 120
358+
359+
var blobNames []string
360+
var contentFiles []string
361+
362+
for i := 0; i < totalObjects; i++ {
363+
blobName := fmt.Sprintf("%s/%03d", prefix, i)
364+
blobNames = append(blobNames, blobName)
365+
366+
contentFile := integration.MakeContentFile(fmt.Sprintf("content-%d", i))
367+
contentFiles = append(contentFiles, contentFile)
368+
369+
cliSession, err := integration.RunCli(cliPath, configPath, "put", contentFile, blobName)
370+
Expect(err).ToNot(HaveOccurred())
371+
Expect(cliSession.ExitCode()).To(BeZero())
372+
}
373+
374+
defer func() {
375+
for _, f := range contentFiles {
376+
_ = os.Remove(f) //nolint:errcheck
377+
}
378+
379+
for _, b := range blobNames {
380+
cliSession, err := integration.RunCli(cliPath, configPath, "delete", b)
381+
if err == nil && (cliSession.ExitCode() == 0 || cliSession.ExitCode() == 3) {
382+
continue
383+
}
384+
}
385+
}()
386+
387+
cliSession, err := integration.RunCli(cliPath, configPath, "list", prefix)
388+
Expect(err).ToNot(HaveOccurred())
389+
Expect(cliSession.ExitCode()).To(BeZero())
390+
391+
output := bytes.NewBuffer(cliSession.Out.Contents()).String()
392+
393+
for _, b := range blobNames {
394+
Expect(output).To(ContainSubstring(b))
395+
}
396+
})
349397
})
350398

351399
Describe("Invoking `ensure-bucket-exists`", func() {
400+
It("creates a bucket that can be observed via the OSS API", func() {
401+
newBucketName := bucketName + "-bommel"
402+
403+
cfg := defaultConfig
404+
cfg.BucketName = newBucketName
405+
406+
configPath = integration.MakeConfigFile(&cfg)
407+
defer func() { _ = os.Remove(configPath) }() //nolint:errcheck
408+
409+
ossClient, err := oss.New(cfg.Endpoint, cfg.AccessKeyID, cfg.AccessKeySecret)
410+
Expect(err).ToNot(HaveOccurred())
411+
412+
defer func() {
413+
if err := ossClient.DeleteBucket(newBucketName); err != nil {
414+
if _, ferr := fmt.Fprintf(GinkgoWriter, "cleanup: failed to delete bucket %s: %v\n", newBucketName, err); ferr != nil {
415+
GinkgoWriter.Printf("cleanup: failed to write cleanup message: %v\n", ferr)
416+
}
417+
}
418+
}()
419+
420+
s1, err := integration.RunCli(cliPath, configPath, "ensure-bucket-exists")
421+
Expect(err).ToNot(HaveOccurred())
422+
Expect(s1.ExitCode()).To(BeZero())
423+
424+
exists, err := ossClient.IsBucketExist(newBucketName)
425+
Expect(err).ToNot(HaveOccurred())
426+
Expect(exists).To(BeTrue())
427+
})
428+
352429
It("is idempotent", func() {
353430
s1, err := integration.RunCli(cliPath, configPath, "ensure-bucket-exists")
354431
Expect(err).ToNot(HaveOccurred())

0 commit comments

Comments
 (0)