Skip to content

Commit 300551c

Browse files
authored
PBM-1498 hmac support for gcs fix (#1128)
1 parent 38dc5aa commit 300551c

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

pbm/storage/gcs/download.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package gcs
22

33
import (
44
"container/heap"
5-
"context"
65
"io"
76
"net/http"
8-
"time"
97

108
"google.golang.org/api/googleapi"
119

@@ -66,10 +64,7 @@ func (g *GCS) newPartReader(fname string, fsize int64, chunkSize int) *storage.P
6664
Buf: make([]byte, 32*1024),
6765
L: g.log,
6866
GetChunk: func(fname string, arena *storage.Arena, cli interface{}, start, end int64) (io.ReadCloser, error) {
69-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
70-
defer cancel()
71-
72-
return cli.(gcsClient).getPartialObject(ctx, fname, start, end-start+1)
67+
return cli.(gcsClient).getPartialObject(fname, arena, start, end-start+1)
7368
},
7469
GetSess: func() (interface{}, error) {
7570
return g.client, nil // re-use the already-initialized client

pbm/storage/gcs/gcs.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gcs
22

33
import (
4-
"context"
54
"io"
65
"path"
76
"reflect"
@@ -67,7 +66,7 @@ type gcsClient interface {
6766
list(prefix, suffix string) ([]storage.FileInfo, error)
6867
delete(name string) error
6968
copy(src, dst string) error
70-
getPartialObject(ctx context.Context, name string, start, length int64) (io.ReadCloser, error)
69+
getPartialObject(name string, buf *storage.Arena, start, length int64) (io.ReadCloser, error)
7170
}
7271

7372
type GCS struct {

pbm/storage/gcs/google_client.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"path"
99
"strings"
10+
"time"
1011

1112
storagegcs "cloud.google.com/go/storage"
1213
"github.com/googleapis/gax-go/v2"
@@ -216,7 +217,10 @@ func (g googleClient) copy(src, dst string) error {
216217
return err
217218
}
218219

219-
func (g googleClient) getPartialObject(ctx context.Context, name string, start, length int64) (io.ReadCloser, error) {
220+
func (g googleClient) getPartialObject(name string, buf *storage.Arena, start, length int64) (io.ReadCloser, error) {
221+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
222+
defer cancel()
223+
220224
obj := g.bucketHandle.Object(path.Join(g.opts.Prefix, name))
221225
reader, err := obj.NewRangeReader(ctx, start, length)
222226
if err != nil {
@@ -226,5 +230,13 @@ func (g googleClient) getPartialObject(ctx context.Context, name string, start,
226230

227231
return nil, storage.GetObjError{Err: err}
228232
}
229-
return reader, nil
233+
234+
ch := buf.GetSpan()
235+
_, err = io.CopyBuffer(ch, reader, buf.CpBuf)
236+
if err != nil {
237+
ch.Close()
238+
return nil, errors.Wrap(err, "copy")
239+
}
240+
reader.Close()
241+
return ch, nil
230242
}

pbm/storage/gcs/hmac_client.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path"
77
"runtime"
88
"strings"
9+
"time"
910

1011
"github.com/minio/minio-go/v7"
1112
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -181,7 +182,10 @@ func (h hmacClient) copy(src, dst string) error {
181182
return err
182183
}
183184

184-
func (h hmacClient) getPartialObject(ctx context.Context, name string, start, length int64) (io.ReadCloser, error) {
185+
func (h hmacClient) getPartialObject(name string, buf *storage.Arena, start, length int64) (io.ReadCloser, error) {
186+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
187+
defer cancel()
188+
185189
objectName := path.Join(h.opts.Prefix, name)
186190

187191
opts := minio.GetObjectOptions{}
@@ -200,6 +204,14 @@ func (h hmacClient) getPartialObject(ctx context.Context, name string, start, le
200204

201205
return nil, storage.GetObjError{Err: err}
202206
}
207+
defer object.Close()
208+
209+
ch := buf.GetSpan()
210+
_, err = io.CopyBuffer(ch, object, buf.CpBuf)
211+
if err != nil {
212+
ch.Close()
213+
return nil, errors.Wrap(err, "copy")
214+
}
203215

204-
return object, nil
216+
return ch, nil
205217
}

0 commit comments

Comments
 (0)