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.1 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Delete(path string) error
  21. DeleteDir(dir string) error
  22. PresignedGetURL(path string, fileName string) (string, error)
  23. PresignedPutURL(path string) (string, error)
  24. HasObject(path string) (bool, error)
  25. UploadObject(fileName, filePath string) error
  26. }
  27. // Copy copys a file from source ObjectStorage to dest ObjectStorage
  28. func Copy(dstStorage ObjectStorage, dstPath string, srcStorage ObjectStorage, srcPath string) (int64, error) {
  29. f, err := srcStorage.Open(srcPath)
  30. if err != nil {
  31. return 0, err
  32. }
  33. defer f.Close()
  34. return dstStorage.Save(dstPath, f)
  35. }
  36. var (
  37. // Attachments represents attachments storage
  38. Attachments ObjectStorage
  39. ObsCli *obs.ObsClient
  40. )
  41. // Init init the stoarge
  42. func Init() error {
  43. var err error
  44. switch setting.Attachment.StoreType {
  45. case LocalStorageType:
  46. Attachments, err = NewLocalStorage(setting.Attachment.Path)
  47. log.Info("local storage inited.")
  48. case MinioStorageType:
  49. minio := setting.Attachment.Minio
  50. Attachments, err = NewMinioStorage(
  51. minio.Endpoint,
  52. minio.AccessKeyID,
  53. minio.SecretAccessKey,
  54. minio.Bucket,
  55. minio.Location,
  56. minio.BasePath,
  57. minio.UseSSL,
  58. )
  59. log.Info("minio storage inited.")
  60. default:
  61. return fmt.Errorf("Unsupported attachment store type: %s", setting.Attachment.StoreType)
  62. }
  63. ObsCli, err = obs.New(setting.AccessKeyID, setting.SecretAccessKey, setting.Endpoint)
  64. if err != nil {
  65. log.Error("obs.New failed:", err)
  66. return err
  67. }
  68. log.Info("obs cli inited.")
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }