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.6 kB

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