oss.go 898 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package oss
  2. import (
  3. "bytes"
  4. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  5. )
  6. type Oss struct {
  7. client *oss.Client
  8. bucket *oss.Bucket
  9. }
  10. type Config struct {
  11. EndPoint string
  12. AccessId string
  13. AccessSecret string
  14. BucketName string
  15. }
  16. func New(config *Config, options ...oss.ClientOption) (*Oss, error) {
  17. client, err := oss.New(config.EndPoint, config.AccessId, config.AccessSecret, options...)
  18. if err != nil {
  19. return nil, err
  20. }
  21. bucket, err := client.Bucket(config.BucketName)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &Oss{
  26. client: client,
  27. bucket: bucket,
  28. }, nil
  29. }
  30. // PutObject 上传字符串
  31. func (o *Oss) PutObject(object string, content []byte) error {
  32. return o.bucket.PutObject(object, bytes.NewReader(content))
  33. }
  34. // PutObjectFromFile 上传文件
  35. func (o *Oss) PutObjectFromFile(object, local string) error {
  36. return o.bucket.PutObjectFromFile(object, local)
  37. }