feat: add scripts (#525)

This commit is contained in:
Xinwei Xiong
2023-07-13 11:37:23 +08:00
committed by GitHub
parent eb7953cacb
commit 7bf8a898e2
56 changed files with 2049 additions and 442 deletions
+19 -6
View File
@@ -6,13 +6,15 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/google/uuid"
"path"
"strings"
"time"
"github.com/google/uuid"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
)
func New(impl s3.Interface) *Controller {
@@ -56,7 +58,13 @@ func (c *Controller) GetHashObject(ctx context.Context, hash string) (*s3.Object
return c.impl.StatObject(ctx, c.HashPath(hash))
}
func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64, expire time.Duration, maxParts int) (*InitiateUploadResult, error) {
func (c *Controller) InitiateUpload(
ctx context.Context,
hash string,
size int64,
expire time.Duration,
maxParts int,
) (*InitiateUploadResult, error) {
defer log.ZDebug(ctx, "return")
if size < 0 {
return nil, errors.New("invalid size")
@@ -239,6 +247,11 @@ func (c *Controller) IsNotFound(err error) bool {
return c.impl.IsNotFound(err)
}
func (c *Controller) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
func (c *Controller) AccessURL(
ctx context.Context,
name string,
expire time.Duration,
opt *s3.AccessURLOption,
) (string, error) {
return c.impl.AccessURL(ctx, name, expire, opt)
}
+1
View File
@@ -2,6 +2,7 @@ package cont
import (
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
)
+56 -10
View File
@@ -4,14 +4,16 @@ import (
"context"
"errors"
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
"github.com/tencentyun/cos-go-sdk-v5"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/tencentyun/cos-go-sdk-v5"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
)
const (
@@ -70,7 +72,12 @@ func (c *Cos) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini
}, nil
}
func (c *Cos) CompleteMultipartUpload(ctx context.Context, uploadID string, name string, parts []s3.Part) (*s3.CompleteMultipartUploadResult, error) {
func (c *Cos) CompleteMultipartUpload(
ctx context.Context,
uploadID string,
name string,
parts []s3.Part,
) (*s3.CompleteMultipartUploadResult, error) {
opts := &cos.CompleteMultipartUploadOptions{
Parts: make([]cos.Object, len(parts)),
}
@@ -109,7 +116,13 @@ func (c *Cos) PartSize(ctx context.Context, size int64) (int64, error) {
return partSize, nil
}
func (c *Cos) AuthSign(ctx context.Context, uploadID string, name string, expire time.Duration, partNumbers []int) (*s3.AuthSignResult, error) {
func (c *Cos) AuthSign(
ctx context.Context,
uploadID string,
name string,
expire time.Duration,
partNumbers []int,
) (*s3.AuthSignResult, error) {
result := s3.AuthSignResult{
URL: c.client.BaseURL.BucketURL.String() + "/" + cos.EncodeURIComponent(name),
Query: url.Values{"uploadId": {uploadID}},
@@ -120,7 +133,13 @@ func (c *Cos) AuthSign(ctx context.Context, uploadID string, name string, expire
if err != nil {
return nil, err
}
cos.AddAuthorizationHeader(c.credential.SecretID, c.credential.SecretKey, c.credential.SessionToken, req, cos.NewAuthTime(expire))
cos.AddAuthorizationHeader(
c.credential.SecretID,
c.credential.SecretKey,
c.credential.SessionToken,
req,
cos.NewAuthTime(expire),
)
result.Header = req.Header
for i, partNumber := range partNumbers {
result.Parts[i] = s3.SignPart{
@@ -132,7 +151,15 @@ func (c *Cos) AuthSign(ctx context.Context, uploadID string, name string, expire
}
func (c *Cos) PresignedPutObject(ctx context.Context, name string, expire time.Duration) (string, error) {
rawURL, err := c.client.Object.GetPresignedURL(ctx, http.MethodPut, name, c.credential.SecretID, c.credential.SecretKey, expire, nil)
rawURL, err := c.client.Object.GetPresignedURL(
ctx,
http.MethodPut,
name,
c.credential.SecretID,
c.credential.SecretKey,
expire,
nil,
)
if err != nil {
return "", err
}
@@ -204,7 +231,13 @@ func (c *Cos) AbortMultipartUpload(ctx context.Context, uploadID string, name st
return err
}
func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name string, partNumberMarker int, maxParts int) (*s3.ListUploadedPartsResult, error) {
func (c *Cos) ListUploadedParts(
ctx context.Context,
uploadID string,
name string,
partNumberMarker int,
maxParts int,
) (*s3.ListUploadedPartsResult, error) {
result, _, err := c.client.Object.ListParts(ctx, name, uploadID, &cos.ObjectListPartsOptions{
MaxParts: strconv.Itoa(maxParts),
PartNumberMarker: strconv.Itoa(partNumberMarker),
@@ -231,7 +264,12 @@ func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name strin
return res, nil
}
func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
func (c *Cos) AccessURL(
ctx context.Context,
name string,
expire time.Duration,
opt *s3.AccessURLOption,
) (string, error) {
//reqParams := make(url.Values)
//if opt != nil {
// if opt.ContentType != "" {
@@ -246,7 +284,15 @@ func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration,
} else if expire < time.Second {
expire = time.Second
}
rawURL, err := c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, nil)
rawURL, err := c.client.Object.GetPresignedURL(
ctx,
http.MethodGet,
name,
c.credential.SecretID,
c.credential.SecretKey,
expire,
nil,
)
if err != nil {
return "", err
}
+41 -10
View File
@@ -4,16 +4,18 @@ import (
"context"
"errors"
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/signer"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/signer"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
)
const (
@@ -79,7 +81,12 @@ func (m *Minio) InitiateMultipartUpload(ctx context.Context, name string) (*s3.I
}, nil
}
func (m *Minio) CompleteMultipartUpload(ctx context.Context, uploadID string, name string, parts []s3.Part) (*s3.CompleteMultipartUploadResult, error) {
func (m *Minio) CompleteMultipartUpload(
ctx context.Context,
uploadID string,
name string,
parts []s3.Part,
) (*s3.CompleteMultipartUploadResult, error) {
minioParts := make([]minio.CompletePart, len(parts))
for i, part := range parts {
minioParts[i] = minio.CompletePart{
@@ -116,7 +123,13 @@ func (m *Minio) PartSize(ctx context.Context, size int64) (int64, error) {
return partSize, nil
}
func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expire time.Duration, partNumbers []int) (*s3.AuthSignResult, error) {
func (m *Minio) AuthSign(
ctx context.Context,
uploadID string,
name string,
expire time.Duration,
partNumbers []int,
) (*s3.AuthSignResult, error) {
creds, err := m.opts.Creds.Get()
if err != nil {
return nil, err
@@ -133,7 +146,14 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi
return nil, err
}
request.Header.Set("X-Amz-Content-Sha256", unsignedPayload)
request = signer.SignV4Trailer(*request, creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken, "us-east-1", nil)
request = signer.SignV4Trailer(
*request,
creds.AccessKeyID,
creds.SecretAccessKey,
creds.SessionToken,
"us-east-1",
nil,
)
result.Parts[i] = s3.SignPart{
PartNumber: partNumber,
URL: request.URL.String(),
@@ -204,7 +224,13 @@ func (m *Minio) AbortMultipartUpload(ctx context.Context, uploadID string, name
return m.core.AbortMultipartUpload(ctx, m.bucket, name, uploadID)
}
func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name string, partNumberMarker int, maxParts int) (*s3.ListUploadedPartsResult, error) {
func (m *Minio) ListUploadedParts(
ctx context.Context,
uploadID string,
name string,
partNumberMarker int,
maxParts int,
) (*s3.ListUploadedPartsResult, error) {
result, err := m.core.ListObjectParts(ctx, m.bucket, name, uploadID, partNumberMarker, maxParts)
if err != nil {
return nil, err
@@ -227,7 +253,12 @@ func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name str
return res, nil
}
func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
func (m *Minio) AccessURL(
ctx context.Context,
name string,
expire time.Duration,
opt *s3.AccessURLOption,
) (string, error) {
//reqParams := make(url.Values)
//if opt != nil {
// if opt.ContentType != "" {
+40 -8
View File
@@ -4,14 +4,16 @@ import (
"context"
"errors"
"fmt"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3"
)
const (
@@ -73,7 +75,12 @@ func (o *OSS) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini
}, nil
}
func (o *OSS) CompleteMultipartUpload(ctx context.Context, uploadID string, name string, parts []s3.Part) (*s3.CompleteMultipartUploadResult, error) {
func (o *OSS) CompleteMultipartUpload(
ctx context.Context,
uploadID string,
name string,
parts []s3.Part,
) (*s3.CompleteMultipartUploadResult, error) {
ossParts := make([]oss.UploadPart, len(parts))
for i, part := range parts {
ossParts[i] = oss.UploadPart{
@@ -114,7 +121,13 @@ func (o *OSS) PartSize(ctx context.Context, size int64) (int64, error) {
return partSize, nil
}
func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire time.Duration, partNumbers []int) (*s3.AuthSignResult, error) {
func (o *OSS) AuthSign(
ctx context.Context,
uploadID string,
name string,
expire time.Duration,
partNumbers []int,
) (*s3.AuthSignResult, error) {
result := s3.AuthSignResult{
URL: o.bucketURL + name,
Query: url.Values{"uploadId": {uploadID}},
@@ -132,7 +145,15 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire
}
request.Header.Set(oss.HTTPHeaderHost, request.Host)
request.Header.Set(oss.HTTPHeaderDate, time.Now().UTC().Format(http.TimeFormat))
authorization := fmt.Sprintf(`OSS %s:%s`, o.credentials.GetAccessKeyID(), o.getSignedStr(request, fmt.Sprintf(`/%s/%s?partNumber=%d&uploadId=%s`, o.bucket.BucketName, name, partNumber, uploadID), o.credentials.GetAccessKeySecret()))
authorization := fmt.Sprintf(
`OSS %s:%s`,
o.credentials.GetAccessKeyID(),
o.getSignedStr(
request,
fmt.Sprintf(`/%s/%s?partNumber=%d&uploadId=%s`, o.bucket.BucketName, name, partNumber, uploadID),
o.credentials.GetAccessKeySecret(),
),
)
request.Header.Set(oss.HTTPHeaderAuthorization, authorization)
result.Parts[i] = s3.SignPart{
PartNumber: partNumber,
@@ -213,7 +234,13 @@ func (o *OSS) AbortMultipartUpload(ctx context.Context, uploadID string, name st
})
}
func (o *OSS) ListUploadedParts(ctx context.Context, uploadID string, name string, partNumberMarker int, maxParts int) (*s3.ListUploadedPartsResult, error) {
func (o *OSS) ListUploadedParts(
ctx context.Context,
uploadID string,
name string,
partNumberMarker int,
maxParts int,
) (*s3.ListUploadedPartsResult, error) {
result, err := o.bucket.ListUploadedParts(oss.InitiateMultipartUploadResult{
UploadID: uploadID,
Key: name,
@@ -240,7 +267,12 @@ func (o *OSS) ListUploadedParts(ctx context.Context, uploadID string, name strin
return res, nil
}
func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
func (o *OSS) AccessURL(
ctx context.Context,
name string,
expire time.Duration,
opt *s3.AccessURLOption,
) (string, error) {
//var opts []oss.Option
//if opt != nil {
// if opt.ContentType != "" {
+6 -2
View File
@@ -5,12 +5,13 @@ import (
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"hash"
"io"
"net/http"
"sort"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func (o *OSS) getAdditionalHeaderKeys(req *http.Request) ([]string, map[string]string) {
@@ -71,7 +72,10 @@ func (o *OSS) getSignedStr(req *http.Request, canonicalizedResource string, keyS
// v2 signature
if o.bucket.Client.Config.AuthVersion == oss.AuthV2 {
signStr = req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + strings.Join(additionalList, ";") + "\n" + canonicalizedResource
signStr = req.Method + "\n" + contentMd5 + "\n" + contentType + "\n" + date + "\n" + canonicalizedOSSHeaders + strings.Join(
additionalList,
";",
) + "\n" + canonicalizedResource
h = hmac.New(func() hash.Hash { return sha256.New() }, []byte(keySecret))
}
_, _ = io.WriteString(h, signStr)
+20 -3
View File
@@ -112,10 +112,21 @@ type Interface interface {
PartLimit() *PartLimit
InitiateMultipartUpload(ctx context.Context, name string) (*InitiateMultipartUploadResult, error)
CompleteMultipartUpload(ctx context.Context, uploadID string, name string, parts []Part) (*CompleteMultipartUploadResult, error)
CompleteMultipartUpload(
ctx context.Context,
uploadID string,
name string,
parts []Part,
) (*CompleteMultipartUploadResult, error)
PartSize(ctx context.Context, size int64) (int64, error)
AuthSign(ctx context.Context, uploadID string, name string, expire time.Duration, partNumbers []int) (*AuthSignResult, error)
AuthSign(
ctx context.Context,
uploadID string,
name string,
expire time.Duration,
partNumbers []int,
) (*AuthSignResult, error)
PresignedPutObject(ctx context.Context, name string, expire time.Duration) (string, error)
@@ -128,7 +139,13 @@ type Interface interface {
IsNotFound(err error) bool
AbortMultipartUpload(ctx context.Context, uploadID string, name string) error
ListUploadedParts(ctx context.Context, uploadID string, name string, partNumberMarker int, maxParts int) (*ListUploadedPartsResult, error)
ListUploadedParts(
ctx context.Context,
uploadID string,
name string,
partNumberMarker int,
maxParts int,
) (*ListUploadedPartsResult, error)
AccessURL(ctx context.Context, name string, expire time.Duration, opt *AccessURLOption) (string, error)
}