// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package util import ( "github.com/aliyun/aliyun-oss-go-sdk/oss" ) // AliyunOSS 阿里云存储 type AliyunOSS struct { bucket string url string accessKeyId string accessKeySecret string } // NewAliyunOSS 新建阿里云存储结构体 func NewAliyunOSS(bucket, url, accessKeyId, accessKeySecret string) *AliyunOSS { return &AliyunOSS{ bucket: bucket, url: url, accessKeyId: accessKeyId, accessKeySecret: accessKeySecret, } } // Upload 上传操作 func (p *AliyunOSS) Upload(srcPath, dstPath string) error { client, err := oss.New(p.url, p.accessKeyId, p.accessKeySecret) if err != nil { return err } bucket, err := client.Bucket(p.bucket) if err != nil { return err } err = bucket.PutObjectFromFile(dstPath, srcPath) if err != nil { return err } return nil }