s3.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package persistence
  2. import (
  3. "path"
  4. "github.com/langgenius/dify-plugin-daemon/internal/oss"
  5. )
  6. type wrapper struct {
  7. oss oss.OSS
  8. }
  9. func NewWrapper(oss oss.OSS) *wrapper {
  10. return &wrapper{
  11. oss: oss,
  12. }
  13. }
  14. func (s *wrapper) getFilePath(tenant_id string, plugin_checksum string, key string) string {
  15. return path.Join(tenant_id, plugin_checksum, key)
  16. }
  17. func (s *wrapper) Save(tenant_id string, plugin_checksum string, key string, data []byte) error {
  18. // save to s3
  19. file_path := s.getFilePath(tenant_id, plugin_checksum, key)
  20. return s.oss.Save(file_path, data)
  21. }
  22. func (s *wrapper) Load(tenant_id string, plugin_checksum string, key string) ([]byte, error) {
  23. // load from s3
  24. file_path := s.getFilePath(tenant_id, plugin_checksum, key)
  25. return s.oss.Load(file_path)
  26. }
  27. func (s *wrapper) Delete(tenant_id string, plugin_checksum string, key string) error {
  28. file_path := s.getFilePath(tenant_id, plugin_checksum, key)
  29. return s.oss.Delete(file_path)
  30. }
  31. func (s *wrapper) StateSize(tenant_id string, plugin_checksum string, key string) (int64, error) {
  32. file_path := s.getFilePath(tenant_id, plugin_checksum, key)
  33. state, err := s.oss.State(file_path)
  34. if err != nil {
  35. return 0, err
  36. }
  37. return state.Size, nil
  38. }