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.

object.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package cmdline
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "github.com/jedib0t/go-pretty/v6/table"
  8. "gitlink.org.cn/cloudream/client/internal/services"
  9. )
  10. func (c *Commandline) ListBucketObjects(bucketID int) error {
  11. userID := 0
  12. objects, err := services.BucketSvc(c.svc).GetBucketObjects(userID, bucketID)
  13. if err != nil {
  14. return err
  15. }
  16. fmt.Printf("Find %d objects in bucket %d for user %d:\n", len(objects), bucketID, userID)
  17. tb := table.NewWriter()
  18. tb.AppendHeader(table.Row{"ID", "Name", "Size", "BucketID", "State", "Redundancy", "NumRep", "ECName"})
  19. for _, obj := range objects {
  20. tb.AppendRow(table.Row{obj.ObjectID, obj.Name, obj.BucketID, obj.State, obj.FileSizeInBytes, obj.Redundancy, obj.NumRep, obj.ECName})
  21. }
  22. fmt.Print(tb.Render())
  23. return nil
  24. }
  25. func (c *Commandline) Read(localFilePath string, objectID int) error {
  26. // 创建本地文件
  27. curExecPath, err := os.Executable()
  28. if err != nil {
  29. return fmt.Errorf("get executable directory failed, err: %w", err)
  30. }
  31. outputFilePath := filepath.Join(filepath.Dir(curExecPath), localFilePath)
  32. outputFileDir := filepath.Dir(outputFilePath)
  33. err = os.MkdirAll(outputFileDir, os.ModePerm)
  34. if err != nil {
  35. return fmt.Errorf("create output file directory %s failed, err: %w", outputFileDir, err)
  36. }
  37. outputFile, err := os.Create(outputFilePath)
  38. if err != nil {
  39. return fmt.Errorf("create output file %s failed, err: %w", outputFilePath, err)
  40. }
  41. defer outputFile.Close()
  42. // 下载文件
  43. reader, err := services.ObjectSvc(c.svc).DownloadObject(0, objectID)
  44. if err != nil {
  45. return fmt.Errorf("download object failed, err: %w", err)
  46. }
  47. defer reader.Close()
  48. _, err = io.Copy(outputFile, reader)
  49. if err != nil {
  50. // TODO 写入到文件失败,是否要考虑删除这个不完整的文件?
  51. return fmt.Errorf("copy object data to local file failed, err: %w", err)
  52. }
  53. return nil
  54. }
  55. func (c *Commandline) RepWrite(localFilePath string, bucketID int, objectName string, repNum int) error {
  56. file, err := os.Open(localFilePath)
  57. if err != nil {
  58. return fmt.Errorf("open file %s failed, err: %w", localFilePath, err)
  59. }
  60. defer file.Close()
  61. fileInfo, err := file.Stat()
  62. if err != nil {
  63. return fmt.Errorf("get file %s state failed, err: %w", localFilePath, err)
  64. }
  65. fileSize := fileInfo.Size()
  66. err = services.ObjectSvc(c.svc).UploadRepObject(0, bucketID, objectName, file, fileSize, repNum)
  67. if err != nil {
  68. return fmt.Errorf("upload file data failed, err: %w", err)
  69. }
  70. return nil
  71. }
  72. func (c *Commandline) EcWrite(localFilePath string, bucketID int, objectName string, ecName string) error {
  73. // TODO
  74. panic("not implement yet")
  75. }
  76. func (c *Commandline) UpdateRepObject(objectID int, filePath string) error {
  77. userID := 0
  78. file, err := os.Open(filePath)
  79. if err != nil {
  80. return fmt.Errorf("open file %s failed, err: %w", filePath, err)
  81. }
  82. defer file.Close()
  83. fileInfo, err := file.Stat()
  84. if err != nil {
  85. return fmt.Errorf("get file %s state failed, err: %w", filePath, err)
  86. }
  87. fileSize := fileInfo.Size()
  88. err = services.ObjectSvc(c.svc).UpdateRepObject(userID, objectID, file, fileSize)
  89. if err != nil {
  90. return fmt.Errorf("update object %d failed, err: %w", objectID, err)
  91. }
  92. return nil
  93. }
  94. func (c *Commandline) DeleteObject(objectID int) error {
  95. userID := 0
  96. err := services.ObjectSvc(c.svc).DeleteObject(userID, objectID)
  97. if err != nil {
  98. return fmt.Errorf("delete object %d failed, err: %w", objectID, err)
  99. }
  100. return nil
  101. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。