package minio import ( "context" "fmt" "io" "log" "net/url" "time" "github.com/minio/minio-go/v7" "go.uber.org/zap" ) type MinioFileObject struct { ETag string `json:"etag"` // An ETag is optionally set to md5sum of an object. Key string `json:"name"` // Name of the object LastModified time.Time `json:"lastModified"` // Date and time the object was last modified. Size int64 `json:"size"` // Size in bytes of the object. ContentType string `json:"contentType"` // A standard MIME type describing the format of the object data. Expires time.Time `json:"expires"` // The date and time at which the object is no longer able to be cached. // Checksum values ChecksumCRC32 string `json:"checksumCRC32"` ChecksumCRC32C string `json:"checksumCRC32C"` ChecksumSHA1 string `json:"checksumSHA1"` ChecksumSHA256 string `json:"checksumSHA256"` ChecksumCRC64NVME string `json:"checksumCRC64NVME"` ChecksumMode string `json:"checksumMode"` } func UploadFileToBucket(bucketName, localPath, objectName string) error { ctx := context.Background() info, err := Client.FPutObject(ctx, bucketName, objectName, localPath, minio.PutObjectOptions{}) if err != nil { return err } zap.L().Info(fmt.Sprintf("✅ Uploaded %s (%d bytes)", info.Key, info.Size)) return nil } func DownloadFileFromBucket(bucketName, objectName, localPath string) error { ctx := context.Background() return Client.FGetObject(ctx, bucketName, objectName, localPath, minio.GetObjectOptions{}) } func ListFilesFromBucket(bucketName, prefix string, recursive bool) ([]string, error) { ctx := context.Background() var result []string for obj := range Client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{ Prefix: prefix, // 遍历 prefix 目录 Recursive: recursive, // true - 递归所有文件, false - 只列出当前层级 }) { if obj.Err != nil { return nil, obj.Err } result = append(result, obj.Key) } return result, nil } func ListFilesObjectFromBucket(bucketName, prefix string, recursive bool) ([]MinioFileObject, error) { ctx := context.Background() var result []MinioFileObject for obj := range Client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{ Prefix: prefix, // 遍历 prefix 目录 Recursive: recursive, // true - 递归所有文件, false - 只列出当前层级 }) { if obj.Err != nil { return nil, obj.Err } result = append(result, MinioFileObject{ ETag: obj.ETag, Key: obj.Key, LastModified: obj.LastModified, Size: obj.Size, ContentType: obj.ContentType, Expires: obj.Expires, // Checksum values ChecksumCRC32: obj.ChecksumCRC32, ChecksumCRC32C: obj.ChecksumCRC32C, ChecksumSHA1: obj.ChecksumSHA1, ChecksumSHA256: obj.ChecksumSHA256, ChecksumCRC64NVME: obj.ChecksumCRC64NVME, ChecksumMode: obj.ChecksumMode, }) } return result, nil } func ListAllFilesFromBucket(bucketName string) ([]string, error) { ctx := context.Background() var result []string for obj := range Client.ListObjects(ctx, bucketName, minio.ListObjectsOptions{Recursive: true}) { if obj.Err != nil { return nil, obj.Err } result = append(result, obj.Key) } return result, nil } func DeleteFileFromBucket(bucketName, objectName string) error { ctx := context.Background() return Client.RemoveObject(ctx, bucketName, objectName, minio.RemoveObjectOptions{}) } func GetObjectFromBucket(bucketName, objectName string) (*minio.Object, error) { ctx := context.Background() return Client.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{}) } // StatObject 会验证对象是否存在,您是否拥有访问该对象的权限,并返回有关该对象的信息。 func StatObjectFromBucket(bucketName, objectName string) (minio.ObjectInfo, error) { ctx := context.Background() return Client.StatObject(ctx, bucketName, objectName, minio.StatObjectOptions{}) } // 生成临时下载链接 func GetPresignedURLFromBucket(bucketName, objectName string, expire time.Duration) (*url.URL, error) { ctx := context.Background() return Client.PresignedGetObject(ctx, bucketName, objectName, expire, nil) } // 从流上传(用于HTTP上传) func UploadFromReaderToBucket(bucketName string, file io.Reader, size int64, objectName, contentType string) error { ctx := context.Background() info, err := Client.PutObject(ctx, bucketName, objectName, file, size, minio.PutObjectOptions{ ContentType: contentType, }) if err != nil { return err } zap.L().Info(fmt.Sprintf("✅ Uploaded %s (%d bytes)", info.Key, info.Size)) return nil } // 判断文件是否存在 func ExistsObjectFromBucket(bucketName, objectName string) (bool, error) { _, err := StatObjectFromBucket(bucketName, objectName) if err != nil { // 对象不存在的判断(最常见) if minio.ToErrorResponse(err).Code == "NoSuchKey" { log.Printf("Object(%s) does NOT exist\n", objectName) return false, nil } return false, err } return true, nil }