2023-02-13 19:36:48 +08:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-01-24 11:28:26 +08:00
|
|
|
"net/url"
|
2023-06-03 14:38:28 +08:00
|
|
|
"strings"
|
2024-01-29 21:12:29 +08:00
|
|
|
"time"
|
2023-02-13 19:36:48 +08:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
2023-02-24 00:02:51 +08:00
|
|
|
s3config "github.com/aws/aws-sdk-go-v2/config"
|
2023-02-13 19:36:48 +08:00
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
|
|
|
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
2024-01-29 23:15:47 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-02-13 19:36:48 +08:00
|
|
|
)
|
|
|
|
|
2024-01-29 21:12:29 +08:00
|
|
|
const LinkLifetime = 24 * time.Hour
|
|
|
|
|
2023-02-24 00:02:51 +08:00
|
|
|
type Config struct {
|
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
Bucket string
|
|
|
|
EndPoint string
|
|
|
|
Region string
|
|
|
|
URLPrefix string
|
2023-04-01 15:28:00 +08:00
|
|
|
URLSuffix string
|
2024-01-29 21:12:29 +08:00
|
|
|
PreSign bool
|
2023-02-24 00:02:51 +08:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:36:48 +08:00
|
|
|
type Client struct {
|
2023-02-24 00:02:51 +08:00
|
|
|
Client *awss3.Client
|
|
|
|
Config *Config
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
|
|
|
|
2023-02-24 00:02:51 +08:00
|
|
|
func NewClient(ctx context.Context, config *Config) (*Client, error) {
|
2023-06-03 14:38:28 +08:00
|
|
|
// For some s3-compatible object stores, converting the hostname is not required,
|
|
|
|
// and not setting this option will result in not being able to access the corresponding object store address.
|
|
|
|
// But Aliyun OSS should disable this option
|
|
|
|
hostnameImmutable := true
|
|
|
|
if strings.HasSuffix(config.EndPoint, "aliyuncs.com") {
|
|
|
|
hostnameImmutable = false
|
|
|
|
}
|
2023-03-18 22:34:22 +08:00
|
|
|
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) {
|
2023-02-13 19:36:48 +08:00
|
|
|
return aws.Endpoint{
|
2023-06-03 14:38:28 +08:00
|
|
|
URL: config.EndPoint,
|
|
|
|
SigningRegion: config.Region,
|
|
|
|
HostnameImmutable: hostnameImmutable,
|
2023-02-13 19:36:48 +08:00
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
|
2023-02-27 21:26:50 +08:00
|
|
|
awsConfig, err := s3config.LoadDefaultConfig(ctx,
|
2023-02-24 00:02:51 +08:00
|
|
|
s3config.WithEndpointResolverWithOptions(resolver),
|
|
|
|
s3config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(config.AccessKey, config.SecretKey, "")),
|
2024-01-04 19:08:54 +08:00
|
|
|
s3config.WithRegion(config.Region),
|
2023-02-13 19:36:48 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:26:50 +08:00
|
|
|
client := awss3.NewFromConfig(awsConfig)
|
2023-02-13 19:36:48 +08:00
|
|
|
|
|
|
|
return &Client{
|
2023-02-24 00:02:51 +08:00
|
|
|
Client: client,
|
|
|
|
Config: config,
|
2023-02-13 19:36:48 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-24 00:02:51 +08:00
|
|
|
func (client *Client) UploadFile(ctx context.Context, filename string, fileType string, src io.Reader) (string, error) {
|
2023-02-13 19:36:48 +08:00
|
|
|
uploader := manager.NewUploader(client.Client)
|
2023-11-24 21:55:09 +08:00
|
|
|
putInput := awss3.PutObjectInput{
|
2023-02-24 00:02:51 +08:00
|
|
|
Bucket: aws.String(client.Config.Bucket),
|
2023-03-06 20:04:19 +08:00
|
|
|
Key: aws.String(filename),
|
2023-02-13 19:36:48 +08:00
|
|
|
Body: src,
|
|
|
|
ContentType: aws.String(fileType),
|
2023-11-23 23:20:11 +08:00
|
|
|
}
|
2023-11-24 21:55:09 +08:00
|
|
|
// Set ACL according to if url prefix is set.
|
2023-11-23 23:20:11 +08:00
|
|
|
if client.Config.URLPrefix == "" {
|
2023-11-24 21:55:09 +08:00
|
|
|
putInput.ACL = types.ObjectCannedACL(*aws.String("public-read"))
|
2023-11-23 23:20:11 +08:00
|
|
|
}
|
2023-11-24 21:55:09 +08:00
|
|
|
uploadOutput, err := uploader.Upload(ctx, &putInput)
|
2023-02-13 19:36:48 +08:00
|
|
|
if err != nil {
|
2023-02-15 22:54:46 +08:00
|
|
|
return "", err
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
2023-02-27 21:26:50 +08:00
|
|
|
|
|
|
|
link := uploadOutput.Location
|
2023-03-04 20:06:32 +08:00
|
|
|
// If url prefix is set, use it as the file link.
|
|
|
|
if client.Config.URLPrefix != "" {
|
2024-01-24 11:28:26 +08:00
|
|
|
parts := strings.Split(filename, "/")
|
|
|
|
for i := range parts {
|
|
|
|
parts[i] = url.PathEscape(parts[i])
|
|
|
|
}
|
|
|
|
link = fmt.Sprintf("%s/%s%s", client.Config.URLPrefix, strings.Join(parts, "/"), client.Config.URLSuffix)
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
2023-03-04 20:06:32 +08:00
|
|
|
if link == "" {
|
2023-09-17 22:55:13 +08:00
|
|
|
return "", errors.New("failed to get file link")
|
2023-03-04 20:06:32 +08:00
|
|
|
}
|
2024-01-29 21:12:29 +08:00
|
|
|
if client.Config.PreSign {
|
|
|
|
return client.PreSignLink(ctx, link)
|
|
|
|
}
|
2023-02-15 22:54:46 +08:00
|
|
|
return link, nil
|
2023-02-13 19:36:48 +08:00
|
|
|
}
|
2024-01-29 21:12:29 +08:00
|
|
|
|
|
|
|
// PreSignLink generates a pre-signed URL for the given sourceLink.
|
|
|
|
// If the link does not belong to the configured storage endpoint, it is returned as-is.
|
|
|
|
// If the link belongs to the storage, the function generates a pre-signed URL using the AWS S3 client.
|
|
|
|
func (client *Client) PreSignLink(ctx context.Context, sourceLink string) (string, error) {
|
|
|
|
u, err := url.Parse(sourceLink)
|
|
|
|
if err != nil {
|
2024-01-29 23:15:47 +08:00
|
|
|
return "", errors.Wrapf(err, "parse URL")
|
2024-01-29 21:12:29 +08:00
|
|
|
}
|
|
|
|
// if link doesn't belong to storage, then return as-is.
|
|
|
|
// the empty hostname is corner-case for AWS native endpoint.
|
|
|
|
if client.Config.EndPoint != "" && !strings.Contains(client.Config.EndPoint, u.Hostname()) {
|
|
|
|
return sourceLink, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := u.Path
|
|
|
|
if prefixLen := len(client.Config.URLPrefix); len(filename) >= prefixLen {
|
|
|
|
filename = filename[prefixLen:]
|
|
|
|
}
|
|
|
|
if suffixLen := len(client.Config.URLSuffix); len(filename) >= suffixLen {
|
|
|
|
filename = filename[:len(filename)-suffixLen]
|
|
|
|
}
|
|
|
|
filename = strings.Trim(filename, "/")
|
|
|
|
if strings.HasPrefix(filename, client.Config.Bucket) {
|
|
|
|
filename = strings.Trim(filename[len(client.Config.Bucket):], "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := awss3.NewPresignClient(client.Client).PresignGetObject(ctx, &awss3.GetObjectInput{
|
|
|
|
Bucket: aws.String(client.Config.Bucket),
|
|
|
|
Key: aws.String(filename),
|
|
|
|
}, awss3.WithPresignExpires(LinkLifetime))
|
|
|
|
if err != nil {
|
2024-01-29 23:15:47 +08:00
|
|
|
return "", errors.Wrapf(err, "pre-sign link")
|
2024-01-29 21:12:29 +08:00
|
|
|
}
|
|
|
|
return req.URL, nil
|
|
|
|
}
|