oss.go 952 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package util
  4. import (
  5. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  6. )
  7. // AliyunOSS 阿里云存储
  8. type AliyunOSS struct {
  9. bucket string
  10. url string
  11. accessKeyId string
  12. accessKeySecret string
  13. }
  14. // NewAliyunOSS 新建阿里云存储结构体
  15. func NewAliyunOSS(bucket, url, accessKeyId, accessKeySecret string) *AliyunOSS {
  16. return &AliyunOSS{
  17. bucket: bucket,
  18. url: url,
  19. accessKeyId: accessKeyId,
  20. accessKeySecret: accessKeySecret,
  21. }
  22. }
  23. // Upload 上传操作
  24. func (p *AliyunOSS) Upload(srcPath, dstPath string) error {
  25. client, err := oss.New(p.url, p.accessKeyId, p.accessKeySecret)
  26. if err != nil {
  27. return err
  28. }
  29. bucket, err := client.Bucket(p.bucket)
  30. if err != nil {
  31. return err
  32. }
  33. err = bucket.PutObjectFromFile(dstPath, srcPath)
  34. if err != nil {
  35. return err
  36. }
  37. return nil
  38. }