package gostorage import ( "github.com/aliyun/aliyun-oss-go-sdk/oss" "io" ) // AliYun 阿里云 OSS 存储 type AliYun struct { Endpoint string AccessKeyId string AccessKeySecret string error error // 错误信息 client *oss.Client // 驱动 bucket *oss.Bucket // 存储空间 } // NewAliYun 初始化 func NewAliYun(endpoint string, accessKeyId string, accessKeySecret string) *AliYun { app := &AliYun{Endpoint: endpoint, AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret} app.client, app.error = oss.New(endpoint, accessKeyId, accessKeySecret) return app } // Bucket 存储空间 func (c *AliYun) Bucket(name string) *AliYun { c.bucket, c.error = c.client.Bucket(name) return c } // FileInfo 上传文件的信息 type FileInfo struct { Path string // 文件路径 Name string // 文件名称 Url string // 文件地址 } // 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 }