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.

minio.go 4.1 kB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "io"
  7. "net/url"
  8. "path"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "github.com/minio/minio-go"
  13. )
  14. var (
  15. _ ObjectStorage = &MinioStorage{}
  16. )
  17. const (
  18. PresignedGetUrlExpireTime = time.Hour * 24 * 7
  19. PresignedPutUrlExpireTime = time.Hour * 24 * 7
  20. )
  21. // MinioStorage returns a minio bucket storage
  22. type MinioStorage struct {
  23. client *minio.Client
  24. bucket string
  25. basePath string
  26. }
  27. // NewMinioStorage returns a minio storage
  28. func NewMinioStorage(endpoint, accessKeyID, secretAccessKey, bucket, location, basePath string, useSSL bool) (*MinioStorage, error) {
  29. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if err := minioClient.MakeBucket(bucket, location); err != nil {
  34. // Check to see if we already own this bucket (which happens if you run this twice)
  35. exists, errBucketExists := minioClient.BucketExists(bucket)
  36. if !exists || errBucketExists != nil {
  37. return nil, err
  38. }
  39. }
  40. return &MinioStorage{
  41. client: minioClient,
  42. bucket: bucket,
  43. basePath: basePath,
  44. }, nil
  45. }
  46. func (m *MinioStorage) buildMinioPath(p string) string {
  47. return strings.TrimPrefix(path.Join(m.basePath, p), "/")
  48. }
  49. // Open open a file
  50. func (m *MinioStorage) Open(path string) (io.ReadCloser, error) {
  51. var opts = minio.GetObjectOptions{}
  52. object, err := m.client.GetObject(m.bucket, m.buildMinioPath(path), opts)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return object, nil
  57. }
  58. // Save save a file to minio
  59. func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
  60. return m.client.PutObject(m.bucket, m.buildMinioPath(path), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
  61. }
  62. // Delete delete a file
  63. func (m *MinioStorage) Delete(path string) error {
  64. return m.client.RemoveObject(m.bucket, m.buildMinioPath(path))
  65. }
  66. // Delete delete a file
  67. func (m *MinioStorage) DeleteDir(dir string) error {
  68. objectsCh := make(chan string)
  69. // Send object names that are needed to be removed to objectsCh
  70. go func() {
  71. defer close(objectsCh)
  72. // List all objects from a bucket-name with a matching prefix.
  73. for object := range m.client.ListObjects(m.bucket, dir, true, nil) {
  74. if object.Err != nil {
  75. log.Error("ListObjects failed:%v", object.Err)
  76. }
  77. objectsCh <- object.Key
  78. }
  79. }()
  80. for rErr := range m.client.RemoveObjects(m.bucket, objectsCh) {
  81. log.Error("Error detected during deletion: ", rErr)
  82. }
  83. return nil
  84. }
  85. //Get Presigned URL for get object
  86. func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string, error) {
  87. // Set request parameters for content-disposition.
  88. reqParams := make(url.Values)
  89. reqParams.Set("response-content-disposition", "attachment; filename=\""+fileName+"\"")
  90. var preURL *url.URL
  91. preURL, err := m.client.PresignedGetObject(m.bucket, path, PresignedGetUrlExpireTime, reqParams)
  92. if err != nil {
  93. return "", err
  94. }
  95. return preURL.String(), nil
  96. }
  97. //Get Presigned URL for put object
  98. func (m *MinioStorage) PresignedPutURL(path string) (string, error) {
  99. var preURL *url.URL
  100. preURL, err := m.client.PresignedPutObject(m.bucket, m.buildMinioPath(path), PresignedPutUrlExpireTime)
  101. if err != nil {
  102. return "", err
  103. }
  104. return preURL.String(), nil
  105. }
  106. //check if has the object
  107. func (m *MinioStorage) HasObject(path string) (bool, error) {
  108. hasObject := false
  109. // Create a done channel to control 'ListObjects' go routine.
  110. doneCh := make(chan struct{})
  111. // Indicate to our routine to exit cleanly upon return.
  112. defer close(doneCh)
  113. objectCh := m.client.ListObjects(m.bucket, m.buildMinioPath(path), false, doneCh)
  114. for object := range objectCh {
  115. if object.Err != nil {
  116. return hasObject, object.Err
  117. }
  118. hasObject = true
  119. }
  120. return hasObject, nil
  121. }
  122. //upload object
  123. func (m *MinioStorage) UploadObject(fileName, filePath string) error {
  124. _, err := m.client.FPutObject(m.bucket, fileName, filePath, minio.PutObjectOptions{})
  125. return err
  126. }