Feat: enable using LAN endpoint in serverside request of OSS policy (#399)
This commit is contained in:
parent
0d210e87b3
commit
a5805b022a
5 changed files with 47 additions and 13 deletions
2
assets
2
assets
|
@ -1 +1 @@
|
|||
Subproject commit 1a2f8ce8ca7346b4b1822a7f4dd84bef70db71de
|
||||
Subproject commit 1bd093315526ac8585a8c7a3499e50f2a13d82fa
|
|
@ -47,12 +47,12 @@ type PolicyOption struct {
|
|||
FileType []string `json:"file_type"`
|
||||
// MimeType
|
||||
MimeType string `json:"mimetype"`
|
||||
|
||||
// OdRedirect Onedrive重定向地址
|
||||
OdRedirect string `json:"od_redirect,omitempty"`
|
||||
|
||||
// Region 区域代码
|
||||
Region string `json:"region,omitempty"`
|
||||
// ServerSideEndpoint 服务端请求使用的 Endpoint,为空时使用 Policy.Server 字段
|
||||
ServerSideEndpoint string `json:"server_side_endpoint,omitempty"`
|
||||
}
|
||||
|
||||
var thumbSuffix = map[string][]string{
|
||||
|
|
|
@ -55,7 +55,7 @@ const (
|
|||
// CORS 创建跨域策略
|
||||
func (handler *Driver) CORS() error {
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
if err := handler.InitOSSClient(false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -77,14 +77,20 @@ func (handler *Driver) CORS() error {
|
|||
}
|
||||
|
||||
// InitOSSClient 初始化OSS鉴权客户端
|
||||
func (handler *Driver) InitOSSClient() error {
|
||||
func (handler *Driver) InitOSSClient(forceUsePublicEndpoint bool) error {
|
||||
if handler.Policy == nil {
|
||||
return errors.New("存储策略为空")
|
||||
}
|
||||
|
||||
if handler.client == nil {
|
||||
// 决定是否使用内网 Endpoint
|
||||
endpoint := handler.Policy.Server
|
||||
if handler.Policy.OptionsSerialized.ServerSideEndpoint != "" && !forceUsePublicEndpoint {
|
||||
endpoint = handler.Policy.OptionsSerialized.ServerSideEndpoint
|
||||
}
|
||||
|
||||
// 初始化客户端
|
||||
client, err := oss.New(handler.Policy.Server, handler.Policy.AccessKey, handler.Policy.SecretKey)
|
||||
client, err := oss.New(endpoint, handler.Policy.AccessKey, handler.Policy.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -105,7 +111,7 @@ func (handler *Driver) InitOSSClient() error {
|
|||
// List 列出OSS上的文件
|
||||
func (handler Driver) List(ctx context.Context, base string, recursive bool) ([]response.Object, error) {
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
if err := handler.InitOSSClient(false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -179,6 +185,9 @@ func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser,
|
|||
// 通过VersionID禁止缓存
|
||||
ctx = context.WithValue(ctx, VersionID, time.Now().UnixNano())
|
||||
|
||||
// 尽可能使用私有 Endpoint
|
||||
ctx = context.WithValue(ctx, fsctx.ForceUsePublicEndpoint, false)
|
||||
|
||||
// 获取文件源地址
|
||||
downloadURL, err := handler.Source(
|
||||
ctx,
|
||||
|
@ -219,7 +228,7 @@ func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, s
|
|||
defer file.Close()
|
||||
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
if err := handler.InitOSSClient(false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -243,7 +252,7 @@ func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, s
|
|||
// 返回未删除的文件
|
||||
func (handler Driver) Delete(ctx context.Context, files []string) ([]string, error) {
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
if err := handler.InitOSSClient(false); err != nil {
|
||||
return files, err
|
||||
}
|
||||
|
||||
|
@ -266,7 +275,7 @@ func (handler Driver) Delete(ctx context.Context, files []string) ([]string, err
|
|||
// Thumb 获取文件缩略图
|
||||
func (handler Driver) Thumb(ctx context.Context, path string) (*response.ContentResponse, error) {
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
if err := handler.InitOSSClient(true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -307,7 +316,11 @@ func (handler Driver) Source(
|
|||
speed int,
|
||||
) (string, error) {
|
||||
// 初始化客户端
|
||||
if err := handler.InitOSSClient(); err != nil {
|
||||
usePublicEndpoint := true
|
||||
if forceUsePublicEndpoint, ok := ctx.Value(fsctx.ForceUsePublicEndpoint).(bool); ok {
|
||||
usePublicEndpoint = forceUsePublicEndpoint
|
||||
}
|
||||
if err := handler.InitOSSClient(usePublicEndpoint); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
|
|
@ -30,13 +30,19 @@ func TestDriver_InitOSSClient(t *testing.T) {
|
|||
|
||||
// 成功
|
||||
{
|
||||
asserts.NoError(handler.InitOSSClient())
|
||||
asserts.NoError(handler.InitOSSClient(false))
|
||||
}
|
||||
|
||||
// 使用内网Endpoint
|
||||
{
|
||||
handler.Policy.OptionsSerialized.ServerSideEndpoint = "endpoint2"
|
||||
asserts.NoError(handler.InitOSSClient(false))
|
||||
}
|
||||
|
||||
// 未指定存储策略
|
||||
{
|
||||
handler := Driver{}
|
||||
asserts.Error(handler.InitOSSClient())
|
||||
asserts.Error(handler.InitOSSClient(false))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -182,6 +188,19 @@ func TestDriver_Source(t *testing.T) {
|
|||
asserts.Empty(query.Get("Signature"))
|
||||
asserts.Contains(resURL.String(), handler.Policy.BaseURL)
|
||||
}
|
||||
|
||||
// 强制使用公网 Endpoint
|
||||
{
|
||||
handler.Policy.BaseURL = ""
|
||||
handler.Policy.OptionsSerialized.ServerSideEndpoint = "endpoint.com"
|
||||
res, err := handler.Source(context.WithValue(context.Background(), fsctx.ForceUsePublicEndpoint, false), "/123", url.URL{}, 10, false, 0)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.Empty(query.Get("Signature"))
|
||||
asserts.Contains(resURL.String(), "endpoint.com")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Thumb(t *testing.T) {
|
||||
|
|
|
@ -33,4 +33,6 @@ const (
|
|||
IgnoreConflictCtx
|
||||
// RetryCtx 失败重试次数
|
||||
RetryCtx
|
||||
// ForceUsePublicEndpoint 强制使用公网 Endpoint
|
||||
ForceUsePublicEndpoint
|
||||
)
|
||||
|
|
Loading…
Add table
Reference in a new issue