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