38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
|
|
package aliyun
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"git.apinb.com/ops/files/internal/storage"
|
||
|
|
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (c *Client) PrepareUpload(ctx context.Context, request storage.UploadRequest) (storage.UploadInstruction, error) {
|
||
|
|
if strings.TrimSpace(request.ObjectKey) == "" {
|
||
|
|
return storage.UploadInstruction{}, fmt.Errorf("对象键不能为空")
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(request.ContentType) == "" {
|
||
|
|
return storage.UploadInstruction{}, fmt.Errorf("内容类型不能为空")
|
||
|
|
}
|
||
|
|
|
||
|
|
result, err := c.client.Presign(ctx, &oss.PutObjectRequest{
|
||
|
|
Bucket: oss.Ptr(c.bucket),
|
||
|
|
Key: oss.Ptr(request.ObjectKey),
|
||
|
|
ContentType: oss.Ptr(request.ContentType),
|
||
|
|
ForbidOverwrite: oss.Ptr("true"),
|
||
|
|
}, oss.PresignExpires(time.Duration(c.presignTTL)*time.Second))
|
||
|
|
if err != nil {
|
||
|
|
return storage.UploadInstruction{}, fmt.Errorf("生成上传预签名: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return storage.UploadInstruction{
|
||
|
|
Method: result.Method,
|
||
|
|
URL: result.URL,
|
||
|
|
Headers: result.SignedHeaders,
|
||
|
|
ExpiresAt: result.Expiration,
|
||
|
|
}, nil
|
||
|
|
}
|