You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

rclone.go 1.8 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "context"
  17. "fmt"
  18. "io"
  19. _ "github.com/rclone/rclone/backend/all"
  20. "github.com/rclone/rclone/fs"
  21. )
  22. func getFs(bucketName string) (fs.Fs, error) {
  23. f, err := fs.NewFs(context.Background(), bucketName)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return f, nil
  28. }
  29. func ListObjects2(bucketName string, prefix string) ([]fs.DirEntry, error) {
  30. if bucketName == "" {
  31. return nil, fmt.Errorf("bucket name is empty")
  32. }
  33. f, err := getFs(bucketName)
  34. if err != nil {
  35. return nil, err
  36. }
  37. entries, err := f.List(context.Background(), prefix)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return entries, nil
  42. }
  43. func PutObject2(bucketName string, key string, in io.Reader) error {
  44. f, err := getFs(bucketName)
  45. if err != nil {
  46. return err
  47. }
  48. // Use Rcat to put an object to the remote
  49. //_, err = operations.Rcat(context.Background(), f, key, nil, nil, nil)
  50. print(f)
  51. return err
  52. }
  53. func DeleteObject2(bucketName string, key string) error {
  54. f, err := getFs(bucketName)
  55. if err != nil {
  56. return err
  57. }
  58. remoteObj, err := f.NewObject(context.Background(), key)
  59. if err != nil {
  60. return err
  61. }
  62. return remoteObj.Remove(context.Background())
  63. }