minio提供了多种语言的SDK(https://min.io/docs/minio/linux/developers/minio-drivers.html),这里我选择Go SDK(https://www.bookstack.cn/read/MinioCookbookZH/26.md

创建Client

ctx := context.Background()
endpoint := "localhost:9090"
accessKeyID := "对应用户的AK"
secretAccessKey := "对应用户的SK"
useSSL := false

// Initialize minio client object.
minioClient, err := minio.New(
	endpoint,
	&minio.Options{
		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
		Secure: useSSL,
	},
)
if err != nil {
	fmt.Println(err)
	return
}

bucketName := "images"

// Check to see if we already own this bucket (which happens if you run this twice)
exists, err := minioClient.BucketExists(ctx, bucketName)
if err != nil || !exists {
	fmt.Println(err)
	return
}

创建Bucket

// Make a new bucket called testbucket.
bucketName := "testbucket"
location := "us-east-1"

err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
	// Check to see if we already own this bucket (which happens if you run this twice)
	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
	if errBucketExists == nil && exists {
		fmt.Printf("We already own %s\n", bucketName)
	} else {
		fmt.Println(err)
		return 
	}
} else {
	fmt.Printf("Successfully created %s\n", bucketName)
}

上传Object

n, err := minioClient.FPutObject(ctx, "my-bucketname", "my-objectname", "my-filename.png", minio.PutObjectOptions{
    ContentType: "application/octet-stream",
});
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println("Successfully uploaded bytes: ", n)

获取Object

err = minioClient.FGetObject(ctx, "mybucket", "myobject", "/tmp/myobject", minio.GetObjectOptions{})
if err != nil {
    fmt.Println(err)
    return
}

临时访问

生成一个用于HTTP GET操作的presigned URL。浏览器/移动客户端可以在即使存储桶为私有的情况下也可以通过这个URL进行下载。这个presigned URL可以有一个过期时间,默认是7天

// Set request parameters for content-disposition.
reqParams := make(url.Values)
reqParams.Set("response-content-disposition", "attachment; filename=\"your-filename.txt\"")
// Generates a presigned url which expires in a day.
presignedURL, err := minioClient.PresignedGetObject("mybucket", "myobject", time.Second * 24 * 60 * 60, reqParams)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Println("Successfully generated presigned URL", presignedURL)
0

本文为原创文章,转载请注明出处,欢迎访问作者网站(和而不同)

发表评论

error: Content is protected !!