From a87fc7d09bda8eb8b2a5755b0656871ab5c0e604 Mon Sep 17 00:00:00 2001 From: Alex Pardoe Date: Thu, 12 Sep 2019 18:18:47 +0100 Subject: [PATCH] Use the B2 'list' endpoint to determine file ID (#8169) - More effective deletion and checking for existence. - Rever Dockerfile. - Add a 'GOPROXY' to the Dockerfile to workaround Apache issues. --- Dockerfile | 1 + cmd/gateway/b2/gateway-b2.go | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0164cd73b..4b5f817c9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ LABEL maintainer="MinIO Inc " ENV GOPATH /go ENV CGO_ENABLED 0 ENV GO111MODULE on +ENV GOPROXY https://proxy.golang.org RUN \ apk add --no-cache git && \ diff --git a/cmd/gateway/b2/gateway-b2.go b/cmd/gateway/b2/gateway-b2.go index 72051bc18..36169f867 100644 --- a/cmd/gateway/b2/gateway-b2.go +++ b/cmd/gateway/b2/gateway-b2.go @@ -22,7 +22,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "net/http" "strings" @@ -460,13 +459,23 @@ func (l *b2Objects) GetObjectInfo(ctx context.Context, bucket string, object str if err != nil { return objInfo, err } - f, err := bkt.DownloadFileByName(l.ctx, object, 0, 1) + + f, _, err := bkt.ListFileNames(l.ctx, 1, object, "", "") if err != nil { logger.LogIf(ctx, err) return objInfo, b2ToObjectError(err, bucket, object) } - f.Close() - fi, err := bkt.File(f.ID, object).GetFileInfo(l.ctx) + + // B2's list will return the next item in the bucket if the object doesn't + // exist so we need to perform a name check too + if len(f) != 1 || (len(f) == 1 && f[0].Name != object) { + return objInfo, minio.ObjectNotFound{ + Bucket: bucket, + Object: object, + } + } + + fi, err := bkt.File(f[0].ID, object).GetFileInfo(l.ctx) if err != nil { logger.LogIf(ctx, err) return objInfo, b2ToObjectError(err, bucket, object) @@ -474,7 +483,7 @@ func (l *b2Objects) GetObjectInfo(ctx context.Context, bucket string, object str return minio.ObjectInfo{ Bucket: bucket, Name: object, - ETag: minio.ToS3ETag(f.ID), + ETag: minio.ToS3ETag(f[0].ID), Size: fi.Size, ModTime: fi.Timestamp, ContentType: fi.ContentType, @@ -595,14 +604,10 @@ func (l *b2Objects) DeleteObject(ctx context.Context, bucket string, object stri if err != nil { return err } - reader, err := bkt.DownloadFileByName(l.ctx, object, 0, 1) - if err != nil { - logger.LogIf(ctx, err) - return b2ToObjectError(err, bucket, object) - } - io.Copy(ioutil.Discard, reader) - reader.Close() - err = bkt.File(reader.ID, object).DeleteFileVersion(l.ctx) + + // If we hide the file we'll conform to B2's versioning policy, it also + // saves an additional call to check if the file exists first + _, err = bkt.HideFile(l.ctx, object) logger.LogIf(ctx, err) return b2ToObjectError(err, bucket, object) }