specifyBucket.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package minio
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net/url"
  8. "time"
  9. "github.com/minio/minio-go/v7"
  10. "go.uber.org/zap"
  11. )
  12. type MinioFileObject struct {
  13. ETag string `json:"etag"` // An ETag is optionally set to md5sum of an object.
  14. Key string `json:"name"` // Name of the object
  15. LastModified time.Time `json:"lastModified"` // Date and time the object was last modified.
  16. Size int64 `json:"size"` // Size in bytes of the object.
  17. ContentType string `json:"contentType"` // A standard MIME type describing the format of the object data.
  18. Expires time.Time `json:"expires"` // The date and time at which the object is no longer able to be cached.
  19. // Checksum values
  20. ChecksumCRC32 string `json:"checksumCRC32"`
  21. ChecksumCRC32C string `json:"checksumCRC32C"`
  22. ChecksumSHA1 string `json:"checksumSHA1"`
  23. ChecksumSHA256 string `json:"checksumSHA256"`
  24. ChecksumCRC64NVME string `json:"checksumCRC64NVME"`
  25. ChecksumMode string `json:"checksumMode"`
  26. }
  27. func UploadFileToBucket(bucketName, localPath, objectName string) error {
  28. ctx := context.Background()
  29. info, err := Client.FPutObject(ctx, bucketName, objectName, localPath, minio.PutObjectOptions{})
  30. if err != nil {
  31. return err
  32. }
  33. zap.L().Info(fmt.Sprintf("✅ Uploaded %s (%d bytes)", info.Key, info.Size))
  34. return nil
  35. }
  36. func DownloadFileFromBucket(bucketName, objectName, localPath string) error {
  37. ctx := context.Background()
  38. return Client.FGetObject(ctx, bucketName, objectName, localPath, minio.GetObjectOptions{})
  39. }
  40. func ListFilesFromBucket(bucketName, prefix string, recursive bool) ([]string, error) {
  41. ctx := context.Background()
  42. var result []string
  43. for obj := range Client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{
  44. Prefix: prefix, // 遍历 prefix 目录
  45. Recursive: recursive, // true - 递归所有文件, false - 只列出当前层级
  46. }) {
  47. if obj.Err != nil {
  48. return nil, obj.Err
  49. }
  50. result = append(result, obj.Key)
  51. }
  52. return result, nil
  53. }
  54. func ListFilesObjectFromBucket(bucketName, prefix string, recursive bool) ([]MinioFileObject, error) {
  55. ctx := context.Background()
  56. var result []MinioFileObject
  57. for obj := range Client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{
  58. Prefix: prefix, // 遍历 prefix 目录
  59. Recursive: recursive, // true - 递归所有文件, false - 只列出当前层级
  60. }) {
  61. if obj.Err != nil {
  62. return nil, obj.Err
  63. }
  64. result = append(result, MinioFileObject{
  65. ETag: obj.ETag,
  66. Key: obj.Key,
  67. LastModified: obj.LastModified,
  68. Size: obj.Size,
  69. ContentType: obj.ContentType,
  70. Expires: obj.Expires,
  71. // Checksum values
  72. ChecksumCRC32: obj.ChecksumCRC32,
  73. ChecksumCRC32C: obj.ChecksumCRC32C,
  74. ChecksumSHA1: obj.ChecksumSHA1,
  75. ChecksumSHA256: obj.ChecksumSHA256,
  76. ChecksumCRC64NVME: obj.ChecksumCRC64NVME,
  77. ChecksumMode: obj.ChecksumMode,
  78. })
  79. }
  80. return result, nil
  81. }
  82. func ListAllFilesFromBucket(bucketName string) ([]string, error) {
  83. ctx := context.Background()
  84. var result []string
  85. for obj := range Client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{Recursive: true}) {
  86. if obj.Err != nil {
  87. return nil, obj.Err
  88. }
  89. result = append(result, obj.Key)
  90. }
  91. return result, nil
  92. }
  93. func DeleteFileFromBucket(bucketName, objectName string) error {
  94. ctx := context.Background()
  95. return Client.RemoveObject(ctx, bucketName, objectName, minio.RemoveObjectOptions{})
  96. }
  97. func GetObjectFromBucket(bucketName, objectName string) (*minio.Object, error) {
  98. ctx := context.Background()
  99. return Client.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
  100. }
  101. // StatObject 会验证对象是否存在,您是否拥有访问该对象的权限,并返回有关该对象的信息。
  102. func StatObjectFromBucket(bucketName, objectName string) (minio.ObjectInfo, error) {
  103. ctx := context.Background()
  104. return Client.StatObject(ctx, bucketName, objectName, minio.StatObjectOptions{})
  105. }
  106. // 生成临时下载链接
  107. func GetPresignedURLFromBucket(bucketName, objectName string, expire time.Duration) (*url.URL, error) {
  108. ctx := context.Background()
  109. return Client.PresignedGetObject(ctx, bucketName, objectName, expire, nil)
  110. }
  111. // 从流上传(用于HTTP上传)
  112. func UploadFromReaderToBucket(bucketName string, file io.Reader, size int64, objectName, contentType string) error {
  113. ctx := context.Background()
  114. info, err := Client.PutObject(ctx, bucketName, objectName, file, size, minio.PutObjectOptions{
  115. ContentType: contentType,
  116. })
  117. if err != nil {
  118. return err
  119. }
  120. zap.L().Info(fmt.Sprintf("✅ Uploaded %s (%d bytes)", info.Key, info.Size))
  121. return nil
  122. }
  123. // 判断文件是否存在
  124. func ExistsObjectFromBucket(bucketName, objectName string) (bool, error) {
  125. _, err := StatObjectFromBucket(bucketName, objectName)
  126. if err != nil {
  127. // 对象不存在的判断(最常见)
  128. if minio.ToErrorResponse(err).Code == "NoSuchKey" {
  129. log.Printf("Object(%s) does NOT exist\n", objectName)
  130. return false, nil
  131. }
  132. return false, err
  133. }
  134. return true, nil
  135. }