@@ -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.
8774func (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,20 +84,19 @@ 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
113- // A part file is created so the download can be resumable.
114100func (c * GCPClient ) FGetObject (ctx context.Context , bucketName , objectName , localPath string ) error {
115101 // Verify if destination already exists.
116102 dirStatus , err := os .Stat (localPath )
@@ -140,65 +126,33 @@ func (c *GCPClient) FGetObject(ctx context.Context, bucketName, objectName, loca
140126 // ObjectExists verifies if object exists and you have permission to access.
141127 // Check if the object exists and if you have permission to access it
142128 // The Object attributes are returned if the Object exists.
143- exists , attrs , err := c .ObjectAttributes (ctx , bucketName , objectName )
129+ exists , err := c .ObjectAttributes (ctx , bucketName , objectName )
144130 if err != nil {
145131 return err
146132 }
147133 if ! exists {
148134 return ErrorObjectDoesNotExist
149135 }
150136
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 )
137+ objectFile , err := os .OpenFile (localPath , os .O_CREATE | os .O_WRONLY , 0600 )
155138 if err != nil {
156139 return err
157140 }
158141
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-
182142 // Get Object from GCP Bucket
183- objectReader , err := c .Client .Bucket (bucketName ).Object (objectName ).NewRangeReader (ctx , c . StartRange , c . EndRange )
143+ objectReader , err := c .Client .Bucket (bucketName ).Object (objectName ).NewReader (ctx )
184144 if err != nil {
185145 return err
186146 }
187147 defer objectReader .Close ()
188148
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 {
149+ // Write Object to file.
150+ if _ , err := io .Copy (objectFile , objectReader ); err != nil {
197151 return err
198152 }
199153
200- // Safely completed. Now commit by renaming to actual filename .
201- if err := os . Rename ( filePartPath , localPath ); err != nil {
154+ // Close the file .
155+ if err := objectFile . Close ( ); err != nil {
202156 return err
203157 }
204158
0 commit comments