4
4
"context"
5
5
"fmt"
6
6
"io"
7
+ "io/ioutil"
7
8
"log"
8
9
"mime/multipart"
9
10
"time"
@@ -15,7 +16,7 @@ type CloudStorageService interface {
15
16
GetInstance () * storage.Client
16
17
CreateBucket (string ) error
17
18
UploadFile (multipart.File , string , string ) error
18
- DownloadFile () error
19
+ DownloadFile (string , string ) ([] byte , error )
19
20
}
20
21
21
22
type storage struct {
@@ -24,7 +25,7 @@ type storage struct {
24
25
bucket string
25
26
}
26
27
27
- func NewCloudStorageClient (projectID , bucketName string ) (CloudStorageService , error ) {
28
+ func NewCloudStorageClient (projectID string ) (CloudStorageService , error ) {
28
29
// Creates Google Cloud Storage client agent
29
30
client , err := storage .NewClient (context .Background ())
30
31
if err != nil {
@@ -33,7 +34,7 @@ func NewCloudStorageClient(projectID, bucketName string) (CloudStorageService, e
33
34
34
35
defer client .Close ()
35
36
36
- return & storage {client , projectID , bucketName }, nil
37
+ return & storage {client , projectID }, nil
37
38
}
38
39
39
40
func (g * storage ) GetInstance () * storage.Client {
@@ -79,6 +80,23 @@ func (g *storage) UploadFile(file multipart.File, folderName, fileName string) e
79
80
return nil
80
81
}
81
82
82
- func (g * storage ) DownloadFile () error {
83
- return nil
83
+ func (g * storage ) DownloadFile (bucketName , fileName string ) ([]byte , error ) {
84
+ ctx := context .Background ()
85
+
86
+ ctx , cancel := context .WithTimeout (ctx , time .Second * 50 )
87
+ defer cancel ()
88
+
89
+ rc , err := g .client .Bucket (bucketName ).Object (fileName ).NewReader (ctx )
90
+ if err != nil {
91
+ return nil , err
92
+ }
93
+
94
+ defer rc .Close ()
95
+
96
+ dataFile , err := ioutil .ReadAll (rc )
97
+ if err != nil {
98
+ return nil , err
99
+ }
100
+
101
+ return dataFile nil
84
102
}
0 commit comments