You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gostorage/aliyun.go

176 lines
4.1 KiB

package gostorage
import (
"errors"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"io"
"sync"
)
// AliYunConfig 阿里云配置
type AliYunConfig struct {
AccessKeyId string // 账号信息
AccessKeySecret string // 账号信息
Endpoint string // 地域节点 外网访问 test3
EndpointEcs string // 地域节点 ECS 的经典网络访问(内网) test1
EndpointAccelerate string // 地域节点 传输加速域名(全地域上传下载加速 test4
BucketName string // 存储空间名称
}
// AliYun 阿里云
type AliYun struct {
AliYunConfig
error error // 错误信息
client *oss.Client // 驱动
bucket *oss.Bucket // 存储空间
endpointError error // 错误信息
endpointEcsError error // 错误信息
endpointAccelerateError error // 错误信息
}
// NewAliYun 初始化
// https://help.aliyun.com/document_detail/32144.html
func NewAliYun(config *AliYunConfig) (*AliYun, error) {
app := &AliYun{}
app.AccessKeyId = config.AccessKeyId
app.AccessKeySecret = config.AccessKeySecret
app.Endpoint = config.Endpoint
app.EndpointEcs = config.EndpointEcs
app.EndpointAccelerate = config.EndpointAccelerate
app.BucketName = config.BucketName
wg := sync.WaitGroup{}
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 存储空间
func (c *AliYun) Bucket(name string) *AliYun {
c.bucket, c.error = c.client.Bucket(name)
return c
}
// PutObject 上传文件流
// @param file 文件流
// @param filePath 文件路径
// @param fileName 文件名称
func (c *AliYun) PutObject(file io.Reader, filePath, fileName string) (resp FileInfo, err error) {
objectKey := filePath
if fileName != "" {
objectKey = filePath + "/" + fileName
}
err = c.bucket.PutObject(objectKey, file)
resp.Path = filePath
resp.Name = fileName
resp.Url = objectKey
return
}