go 判断minio是否存在文件
时间: 2023-12-21 20:04:13 浏览: 25
你可以使用Minio的Go SDK来判断一个文件是否存在。具体实现可以参考以下示例代码:
```go
package main
import (
"log"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
endpoint := "your-minio-endpoint"
accessKeyID := "your-access-key-id"
secretAccessKey := "your-secret-access-key"
useSSL := true // 是否启用 SSL
// 初始化 Minio 客户端对象
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
// 判断文件是否存在
bucketName := "your-bucket-name"
objectName := "your-object-name"
found, err := minioClient.ObjectExists(context.Background(), bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
log.Fatalln(err)
}
// 输出结果
if found {
log.Printf("File %s exists in bucket %s\n", objectName, bucketName)
} else {
log.Printf("File %s does not exist in bucket %s\n", objectName, bucketName)
}
}
```
其中,`minioClient.ObjectExists` 方法用于判断指定的文件是否存在,如果存在则返回 `true`,否则返回 `false`。你需要将 `your-minio-endpoint`、`your-access-key-id`、`your-secret-access-key`、`your-bucket-name` 和 `your-object-name` 替换为实际的值。