|
|
@ -1,37 +1,155 @@
|
|
|
|
package gostorage
|
|
|
|
package gostorage
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
|
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// AliYunConfig 阿里云配置
|
|
|
|
// AliYunConfig 阿里云配置
|
|
|
|
type AliYunConfig struct {
|
|
|
|
type AliYunConfig struct {
|
|
|
|
AccessKeyId string
|
|
|
|
AccessKeyId string // 账号信息
|
|
|
|
AccessKeySecret string
|
|
|
|
AccessKeySecret string // 账号信息
|
|
|
|
Endpoint string
|
|
|
|
Endpoint string // 地域节点 外网访问 test3
|
|
|
|
BucketName string
|
|
|
|
EndpointEcs string // 地域节点 ECS 的经典网络访问(内网) test1
|
|
|
|
|
|
|
|
EndpointAccelerate string // 地域节点 传输加速域名(全地域上传下载加速 test4
|
|
|
|
|
|
|
|
BucketName string // 存储空间名称
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// AliYun 阿里云
|
|
|
|
// AliYun 阿里云
|
|
|
|
type AliYun struct {
|
|
|
|
type AliYun struct {
|
|
|
|
AliYunConfig
|
|
|
|
AliYunConfig
|
|
|
|
error error // 错误信息
|
|
|
|
error error // 错误信息
|
|
|
|
client *oss.Client // 驱动
|
|
|
|
client *oss.Client // 驱动
|
|
|
|
bucket *oss.Bucket // 存储空间
|
|
|
|
bucket *oss.Bucket // 存储空间
|
|
|
|
|
|
|
|
endpointError error // 错误信息
|
|
|
|
|
|
|
|
endpointEcsError error // 错误信息
|
|
|
|
|
|
|
|
endpointAccelerateError error // 错误信息
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewAliYun 初始化
|
|
|
|
// NewAliYun 初始化
|
|
|
|
// https://help.aliyun.com/document_detail/32144.html
|
|
|
|
// https://help.aliyun.com/document_detail/32144.html
|
|
|
|
func NewAliYun(config *AliYunConfig) *AliYun {
|
|
|
|
func NewAliYun(config *AliYunConfig) (*AliYun, error) {
|
|
|
|
app := &AliYun{}
|
|
|
|
app := &AliYun{}
|
|
|
|
app.AccessKeyId = config.AccessKeyId
|
|
|
|
app.AccessKeyId = config.AccessKeyId
|
|
|
|
app.AccessKeySecret = config.AccessKeySecret
|
|
|
|
app.AccessKeySecret = config.AccessKeySecret
|
|
|
|
app.Endpoint = config.Endpoint
|
|
|
|
app.Endpoint = config.Endpoint
|
|
|
|
|
|
|
|
app.EndpointEcs = config.EndpointEcs
|
|
|
|
|
|
|
|
app.EndpointAccelerate = config.EndpointAccelerate
|
|
|
|
app.BucketName = config.BucketName
|
|
|
|
app.BucketName = config.BucketName
|
|
|
|
app.client, app.error = oss.New(app.Endpoint, app.AccessKeyId, app.AccessKeySecret)
|
|
|
|
|
|
|
|
app.bucket, app.error = app.client.Bucket(app.BucketName)
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
return app
|
|
|
|
wg.Add(3)
|
|
|
|
|
|
|
|
go app.configEndpointEcs(&wg)
|
|
|
|
|
|
|
|
go app.configEndpoint(&wg)
|
|
|
|
|
|
|
|
go app.configEndpointAccelerate(&wg)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断结果
|
|
|
|
|
|
|
|
if app.endpointEcsError == nil {
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.endpointError == nil {
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.endpointAccelerateError == nil {
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return app, errors.New("链接失败")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *AliYun) configEndpoint(wg *sync.WaitGroup) {
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if c.Endpoint == "" {
|
|
|
|
|
|
|
|
c.endpointError = errors.New("没有配置")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建链接
|
|
|
|
|
|
|
|
c.client, c.endpointError = oss.New(c.Endpoint, c.AccessKeyId, c.AccessKeySecret)
|
|
|
|
|
|
|
|
if c.endpointError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 填写存储空间名称
|
|
|
|
|
|
|
|
c.bucket, c.endpointError = c.client.Bucket(c.BucketName)
|
|
|
|
|
|
|
|
if c.endpointError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断存储空间是否存在
|
|
|
|
|
|
|
|
_, c.endpointError = c.client.IsBucketExist(c.BucketName)
|
|
|
|
|
|
|
|
if c.endpointError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.endpointError = nil
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *AliYun) configEndpointEcs(wg *sync.WaitGroup) {
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if c.EndpointEcs == "" {
|
|
|
|
|
|
|
|
c.endpointEcsError = errors.New("没有配置")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建链接
|
|
|
|
|
|
|
|
c.client, c.endpointEcsError = oss.New(c.EndpointEcs, c.AccessKeyId, c.AccessKeySecret)
|
|
|
|
|
|
|
|
if c.endpointEcsError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 填写存储空间名称
|
|
|
|
|
|
|
|
c.bucket, c.endpointEcsError = c.client.Bucket(c.BucketName)
|
|
|
|
|
|
|
|
if c.endpointEcsError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断存储空间是否存在
|
|
|
|
|
|
|
|
_, c.endpointEcsError = c.client.IsBucketExist(c.BucketName)
|
|
|
|
|
|
|
|
if c.endpointEcsError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.endpointEcsError = nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (c *AliYun) configEndpointAccelerate(wg *sync.WaitGroup) {
|
|
|
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if c.EndpointAccelerate == "" {
|
|
|
|
|
|
|
|
c.endpointAccelerateError = errors.New("没有配置")
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建链接
|
|
|
|
|
|
|
|
c.client, c.endpointAccelerateError = oss.New(c.EndpointAccelerate, c.AccessKeyId, c.AccessKeySecret)
|
|
|
|
|
|
|
|
if c.endpointAccelerateError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 填写存储空间名称
|
|
|
|
|
|
|
|
c.bucket, c.endpointAccelerateError = c.client.Bucket(c.BucketName)
|
|
|
|
|
|
|
|
if c.endpointAccelerateError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断存储空间是否存在
|
|
|
|
|
|
|
|
_, c.endpointAccelerateError = c.client.IsBucketExist(c.BucketName)
|
|
|
|
|
|
|
|
if c.endpointAccelerateError != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.endpointAccelerateError = nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Bucket 存储空间
|
|
|
|
// Bucket 存储空间
|
|
|
|