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.

storage.go 2.2 kB

5 years ago
4 years ago
5 years ago
3 years ago
5 years ago
5 years ago
3 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package storage
  5. import (
  6. "fmt"
  7. "io"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/obs"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. const (
  13. MinioStorageType = "minio"
  14. LocalStorageType = "local"
  15. )
  16. // ObjectStorage represents an object storage to handle a bucket and files
  17. type ObjectStorage interface {
  18. Save(path string, r io.Reader) (int64, error)
  19. Open(path string) (io.ReadCloser, error)
  20. DownloadAFile(bucket string, objectName string) (io.ReadCloser, error)
  21. Delete(path string) error
  22. DeleteDir(dir string) error
  23. PresignedGetURL(path string, fileName string) (string, error)
  24. PresignedPutURL(path string) (string, error)
  25. HasObject(path string) (bool, error)
  26. UploadObject(fileName, filePath string) error
  27. }
  28. // Copy copys a file from source ObjectStorage to dest ObjectStorage
  29. func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, srcPath string) (int64, error) {
  30. f, err := srcStorage.Open(srcPath)
  31. if err != nil {
  32. return 0, err
  33. }
  34. defer f.Close()
  35. return dstStorage.Save(dstPath, f)
  36. }
  37. var (
  38. // Attachments represents attachments storage
  39. Attachments ObjectStorage
  40. ObsCli *obs.ObsClient
  41. )
  42. // Init init the stoarge
  43. func Init() error {
  44. var err error
  45. switch setting.Attachment.StoreType {
  46. case LocalStorageType:
  47. Attachments, err = NewLocalStorage(setting.Attachment.Path)
  48. log.Info("local storage inited.")
  49. case MinioStorageType:
  50. minio := setting.Attachment.Minio
  51. Attachments, err = NewMinioStorage(
  52. minio.Endpoint,
  53. minio.AccessKeyID,
  54. minio.SecretAccessKey,
  55. minio.Bucket,
  56. minio.Location,
  57. minio.BasePath,
  58. minio.UseSSL,
  59. )
  60. log.Info("minio storage inited.")
  61. default:
  62. return fmt.Errorf("Unsupported attachment store type: %s", setting.Attachment.StoreType)
  63. }
  64. ObsCli, err = obs.New(setting.AccessKeyID, setting.SecretAccessKey, setting.Endpoint)
  65. if err != nil {
  66. log.Error("obs.New failed:", err)
  67. return err
  68. }
  69. log.Info("obs cli inited.")
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }