dao.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package oss
  2. import (
  3. "context"
  4. "io"
  5. "path"
  6. "go-common/app/admin/main/macross/conf"
  7. "go-common/library/log"
  8. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  9. )
  10. // Dao macross dao
  11. type Dao struct {
  12. // conf
  13. c *conf.Config
  14. client *oss.Client
  15. }
  16. // New dao.
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. c: c,
  20. }
  21. client, err := oss.New(d.c.Oss.Endpoint, d.c.Oss.AccessKeyID, d.c.Oss.AccessKeySecret)
  22. if err != nil {
  23. panic(err)
  24. }
  25. d.client = client
  26. return
  27. }
  28. // Put put object into oss.
  29. func (d *Dao) Put(c context.Context, rd io.Reader, apkName string) (uri string, err error) {
  30. bucket, _ := d.client.Bucket(d.c.Oss.Bucket)
  31. uri = path.Join(d.c.Oss.OriginDir, apkName)
  32. // put
  33. if err = bucket.PutObject(uri, rd); err != nil {
  34. log.Error("bucket.PutObject(%s) error(%v)", uri, err)
  35. return
  36. }
  37. uri = path.Join(d.c.Oss.Bucket, uri) // NOTE: begin with '/' when upload success
  38. return
  39. }
  40. // Publish publish object into oss.
  41. func (d *Dao) Publish(objectKey string, destKey string) (uri string, err error) {
  42. bucket, _ := d.client.Bucket(d.c.Oss.Bucket)
  43. _, err = bucket.CopyObject(d.c.Oss.PublishDir+objectKey, d.c.Oss.PublishDir+destKey)
  44. if err != nil {
  45. log.Error("bucket.CopyObject(%s, %s) error(%v)", d.c.Oss.PublishDir+objectKey, d.c.Oss.PublishDir+destKey, err)
  46. return
  47. }
  48. uri = path.Join("/", d.c.Oss.PublishDir+destKey)
  49. return
  50. }
  51. // Close close kafka connection.
  52. func (d *Dao) Close() {
  53. }