updatefilemetalogic.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "app.yhyue.com/moapp/jyfs/rpc/filesystem"
  6. "app.yhyue.com/moapp/jyfs/rpc/internal/svc"
  7. "github.com/aliyun/aliyun-oss-go-sdk/oss"
  8. "github.com/tal-tech/go-zero/core/logx"
  9. )
  10. type UpdateFileMetaLogic struct {
  11. ctx context.Context
  12. svcCtx *svc.ServiceContext
  13. logx.Logger
  14. }
  15. func NewUpdateFileMetaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateFileMetaLogic {
  16. return &UpdateFileMetaLogic{
  17. ctx: ctx,
  18. svcCtx: svcCtx,
  19. Logger: logx.WithContext(ctx),
  20. }
  21. }
  22. // 更新文件元数据
  23. func (l *UpdateFileMetaLogic) UpdateFileMeta(in *filesystem.UpdateFileMetaReq) (*filesystem.FileOpResp, error) {
  24. // todo: add your logic here and delete this line
  25. // 获取存储空间。
  26. bucket, err := l.svcCtx.OssClient.Bucket(in.Domain)
  27. if err != nil {
  28. return &filesystem.FileOpResp{}, err
  29. }
  30. meta := redis.GetStr("jyfs", "jyfs_"+in.Domain)
  31. if meta != nil {
  32. for k, _ := range in.Meta {
  33. if !strings.Contains(meta, k) {
  34. return &filesystem.FileOpResp{}, errors.New("缺少元信息")
  35. }
  36. }
  37. }
  38. // 一次修改多条元信息。
  39. options := []oss.Option{
  40. // oss.Meta("MyMeta", "MyMetaValue2"),
  41. // oss.Meta("MyObjectLocation", "HangZhou"),
  42. }
  43. for k, v := range in.Meta {
  44. options = append(options, oss.Meta(k, v))
  45. }
  46. err = bucket.SetObjectMeta(in.FileId, options...)
  47. if err != nil {
  48. return &filesystem.FileOpResp{State: false}, err
  49. }
  50. // 获取文件元信息。
  51. props, err := bucket.GetObjectDetailedMeta(in.FileId)
  52. if err != nil {
  53. return &filesystem.FileOpResp{}, err
  54. }
  55. fmt.Println("Object Meta:", props)
  56. return &filesystem.FileOpResp{State: true}, nil
  57. }