@@ -45,12 +45,6 @@ type GCPClient struct {
45
45
// client for interacting with the Google Cloud
46
46
// Storage APIs.
47
47
* 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
54
48
}
55
49
56
50
// NewClient creates a new GCP storage client
@@ -63,7 +57,7 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*GCPClient, er
63
57
return nil , err
64
58
}
65
59
66
- return & GCPClient {Client : client , StartRange : 0 , EndRange : - 1 }, nil
60
+ return & GCPClient {Client : client }, nil
67
61
}
68
62
69
63
// ValidateSecret validates the credential secrets
@@ -76,18 +70,11 @@ func ValidateSecret(secret map[string][]byte, name string) error {
76
70
return nil
77
71
}
78
72
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
-
86
73
// BucketExists checks if the bucket with the provided name exists.
87
74
func (c * GCPClient ) BucketExists (ctx context.Context , bucketName string ) (bool , error ) {
88
75
_ , err := c .Client .Bucket (bucketName ).Attrs (ctx )
89
76
if err == gcpStorage .ErrBucketNotExist {
90
- return false , nil
77
+ return false , err
91
78
}
92
79
if err != nil {
93
80
return false , err
@@ -97,16 +84,16 @@ func (c *GCPClient) BucketExists(ctx context.Context, bucketName string) (bool,
97
84
98
85
// ObjectAttributes checks if the object with the provided name exists.
99
86
// 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 )
102
89
// ErrObjectNotExist is returned if the object does not exist
103
90
if err == gcpStorage .ErrObjectNotExist {
104
- return false , nil , err
91
+ return false , err
105
92
}
106
93
if err != nil {
107
- return false , nil , err
94
+ return false , err
108
95
}
109
- return true , attrs , nil
96
+ return true , nil
110
97
}
111
98
112
99
// 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
140
127
// ObjectExists verifies if object exists and you have permission to access.
141
128
// Check if the object exists and if you have permission to access it
142
129
// 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 )
144
131
if err != nil {
145
132
return err
146
133
}
147
134
if ! exists {
148
135
return ErrorObjectDoesNotExist
149
136
}
150
137
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 )
155
139
if err != nil {
156
140
return err
157
141
}
158
142
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
-
182
143
// 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 )
184
145
if err != nil {
185
146
return err
186
147
}
187
148
defer objectReader .Close ()
188
149
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 {
197
152
return err
198
153
}
199
154
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 {
202
157
return err
203
158
}
204
159
0 commit comments