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

5 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. "github.com/minio/minio-go"
  12. )
  13. var (
  14. _ ObjectStorage = &MinioStorage{}
  15. )
  16. const (
  17. PresignedGetUrlExpireTime = time.Hour * 24 * 7
  18. PresignedPutUrlExpireTime = time.Hour * 24 * 7
  19. )
  20. // MinioStorage returns a minio bucket storage
  21. type MinioStorage struct {
  22. client *minio.Client
  23. bucket string
  24. basePath string
  25. }
  26. // NewMinioStorage returns a minio storage
  27. func NewMinioStorage(endpoint, accessKeyID, secretAccessKey, bucket, location, basePath string, useSSL bool) (*MinioStorage, error) {
  28. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if err := minioClient.MakeBucket(bucket, location); err != nil {
  33. // Check to see if we already own this bucket (which happens if you run this twice)
  34. exists, errBucketExists := minioClient.BucketExists(bucket)
  35. if !exists || errBucketExists != nil {
  36. return nil, err
  37. }
  38. }
  39. return &MinioStorage{
  40. client: minioClient,
  41. bucket: bucket,
  42. basePath: basePath,
  43. }, nil
  44. }
  45. func (m *MinioStorage) buildMinioPath(p string) string {
  46. return strings.TrimPrefix(path.Join(m.basePath, p), "/")
  47. }
  48. // Open open a file
  49. func (m *MinioStorage) Open(path string) (io.ReadCloser, error) {
  50. var opts = minio.GetObjectOptions{}
  51. object, err := m.client.GetObject(m.bucket, m.buildMinioPath(path), opts)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return object, nil
  56. }
  57. // Save save a file to minio
  58. func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
  59. return m.client.PutObject(m.bucket, m.buildMinioPath(path), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
  60. }
  61. // Delete delete a file
  62. func (m *MinioStorage) Delete(path string) error {
  63. return m.client.RemoveObject(m.bucket, m.buildMinioPath(path))
  64. }
  65. //Get Presigned URL for get object
  66. func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string, error) {
  67. // Set request parameters for content-disposition.
  68. reqParams := make(url.Values)
  69. reqParams.Set("response-content-disposition", "attachment; filename=\""+fileName+"\"")
  70. var preURL *url.URL
  71. preURL, err := m.client.PresignedGetObject(m.bucket, m.buildMinioPath(path), PresignedGetUrlExpireTime, reqParams)
  72. if err != nil {
  73. return "", err
  74. }
  75. return preURL.String(), nil
  76. }
  77. //Get Presigned URL for put object
  78. func (m *MinioStorage) PresignedPutURL(path string) (string, error) {
  79. var preURL *url.URL
  80. preURL, err := m.client.PresignedPutObject(m.bucket, m.buildMinioPath(path), PresignedPutUrlExpireTime)
  81. if err != nil {
  82. return "", err
  83. }
  84. return preURL.String(), nil
  85. }
  86. //check if has the object
  87. func (m *MinioStorage) HasObject(path string) (bool, error) {
  88. hasObject := false
  89. // Create a done channel to control 'ListObjects' go routine.
  90. doneCh := make(chan struct{})
  91. // Indicate to our routine to exit cleanly upon return.
  92. defer close(doneCh)
  93. objectCh := m.client.ListObjects(m.bucket, m.buildMinioPath(path), false, doneCh)
  94. for object := range objectCh {
  95. if object.Err != nil {
  96. return hasObject, object.Err
  97. }
  98. hasObject = true
  99. }
  100. return hasObject, nil
  101. }