Skip to content

Commit

Permalink
Upgrade qiniu storage API
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytaxii committed Jan 12, 2025
1 parent 2ec2c9c commit 785b6ed
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cmd/faust/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func TryToLoadConfig(file string) (*AppConfig, error) {
viper.SetConfigType(strings.TrimPrefix(ext, "."))
viper.SetConfigName(strings.TrimSuffix(fileName, ext))
viper.AddConfigPath(cfgPath)
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/faust")
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
viper.AddConfigPath(path.Join(home, ".faust"))
viper.AddConfigPath("/etc/faust")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
return cfg, nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/faust/app/faust.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func runUpload(ctx context.Context, opts *options.AppOptions) error {
log.Errorf("error uploading image: %v", err)
return err
}
lf["bucket"] = res.Bucket
lf["key"] = res.Key
lf["size"] = units.HumanSize(float64(res.Size))
lf["hash"] = res.Hash
lf["image_url"] = res.URLs
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/faust/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ func (o *AppOptions) Flags() []cli.Flag {
&cli.PathFlag{
Name: "config-file",
Aliases: []string{"c"},
Usage: "file path of configuration to be loaded",
Usage: "`file` path of configuration to be loaded",
Destination: &o.ConfigFile,
},
&cli.PathFlag{
Category: "upload",
Name: "image",
Aliases: []string{"i"},
Usage: "file path of image to be uploaded",
Usage: "`file` path of image to be uploaded",
Destination: &o.ImagePath,
},
&cli.PathFlag{
Category: "upload",
Name: "cert",
Usage: "file path of certificate to be uploaded",
Usage: "`file` path of certificate to be uploaded",
Destination: &o.CertPath,
},
&cli.PathFlag{
Category: "upload",
Name: "key",
Usage: "file path of private key to be uploaded",
Usage: "`file` path of private key to be uploaded",
Destination: &o.KeyPath,
},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/crazytaxii/faust

go 1.21
go 1.23

require (
github.com/docker/go-units v0.5.0
Expand Down
54 changes: 25 additions & 29 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

_ "github.com/crazytaxii/faust/pkg/image"
"github.com/crazytaxii/faust/pkg/service/utils"
qh "github.com/qiniu/go-sdk/v7/storagev2/http_client"
qu "github.com/qiniu/go-sdk/v7/storagev2/uploader"

"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/auth/qbox"
Expand All @@ -35,10 +37,10 @@ const (

type (
ImageUploadResponse struct {
Key string
Hash string
Size uint64
URLs []string
Bucket string
Key string
Size int64
URLs []string
}
CertsUploadResponse struct {
CommonName string
Expand All @@ -61,14 +63,8 @@ type (
QiniuService struct {
config *QServiceConfig
credentials *qbox.Mac
uploader *storage.FormUploader
bucketManager *storage.BucketManager
}
kodoPutRet struct {
Key string `json:"key"`
Hash string `json:"hash"`
Fsize uint64 `json:"fsize"`
Bucket string `json:"bucket"`
uploader *qu.UploadManager
}
)

Expand Down Expand Up @@ -116,13 +112,16 @@ func (c *QServiceConfig) MakePutPolicy() *storage.PutPolicy {
}

func NewQiniuService(cfg *QServiceConfig) *QiniuService {
sc := &storage.Config{}
mac := auth.New(cfg.AccessKey, cfg.SecretKey)
return &QiniuService{
config: cfg,
credentials: mac,
uploader: storage.NewFormUploader(sc),
bucketManager: storage.NewBucketManager(mac, sc),
bucketManager: storage.NewBucketManager(mac, &storage.Config{}),
uploader: qu.NewUploadManager(&qu.UploadManagerOptions{
Options: qh.Options{
Credentials: mac,
},
}),
}
}

Expand All @@ -143,16 +142,13 @@ func (s *QiniuService) UploadImage(ctx context.Context, name string) (*ImageUplo
return nil, err
}

if _, err := f.Seek(0, io.SeekStart); err != nil {
return nil, err
}

pp := s.config.MakePutPolicy()
token := pp.UploadToken(s.credentials)
ret := &kodoPutRet{}
// doc: https://developer.qiniu.com/kodo/1238/go#upload-file
key := utils.GenUploadKey(format)
// upload file
if err := s.uploader.Put(ctx, ret, token, key, f, fi.Size(), &storage.PutExtra{}); err != nil {
if err := s.uploader.UploadFile(ctx, name, &qu.ObjectOptions{
BucketName: s.config.Bucket,
ObjectName: &key,
FileName: key,
}, nil); err != nil {
return nil, err
}

Expand All @@ -163,14 +159,14 @@ func (s *QiniuService) UploadImage(ctx context.Context, name string) (*ImageUplo
}
urls := make([]string, len(domainInfo))
for i, domain := range domainInfo {
urls[i] = fmt.Sprintf("https://%s/%s", domain.Domain, ret.Key)
urls[i] = fmt.Sprintf("https://%s/%s", domain.Domain, key)
}

return &ImageUploadResponse{
Key: ret.Key,
Hash: ret.Hash,
Size: ret.Fsize,
URLs: urls,
Bucket: s.config.Bucket,
Key: key,
Size: fi.Size(),
URLs: urls,
}, nil
}

Expand Down Expand Up @@ -216,7 +212,7 @@ func (s *QiniuService) UploadCerts(ctx context.Context, keyPath, certPath string
Key: string(rawKeyData),
CertChain: string(rawCertData),
}
if _, err = postRequest(ctx, s.credentials, "/sslcert", reqBody); err != nil {
if _, err := postRequest(ctx, s.credentials, "/sslcert", reqBody); err != nil {
return nil, err
}
return &CertsUploadResponse{
Expand Down
8 changes: 0 additions & 8 deletions pkg/utils/utils.go

This file was deleted.

0 comments on commit 785b6ed

Please sign in to comment.