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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "strings"
  8. "github.com/minio/minio-go"
  9. )
  10. var (
  11. _ ObjectStorage = &MinioStorage{}
  12. )
  13. // MinioStorage returns a minio bucket storage
  14. type MinioStorage struct {
  15. client *minio.Client
  16. location string
  17. bucket string
  18. basePath string
  19. }
  20. // NewMinioStorage returns a minio storage
  21. func NewMinioStorage(endpoint, accessKeyID, secretAccessKey, location, bucket, basePath string, useSSL bool) (*MinioStorage, error) {
  22. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return &MinioStorage{
  27. location: location,
  28. client: minioClient,
  29. bucket: bucket,
  30. basePath: basePath,
  31. }, nil
  32. }
  33. func buildMinioPath(p string) string {
  34. return strings.TrimPrefix(p, "/")
  35. }
  36. // Open open a file
  37. func (m *MinioStorage) Open(path string) (io.ReadCloser, error) {
  38. var opts = minio.GetObjectOptions{}
  39. object, err := m.client.GetObject(m.bucket, buildMinioPath(path), opts)
  40. if err != nil {
  41. return nil, err
  42. }
  43. return object, nil
  44. }
  45. // Save save a file to minio
  46. func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
  47. return m.client.PutObject(m.bucket, buildMinioPath(path), r, -1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
  48. }
  49. // Delete delete a file
  50. func (m *MinioStorage) Delete(path string) error {
  51. return m.client.RemoveObject(m.bucket, buildMinioPath(path))
  52. }