Golang 上传远程 URL 图片到 Minio 存储
你可以使用以下代码将远程 URL 图片文件上传到 Minio:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func main() {
// Initialize Minio client object.
endpoint := 'play.min.io'
accessKeyID := 'Q3AM3UQ867SPQQA43P2F'
secretAccessKey := 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
useSSL := true
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ''),
Secure: useSSL,
})
if err != nil {
fmt.Println(err)
return
}
// Download image from remote URL.
url := 'https://www.example.com/image.jpg'
response, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
return
}
// Upload image to Minio.
bucketName := 'testbucket'
objectName := 'image.jpg'
contentType := 'image/jpeg'
_, err = minioClient.PutObject('testbucket', objectName, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{
ContentType: contentType,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf('Successfully uploaded %s to %s/%s\n', objectName, bucketName, objectName)
}
请注意,此示例代码假设您已安装minio-go库并导入它。此外,您需要替换accessKeyID,secretAccessKey,endpoint,bucketName和objectName变量的值以适应您的Minio实例和存储桶。
原文地址: https://www.cveoy.top/t/topic/lHT2 著作权归作者所有。请勿转载和采集!