package gostorage import ( "github.com/aliyun/aliyun-oss-go-sdk/oss" "io" ) // AliYunConfig 阿里云配置 type AliYunConfig struct { AccessKeyId string AccessKeySecret string Endpoint string BucketName string } // AliYun 阿里云 type AliYun struct { AliYunConfig error error // 错误信息 client *oss.Client // 驱动 bucket *oss.Bucket // 存储空间 } // NewAliYun 初始化 // https://help.aliyun.com/document_detail/32144.html func NewAliYun(config *AliYunConfig) *AliYun { app := &AliYun{} app.AccessKeyId = config.AccessKeyId app.AccessKeySecret = config.AccessKeySecret app.Endpoint = config.Endpoint 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) return app } // 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 }