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

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 PRESIGNED_URL_EXPIRE_TIME = time.Hour * 24 * 7
  17. // MinioStorage returns a minio bucket storage
  18. type MinioStorage struct {
  19. client *minio.Client
  20. bucket string
  21. basePath string
  22. }
  23. // NewMinioStorage returns a minio storage
  24. func NewMinioStorage(endpoint, accessKeyID, secretAccessKey, bucket, location, basePath string, useSSL bool) (*MinioStorage, error) {
  25. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if err := minioClient.MakeBucket(bucket, location); err != nil {
  30. // Check to see if we already own this bucket (which happens if you run this twice)
  31. exists, errBucketExists := minioClient.BucketExists(bucket)
  32. if !exists || errBucketExists != nil {
  33. return nil, err
  34. }
  35. }
  36. return &MinioStorage{
  37. client: minioClient,
  38. bucket: bucket,
  39. basePath: basePath,
  40. }, nil
  41. }
  42. func (m *MinioStorage) buildMinioPath(p string) string {
  43. return strings.TrimPrefix(path.Join(m.basePath, p), "/")
  44. }
  45. // Open open a file
  46. func (m *MinioStorage) Open(path string) (io.ReadCloser, error) {
  47. var opts = minio.GetObjectOptions{}
  48. object, err := m.client.GetObject(m.bucket, m.buildMinioPath(path), opts)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return object, nil
  53. }
  54. // Save save a file to minio
  55. func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
  56. return m.client.PutObject(m.bucket, m.buildMinioPath(path), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
  57. }
  58. // Delete delete a file
  59. func (m *MinioStorage) Delete(path string) error {
  60. return m.client.RemoveObject(m.bucket, m.buildMinioPath(path))
  61. }
  62. //Get Presigned URL
  63. func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string,error) {
  64. // Set request parameters for content-disposition.
  65. reqParams := make(url.Values)
  66. reqParams.Set("response-content-disposition", "attachment; filename=\"" + fileName + "\"")
  67. var preURL *url.URL
  68. preURL,err := m.client.PresignedGetObject(m.bucket, m.buildMinioPath(path), PRESIGNED_URL_EXPIRE_TIME, reqParams)
  69. if err != nil {
  70. return "",err
  71. }
  72. return preURL.String(),nil
  73. }