From b64275f20123283f539dc85fa0b7c4e5110333eb Mon Sep 17 00:00:00 2001 From: wuhuizuo Date: Fri, 15 Dec 2023 12:42:20 +0800 Subject: [PATCH] fix(dl): fix yaml config and get object logic (#60) Signed-off-by: wuhuizuo Signed-off-by: wuhuizuo --- dl/ks3.go | 21 +++++++++++++++------ dl/pkg/ks3/cfg.go | 11 ++++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/dl/ks3.go b/dl/ks3.go index 587495e0..6d0efda2 100644 --- a/dl/ks3.go +++ b/dl/ks3.go @@ -4,7 +4,9 @@ import ( "context" "io" "log" + "net/url" "os" + "path/filepath" "github.com/ks3sdklib/aws-sdk-go/aws" "github.com/ks3sdklib/aws-sdk-go/aws/credentials" @@ -23,11 +25,11 @@ type ks3srvc struct { } func newKS3Client(cfg *pkgks3.Config) *s3.S3 { - var cre = credentials.NewStaticCredentials(cfg.S3AccessKey, cfg.S3SecretKey, "") + var cre = credentials.NewStaticCredentials(cfg.AccessKey, cfg.SecretKey, "") awsConfig := aws.Config{ - Region: cfg.S3Region, // Ref: https://docs.ksyun.com/documents/6761 + Region: cfg.Region, // Ref: https://docs.ksyun.com/documents/6761 Credentials: cre, - Endpoint: cfg.S3Endpoint, // Ref: https://docs.ksyun.com/documents/6761 + Endpoint: cfg.Endpoint, // Ref: https://docs.ksyun.com/documents/6761 DisableSSL: true, LogLevel: 0, LogHTTPBody: false, @@ -65,9 +67,16 @@ func (s *ks3srvc) DownloadObject(ctx context.Context, p *ks3.DownloadObjectPaylo return nil, nil, err } - res = &ks3.DownloadObjectResult{ - Length: *getObjectOutput.ContentLength, - ContentDisposition: *getObjectOutput.ContentDisposition, + res = &ks3.DownloadObjectResult{} + if getObjectOutput != nil { + if getObjectOutput.ContentLength != nil { + res.Length = *getObjectOutput.ContentLength + } + if getObjectOutput.ContentDisposition != nil { + res.ContentDisposition = *getObjectOutput.ContentDisposition + } else { + res.ContentDisposition = `attachment; filename*=UTF-8''` + url.QueryEscape(filepath.Base(p.Key)) + } } return res, getObjectOutput.Body, nil diff --git a/dl/pkg/ks3/cfg.go b/dl/pkg/ks3/cfg.go index 04359967..5fa9203d 100644 --- a/dl/pkg/ks3/cfg.go +++ b/dl/pkg/ks3/cfg.go @@ -1,11 +1,8 @@ package ks3 type Config struct { - S3Region string - S3Bucket string - S3Endpoint string - S3AccessKey string - S3SecretKey string - BaseUrl string - FileserverUrl string + Region string `yaml:"region,omitempty" json:"region,omitempty"` + Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"` + AccessKey string `yaml:"access_key,omitempty" json:"access_key,omitempty"` + SecretKey string `yaml:"secret_key,omitempty" json:"secret_key,omitempty"` }