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.3 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
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // Open open a file
  51. func (m *MinioStorage) Open(path string) (io.ReadCloser, error) {
  52. var opts = minio.GetObjectOptions{}
  53. object, err := m.client.GetObject(m.bucket, m.buildMinioPath(path), opts)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return object, nil
  58. }
  59. // Save save a file to minio
  60. func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
  61. return m.client.PutObject(m.bucket, m.buildMinioPath(path), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
  62. }
  63. // Delete delete a file
  64. func (m *MinioStorage) Delete(path string) error {
  65. return m.client.RemoveObject(m.bucket, m.buildMinioPath(path))
  66. }
  67. // Delete delete a file
  68. func (m *MinioStorage) DeleteDir(dir string) error {
  69. objectsCh := make(chan string)
  70. // Send object names that are needed to be removed to objectsCh
  71. go func() {
  72. defer close(objectsCh)
  73. // List all objects from a bucket-name with a matching prefix.
  74. for object := range m.client.ListObjects(m.bucket, dir, true, nil) {
  75. if object.Err != nil {
  76. log.Error("ListObjects failed:%v", object.Err)
  77. }
  78. objectsCh <- object.Key
  79. }
  80. }()
  81. m.client.RemoveObjects(m.bucket, objectsCh)
  82. return nil
  83. }
  84. //Get Presigned URL for get object
  85. func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string, error) {
  86. // Set request parameters for content-disposition.
  87. reqParams := make(url.Values)
  88. reqParams.Set("response-content-disposition", "attachment; filename=\""+fileName+"\"")
  89. var preURL *url.URL
  90. preURL, err := m.client.PresignedGetObject(m.bucket, path, PresignedGetUrlExpireTime, reqParams)
  91. if err != nil {
  92. return "", err
  93. }
  94. return preURL.String(), nil
  95. }
  96. //Get Presigned URL for put object
  97. func (m *MinioStorage) PresignedPutURL(path string) (string, error) {
  98. var preURL *url.URL
  99. preURL, err := m.client.PresignedPutObject(m.bucket, m.buildMinioPath(path), PresignedPutUrlExpireTime)
  100. if err != nil {
  101. return "", err
  102. }
  103. return preURL.String(), nil
  104. }
  105. //check if has the object
  106. func (m *MinioStorage) HasObject(path string) (bool, error) {
  107. hasObject := false
  108. // Create a done channel to control 'ListObjects' go routine.
  109. doneCh := make(chan struct{})
  110. // Indicate to our routine to exit cleanly upon return.
  111. defer close(doneCh)
  112. objectCh := m.client.ListObjects(m.bucket, m.buildMinioPath(path), false, doneCh)
  113. for object := range objectCh {
  114. if object.Err != nil {
  115. return hasObject, object.Err
  116. }
  117. hasObject = true
  118. }
  119. return hasObject, nil
  120. }
  121. //upload object
  122. func (m *MinioStorage) UploadObject(fileName, filePath string) error {
  123. _, err := m.client.FPutObject(m.bucket, fileName, filePath, minio.PutObjectOptions{})
  124. return err
  125. }
  126. func GetMinioPath(jobName, suffixPath string) string {
  127. return setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.CBCodePathPrefix + jobName + suffixPath
  128. }