mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
2 years ago
|
package storage
|
||
2 years ago
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
|
||
2 years ago
|
"github.com/Masterminds/semver/v3"
|
||
2 years ago
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||
2 years ago
|
"github.com/rocboss/paopao-ce/internal/core"
|
||
2 years ago
|
"github.com/sirupsen/logrus"
|
||
2 years ago
|
)
|
||
|
|
||
2 years ago
|
var (
|
||
|
_ core.ObjectStorageService = (*aliossServant)(nil)
|
||
|
_ core.VersionInfo = (*aliossServant)(nil)
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
type aliossServant struct {
|
||
|
bucket *oss.Bucket
|
||
|
domain string
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (s *aliossServant) Name() string {
|
||
2 years ago
|
return "AliOSS"
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *aliossServant) Version() *semver.Version {
|
||
2 years ago
|
return semver.MustParse("v0.1.0")
|
||
|
}
|
||
|
|
||
2 years ago
|
func (s *aliossServant) PutObject(objectKey string, reader io.Reader, objectSize int64, contentType string) (string, error) {
|
||
|
err := s.bucket.PutObject(objectKey, reader, oss.ContentLength(objectSize), oss.ContentType(contentType))
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return s.domain + objectKey, nil
|
||
|
}
|
||
|
|
||
|
func (s *aliossServant) SignURL(objectKey string, expiredInSec int64) (string, error) {
|
||
|
signedURL, err := s.bucket.SignURL(objectKey, oss.HTTPGet, expiredInSec)
|
||
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("client.SignURL err: %v", err)
|
||
2 years ago
|
return "", err
|
||
|
}
|
||
|
|
||
|
ur, err := url.Parse(signedURL)
|
||
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("url.Parse err: %v", err)
|
||
2 years ago
|
return "", err
|
||
|
}
|
||
|
|
||
|
epath, err := url.PathUnescape(ur.Path)
|
||
|
if err != nil {
|
||
2 years ago
|
logrus.Errorf("url.PathUnescape err: %v", err)
|
||
2 years ago
|
return "", err
|
||
|
}
|
||
|
|
||
|
ur.Path, ur.RawPath = epath, epath
|
||
|
return ur.String(), nil
|
||
|
}
|
||
|
|
||
|
func (s *aliossServant) ObjectURL(objetKey string) string {
|
||
|
return s.domain + objetKey
|
||
|
}
|
||
|
|
||
|
func (s *aliossServant) ObjectKey(objectUrl string) string {
|
||
|
return strings.Replace(objectUrl, s.domain, "", -1)
|
||
|
}
|