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 3.7 kB

5 years ago
4 years ago
5 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
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/setting"
  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. //Get Presigned URL for get object
  67. func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string, error) {
  68. // Set request parameters for content-disposition.
  69. reqParams := make(url.Values)
  70. reqParams.Set("response-content-disposition", "attachment; filename=\""+fileName+"\"")
  71. var preURL *url.URL
  72. preURL, err := m.client.PresignedGetObject(m.bucket, path, PresignedGetUrlExpireTime, reqParams)
  73. if err != nil {
  74. return "", err
  75. }
  76. return preURL.String(), nil
  77. }
  78. //Get Presigned URL for put object
  79. func (m *MinioStorage) PresignedPutURL(path string) (string, error) {
  80. var preURL *url.URL
  81. preURL, err := m.client.PresignedPutObject(m.bucket, m.buildMinioPath(path), PresignedPutUrlExpireTime)
  82. if err != nil {
  83. return "", err
  84. }
  85. return preURL.String(), nil
  86. }
  87. //check if has the object
  88. func (m *MinioStorage) HasObject(path string) (bool, error) {
  89. hasObject := false
  90. // Create a done channel to control 'ListObjects' go routine.
  91. doneCh := make(chan struct{})
  92. // Indicate to our routine to exit cleanly upon return.
  93. defer close(doneCh)
  94. objectCh := m.client.ListObjects(m.bucket, m.buildMinioPath(path), false, doneCh)
  95. for object := range objectCh {
  96. if object.Err != nil {
  97. return hasObject, object.Err
  98. }
  99. hasObject = true
  100. }
  101. return hasObject, nil
  102. }
  103. //upload object
  104. func (m *MinioStorage) UploadObject(fileName, filePath string) error {
  105. _, err := m.client.FPutObject(m.bucket, fileName, filePath, minio.PutObjectOptions{})
  106. return err
  107. }
  108. func GetMinioPath(jobName, suffixPath string) string {
  109. return setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + "/" + setting.CBCodePathPrefix + jobName + suffixPath
  110. }