You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.8 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package storage
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
"uploader/config"
)
type ObsStorage struct {
Config *config.Obs
}
func NewObsStorage(config *config.Obs) *ObsStorage {
return &ObsStorage{Config: config}
}
func (s *ObsStorage) getClient() (*obs.ObsClient, error) {
var obsClient, err = obs.New(s.Config.AccessKey, s.Config.SecretKey, s.Config.Endpoint)
if err != nil {
return nil, err
}
return obsClient, nil
}
func (s *ObsStorage) Upload(filePath, destPath string) (string, error) {
client, err := s.getClient()
if err != nil {
return "", err
}
input := &obs.UploadFileInput{}
input.Bucket = s.Config.Bucket
input.Key = destPath
input.UploadFile = filePath // localfile为待上传的本地文件路径需要指定到具体的文件名
input.EnableCheckpoint = true // 开启断点续传模式
input.PartSize = 50 * 1024 * 1024 // 指定分段大小
input.TaskNum = 4 // 指定分段上传时的最大并发数
output, err := client.UploadFile(input)
if err == nil {
fmt.Printf("RequestId:%s\n", output.RequestId)
fmt.Printf("ETag:%s\n", output.ETag)
} else if obsError, ok := err.(obs.ObsError); ok {
fmt.Printf("Code:%s\n", obsError.Code)
fmt.Printf("Message:%s\n", obsError.Message)
}
if err != nil {
return "", err
}
return s.Config.Domain + "/" + destPath, err
}
func (s *ObsStorage) Delete(objectName string) error {
client, err := s.getClient()
if err != nil {
return err
}
input := &obs.DeleteObjectInput{}
input.Bucket = s.Config.Bucket
input.Key = objectName
output, err := client.DeleteObject(input)
if err == nil {
fmt.Printf("RequestId:%s\n", output.RequestId)
} else {
if obsError, ok := err.(obs.ObsError); ok {
fmt.Println(obsError.Code)
fmt.Println(obsError.Message)
} else {
fmt.Println(err)
}
}
return err
}