Skip to content

Commit 241e529

Browse files
committed
Removed resumable downloads
1 parent 0b97151 commit 241e529

File tree

2 files changed

+23
-94
lines changed

2 files changed

+23
-94
lines changed

pkg/gcp/gcp.go

+14-59
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ type GCPClient struct {
4545
// client for interacting with the Google Cloud
4646
// Storage APIs.
4747
*gcpStorage.Client
48-
// startRange is the starting read value for
49-
// reading the object from bucket.
50-
StartRange int64
51-
// endRange is the ending read value for
52-
// reading the object from bucket.
53-
EndRange int64
5448
}
5549

5650
// NewClient creates a new GCP storage client
@@ -63,7 +57,7 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*GCPClient, er
6357
return nil, err
6458
}
6559

66-
return &GCPClient{Client: client, StartRange: 0, EndRange: -1}, nil
60+
return &GCPClient{Client: client}, nil
6761
}
6862

6963
// ValidateSecret validates the credential secrets
@@ -76,18 +70,11 @@ func ValidateSecret(secret map[string][]byte, name string) error {
7670
return nil
7771
}
7872

79-
// SetRange sets the startRange and endRange used to read the Object from
80-
// the bucket. It is a helper method for resumable downloads.
81-
func (c *GCPClient) SetRange(start, end int64) {
82-
c.StartRange = start
83-
c.EndRange = end
84-
}
85-
8673
// BucketExists checks if the bucket with the provided name exists.
8774
func (c *GCPClient) BucketExists(ctx context.Context, bucketName string) (bool, error) {
8875
_, err := c.Client.Bucket(bucketName).Attrs(ctx)
8976
if err == gcpStorage.ErrBucketNotExist {
90-
return false, nil
77+
return false, err
9178
}
9279
if err != nil {
9380
return false, err
@@ -97,16 +84,16 @@ func (c *GCPClient) BucketExists(ctx context.Context, bucketName string) (bool,
9784

9885
// ObjectAttributes checks if the object with the provided name exists.
9986
// If it exists the Object attributes are returned.
100-
func (c *GCPClient) ObjectAttributes(ctx context.Context, bucketName, objectName string) (bool, *gcpStorage.ObjectAttrs, error) {
101-
attrs, err := c.Client.Bucket(bucketName).Object(objectName).Attrs(ctx)
87+
func (c *GCPClient) ObjectAttributes(ctx context.Context, bucketName, objectName string) (bool, error) {
88+
_, err := c.Client.Bucket(bucketName).Object(objectName).Attrs(ctx)
10289
// ErrObjectNotExist is returned if the object does not exist
10390
if err == gcpStorage.ErrObjectNotExist {
104-
return false, nil, err
91+
return false, err
10592
}
10693
if err != nil {
107-
return false, nil, err
94+
return false, err
10895
}
109-
return true, attrs, nil
96+
return true, nil
11097
}
11198

11299
// FGetObject gets the object from the bucket and downloads the object locally
@@ -140,65 +127,33 @@ func (c *GCPClient) FGetObject(ctx context.Context, bucketName, objectName, loca
140127
// ObjectExists verifies if object exists and you have permission to access.
141128
// Check if the object exists and if you have permission to access it
142129
// The Object attributes are returned if the Object exists.
143-
exists, attrs, err := c.ObjectAttributes(ctx, bucketName, objectName)
130+
exists, err := c.ObjectAttributes(ctx, bucketName, objectName)
144131
if err != nil {
145132
return err
146133
}
147134
if !exists {
148135
return ErrorObjectDoesNotExist
149136
}
150137

151-
// Write to a temporary file "filename.part.gcp" before saving.
152-
filePartPath := localPath + ".part.gcp"
153-
// If exists, open in append mode. If not create it as a part file.
154-
filePart, err := os.OpenFile(filePartPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
138+
objectFile, err := os.OpenFile(localPath, os.O_CREATE|os.O_WRONLY, 0600)
155139
if err != nil {
156140
return err
157141
}
158142

159-
// If we return early with an error, be sure to close and delete
160-
// filePart. If we have an error along the way there is a chance
161-
// that filePart is somehow damaged, and we should discard it.
162-
closeAndRemove := true
163-
defer func() {
164-
if closeAndRemove {
165-
_ = filePart.Close()
166-
_ = os.Remove(filePartPath)
167-
}
168-
}()
169-
170-
// Issue Stat to get the current offset.
171-
partFileStat, err := filePart.Stat()
172-
if err != nil {
173-
return err
174-
}
175-
176-
// Set the File size request range
177-
// If the part file exists
178-
if partFileStat.Size() > 0 {
179-
c.SetRange(partFileStat.Size(), 0)
180-
}
181-
182143
// Get Object from GCP Bucket
183-
objectReader, err := c.Client.Bucket(bucketName).Object(objectName).NewRangeReader(ctx, c.StartRange, c.EndRange)
144+
objectReader, err := c.Client.Bucket(bucketName).Object(objectName).NewReader(ctx)
184145
if err != nil {
185146
return err
186147
}
187148
defer objectReader.Close()
188149

189-
// Write to the part file.
190-
if _, err := io.CopyN(filePart, objectReader, attrs.Size); err != nil {
191-
return err
192-
}
193-
194-
// Close the file before rename, this is specifically needed for Windows users.
195-
closeAndRemove = false
196-
if err := filePart.Close(); err != nil {
150+
// Write Object to file.
151+
if _, err := io.Copy(objectFile, objectReader); err != nil {
197152
return err
198153
}
199154

200-
// Safely completed. Now commit by renaming to actual filename.
201-
if err := os.Rename(filePartPath, localPath); err != nil {
155+
// Close the file.
156+
if err := objectFile.Close(); err != nil {
202157
return err
203158
}
204159

pkg/gcp/gcp_test.go

+9-35
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ func TestNewClient(t *testing.T) {
118118

119119
func TestBucketExists(t *testing.T) {
120120
gcpClient := &gcp.GCPClient{
121-
Client: client,
122-
StartRange: 0,
123-
EndRange: -1,
121+
Client: client,
124122
}
125123
exists, err := gcpClient.BucketExists(context.Background(), bucketName)
126124
assert.NilError(t, err)
@@ -130,35 +128,28 @@ func TestBucketExists(t *testing.T) {
130128
func TestBucketNotExists(t *testing.T) {
131129
bucket := "notexistsbucket"
132130
gcpClient := &gcp.GCPClient{
133-
Client: client,
134-
StartRange: 0,
135-
EndRange: -1,
131+
Client: client,
136132
}
137133
exists, err := gcpClient.BucketExists(context.Background(), bucket)
138-
assert.NilError(t, err)
134+
assert.Error(t, err, "storage: bucket doesn't exist")
139135
assert.Assert(t, !exists)
140136
}
141137

142138
func TestObjectAttributes(t *testing.T) {
143139
gcpClient := &gcp.GCPClient{
144-
Client: client,
145-
StartRange: 0,
146-
EndRange: -1,
140+
Client: client,
147141
}
148-
exists, objectAttrs, err := gcpClient.ObjectAttributes(context.Background(), bucketName, objectName)
142+
exists, err := gcpClient.ObjectAttributes(context.Background(), bucketName, objectName)
149143
if err == gcpStorage.ErrObjectNotExist {
150144
assert.NilError(t, err)
151145
}
152146
assert.NilError(t, err)
153147
assert.Assert(t, exists)
154-
assert.Assert(t, objectAttrs != nil)
155148
}
156149

157150
func TestListObjects(t *testing.T) {
158151
gcpClient := &gcp.GCPClient{
159-
Client: client,
160-
StartRange: 0,
161-
EndRange: -1,
152+
Client: client,
162153
}
163154
objectInterator := gcpClient.ListObjects(context.Background(), bucketName, nil)
164155
for {
@@ -176,9 +167,7 @@ func TestFGetObject(t *testing.T) {
176167
assert.NilError(t, err)
177168
defer os.RemoveAll(tempDir)
178169
gcpClient := &gcp.GCPClient{
179-
Client: client,
180-
StartRange: 0,
181-
EndRange: -1,
170+
Client: client,
182171
}
183172
localPath := filepath.Join(tempDir, objectName)
184173
err = gcpClient.FGetObject(context.Background(), bucketName, objectName, localPath)
@@ -193,9 +182,7 @@ func TestFGetObjectNotExists(t *testing.T) {
193182
assert.NilError(t, err)
194183
defer os.RemoveAll(tempDir)
195184
gcpClient := &gcp.GCPClient{
196-
Client: client,
197-
StartRange: 0,
198-
EndRange: -1,
185+
Client: client,
199186
}
200187
localPath := filepath.Join(tempDir, object)
201188
err = gcpClient.FGetObject(context.Background(), bucketName, object, localPath)
@@ -209,27 +196,14 @@ func TestFGetObjectDirectoryIsFileName(t *testing.T) {
209196
defer os.RemoveAll(tempDir)
210197
assert.NilError(t, err)
211198
gcpClient := &gcp.GCPClient{
212-
Client: client,
213-
StartRange: 0,
214-
EndRange: -1,
199+
Client: client,
215200
}
216201
err = gcpClient.FGetObject(context.Background(), bucketName, objectName, tempDir)
217202
if err != io.EOF {
218203
assert.Error(t, err, "filename is a directory")
219204
}
220205
}
221206

222-
func TestSetRange(t *testing.T) {
223-
gcpClient := &gcp.GCPClient{
224-
Client: client,
225-
StartRange: 0,
226-
EndRange: -1,
227-
}
228-
gcpClient.SetRange(2, 5)
229-
assert.Equal(t, gcpClient.StartRange, int64(2))
230-
assert.Equal(t, gcpClient.EndRange, int64(5))
231-
}
232-
233207
func TestValidateSecret(t *testing.T) {
234208
t.Parallel()
235209
testCases := []struct {

0 commit comments

Comments
 (0)