Skip to content

Latest commit

 

History

History
102 lines (81 loc) · 2.73 KB

zh_CN_aws-sdk-for-go-with-minio.md

File metadata and controls

102 lines (81 loc) · 2.73 KB

如何使用AWS SDK for Go操作MinIO Server Slack

aws-sdk-go是GO语言版本的官方AWS SDK。本文将学习如何使用aws-sdk-go来操作MinIO Server。

1. 前提条件

这里下载并安装MinIO Server。

2. 安装

AWS SDK for GO官方文档下载将安装aws-sdk-go

3. 示例

替换example.go文件中的Endpoint,Credentials, Bucket配置成你的本地配置。

下面的示例讲的是如何使用aws-sdk-go从MinIO Server上putObject和getObject。

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/service/s3/s3manager"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
)

func main() {
	bucket := aws.String("newbucket")
	key := aws.String("testobject")
	
	//  配置成使用MinIO Server。
	s3Config := &aws.Config{
		Credentials:      credentials.NewStaticCredentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", ""),
		Endpoint:         aws.String("http://localhost:9000"),
		Region:           aws.String("us-east-1"),
		DisableSSL:       aws.Bool(true),
		S3ForcePathStyle: aws.Bool(true),
	}
	newSession := session.New(s3Config)

	s3Client := s3.New(newSession)

	cparams := &s3.CreateBucketInput{
		Bucket: bucket, // 必须
	}

	// 调用CreateBucket创建一个新的存储桶。
	_, err := s3Client.CreateBucket(cparams)
	if err != nil {
		// 错误信息
		fmt.Println(err.Error())
		return
	}

	// 上传一个新的文件"testobject"到存储桶"newbucket",内容是"Hello World!" 。
	_, err = s3Client.PutObject(&s3.PutObjectInput{
		Body:   strings.NewReader("Hello from MinIO!!"),
		Bucket: bucket,
		Key:    key,
	})
	if err != nil {
		fmt.Printf("Failed to upload data to %s/%s, %s\n", *bucket, *key, err.Error())
		return
	}
	fmt.Printf("Successfully created bucket %s and uploaded data with key %s\n", *bucket, *key)

	// 从 "newbucket"里获取文件"testobject",并保存到本地文件"testobject_local"。
	file, err := os.Create("testobject_local")
	if err != nil {
	    fmt.Println("Failed to create file", err)
		return
	}
	defer file.Close()
	
	downloader := s3manager.NewDownloader(newSession)
	numBytes, err := downloader.Download(file,
	&s3.GetObjectInput{
		Bucket: bucket,
		Key:    key,
	})
	if err != nil {
		fmt.Println("Failed to download file", err)
		return
	}
	fmt.Println("Downloaded file", file.Name(), numBytes, "bytes")
}

4. 运行程序

go run example.go
Successfully created bucket newbucket and uploaded data with key testobject
Downloaded file testobject_local 18 bytes