Skip to content

Commit b359488

Browse files
committed
feat: add DownloadFile method to Client for file retrieval support
Signed-off-by: tbxark <[email protected]>
1 parent cbfe33d commit b359488

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

storage/qiniu/qiniu.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ type Config struct {
2929
}
3030

3131
type Client struct {
32-
mac *qbox.Mac
3332
config *Config
33+
mac *qbox.Mac
3434
}
3535

3636
func NewClient(config *Config) *Client {
@@ -41,9 +41,10 @@ func NewClient(config *Config) *Client {
4141
config.Host = u.Host
4242
}
4343
}
44+
mac := qbox.NewMac(config.AccessKey, config.SecretKey)
4445
return &Client{
45-
mac: qbox.NewMac(config.AccessKey, config.SecretKey),
4646
config: config,
47+
mac: mac,
4748
}
4849
}
4950

@@ -162,3 +163,8 @@ func (n *Client) UploadLocalFile(ctx context.Context, file string, key string) (
162163
Key: ret.Key,
163164
}, nil
164165
}
166+
167+
func (n *Client) DownloadFile(ctx context.Context, key string) (io.ReadCloser, error) {
168+
manager := storage.NewBucketManager(n.mac, &storage.Config{})
169+
return manager.Get(n.config.Bucket, key, nil)
170+
}

storage/s3/s3.go

+4
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,7 @@ func (s *Client) UploadLocalFile(ctx context.Context, file string, key string) (
116116
Key: info.Key,
117117
}, nil
118118
}
119+
120+
func (s *Client) DownloadFile(ctx context.Context, key string) (io.ReadCloser, error) {
121+
return s.client.GetObject(ctx, s.config.Bucket, key, minio.GetObjectOptions{})
122+
}

storage/storage.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ type FileUploader interface {
2323
UploadLocalFile(ctx context.Context, file string, key string) (*models.FileUploadResult, error)
2424
}
2525

26+
type FileDownloader interface {
27+
DownloadFile(ctx context.Context, key string) (io.ReadCloser, error)
28+
}
29+
2630
type Storage interface {
2731
URLHandler
28-
FileUploader
2932
TokenGenerator
33+
FileUploader
34+
FileDownloader
3035
}

0 commit comments

Comments
 (0)