| 1 | # 适用于与Amazon S3兼容云存储的MinIO Go SDK [](https://slack.min.io) [](https://sourcegraph.com/github.com/minio/minio-go?badge) |
| 2 | |
| 3 | MinIO Go Client SDK提供了简单的API来访问任何与Amazon S3兼容的对象存储服务。 |
| 4 | |
| 5 | **支持的云存储:** |
| 6 | |
| 7 | - AWS Signature Version 4 |
| 8 | - Amazon S3 |
| 9 | - MinIO |
| 10 | |
| 11 | - AWS Signature Version 2 |
| 12 | - Google Cloud Storage (兼容模式) |
| 13 | - Openstack Swift + Swift3 middleware |
| 14 | - Ceph Object Gateway |
| 15 | - Riak CS |
| 16 | |
| 17 | 本文我们将学习如何安装MinIO client SDK,连接到MinIO,并提供一下文件上传的示例。对于完整的API以及示例,请参考[Go Client API Reference](https://docs.min.io/docs/golang-client-api-reference)。 |
| 18 | |
| 19 | 本文假设你已经有 [Go开发环境](https://golang.org/doc/install)。 |
| 20 | |
| 21 | ## 从Github下载 |
| 22 | ```sh |
| 23 | go get -u github.com/minio/minio-go |
| 24 | ``` |
| 25 | |
| 26 | ## 初始化MinIO Client |
| 27 | MinIO client需要以下4个参数来连接与Amazon S3兼容的对象存储。 |
| 28 | |
| 29 | | 参数 | 描述| |
| 30 | | :--- | :--- | |
| 31 | | endpoint | 对象存储服务的URL | |
| 32 | | accessKeyID | Access key是唯一标识你的账户的用户ID。 | |
| 33 | | secretAccessKey | Secret key是你账户的密码。 | |
| 34 | | secure | true代表使用HTTPS | |
| 35 | |
| 36 | |
| 37 | ```go |
| 38 | package main |
| 39 | |
| 40 | import ( |
| 41 | "github.com/minio/minio-go/v6" |
| 42 | "log" |
| 43 | ) |
| 44 | |
| 45 | func main() { |
| 46 | endpoint := "play.min.io" |
| 47 | accessKeyID := "Q3AM3UQ867SPQQA43P2F" |
| 48 | secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" |
| 49 | useSSL := true |
| 50 | |
| 51 | // 初使化 minio client对象。 |
| 52 | minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL) |
| 53 | if err != nil { |
| 54 | log.Fatalln(err) |
| 55 | } |
| 56 | |
| 57 | log.Printf("%#v\n", minioClient) // minioClient初使化成功 |
| 58 | } |
| 59 | ``` |
| 60 | |
| 61 | ## 示例-文件上传 |
| 62 | 本示例连接到一个对象存储服务,创建一个存储桶并上传一个文件到存储桶中。 |
| 63 | |
| 64 | 我们在本示例中使用运行在 [https://play.min.io](https://play.min.io) 上的MinIO服务,你可以用这个服务来开发和测试。示例中的访问凭据是公开的。 |
| 65 | |
| 66 | ### FileUploader.go |
| 67 | ```go |
| 68 | package main |
| 69 | |
| 70 | import ( |
| 71 | "github.com/minio/minio-go/v6" |
| 72 | "log" |
| 73 | ) |
| 74 | |
| 75 | func main() { |
| 76 | endpoint := "play.min.io" |
| 77 | accessKeyID := "Q3AM3UQ867SPQQA43P2F" |
| 78 | secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" |
| 79 | useSSL := true |
| 80 | |
| 81 | // 初使化minio client对象。 |
| 82 | minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL) |
| 83 | if err != nil { |
| 84 | log.Fatalln(err) |
| 85 | } |
| 86 | |
| 87 | // 创建一个叫mymusic的存储桶。 |
| 88 | bucketName := "mymusic" |
| 89 | location := "us-east-1" |
| 90 | |
| 91 | err = minioClient.MakeBucket(bucketName, location) |
| 92 | if err != nil { |
| 93 | // 检查存储桶是否已经存在。 |
| 94 | exists, err := minioClient.BucketExists(bucketName) |
| 95 | if err == nil && exists { |
| 96 | log.Printf("We already own %s\n", bucketName) |
| 97 | } else { |
| 98 | log.Fatalln(err) |
| 99 | } |
| 100 | } |
| 101 | log.Printf("Successfully created %s\n", bucketName) |
| 102 | |
| 103 | // 上传一个zip文件。 |
| 104 | objectName := "golden-oldies.zip" |
| 105 | filePath := "/tmp/golden-oldies.zip" |
| 106 | contentType := "application/zip" |
| 107 | |
| 108 | // 使用FPutObject上传一个zip文件。 |
| 109 | n, err := minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{ContentType:contentType}) |
| 110 | if err != nil { |
| 111 | log.Fatalln(err) |
| 112 | } |
| 113 | |
| 114 | log.Printf("Successfully uploaded %s of size %d\n", objectName, n) |
| 115 | } |
| 116 | ``` |
| 117 | |
| 118 | ### 运行FileUploader |
| 119 | ```sh |
| 120 | go run file-uploader.go |
| 121 | 2016/08/13 17:03:28 Successfully created mymusic |
| 122 | 2016/08/13 17:03:40 Successfully uploaded golden-oldies.zip of size 16253413 |
| 123 | |
| 124 | mc ls play/mymusic/ |
| 125 | [2016-05-27 16:02:16 PDT] 17MiB golden-oldies.zip |
| 126 | ``` |
| 127 | |
| 128 | ## API文档 |
| 129 | 完整的API文档在这里。 |
| 130 | * [完整API文档](https://docs.min.io/docs/golang-client-api-reference) |
| 131 | |
| 132 | ### API文档 : 操作存储桶 |
| 133 | * [`MakeBucket`](https://docs.min.io/docs/golang-client-api-reference#MakeBucket) |
| 134 | * [`ListBuckets`](https://docs.min.io/docs/golang-client-api-reference#ListBuckets) |
| 135 | * [`BucketExists`](https://docs.min.io/docs/golang-client-api-reference#BucketExists) |
| 136 | * [`RemoveBucket`](https://docs.min.io/docs/golang-client-api-reference#RemoveBucket) |
| 137 | * [`ListObjects`](https://docs.min.io/docs/golang-client-api-reference#ListObjects) |
| 138 | * [`ListObjectsV2`](https://docs.min.io/docs/golang-client-api-reference#ListObjectsV2) |
| 139 | * [`ListIncompleteUploads`](https://docs.min.io/docs/golang-client-api-reference#ListIncompleteUploads) |
| 140 | |
| 141 | ### API文档 : 存储桶策略 |
| 142 | * [`SetBucketPolicy`](https://docs.min.io/docs/golang-client-api-reference#SetBucketPolicy) |
| 143 | * [`GetBucketPolicy`](https://docs.min.io/docs/golang-client-api-reference#GetBucketPolicy) |
| 144 | |
| 145 | ### API文档 : 存储桶通知 |
| 146 | * [`SetBucketNotification`](https://docs.min.io/docs/golang-client-api-reference#SetBucketNotification) |
| 147 | * [`GetBucketNotification`](https://docs.min.io/docs/golang-client-api-reference#GetBucketNotification) |
| 148 | * [`RemoveAllBucketNotification`](https://docs.min.io/docs/golang-client-api-reference#RemoveAllBucketNotification) |
| 149 | * [`ListenBucketNotification`](https://docs.min.io/docs/golang-client-api-reference#ListenBucketNotification) (MinIO Extension) |
| 150 | |
| 151 | ### API文档 : 操作文件对象 |
| 152 | * [`FPutObject`](https://docs.min.io/docs/golang-client-api-reference#FPutObject) |
| 153 | * [`FGetObject`](https://docs.min.io/docs/golang-client-api-reference#FPutObject) |
| 154 | * [`FPutObjectWithContext`](https://docs.min.io/docs/golang-client-api-reference#FPutObjectWithContext) |
| 155 | * [`FGetObjectWithContext`](https://docs.min.io/docs/golang-client-api-reference#FGetObjectWithContext) |
| 156 | |
| 157 | ### API文档 : 操作对象 |
| 158 | * [`GetObject`](https://docs.min.io/docs/golang-client-api-reference#GetObject) |
| 159 | * [`PutObject`](https://docs.min.io/docs/golang-client-api-reference#PutObject) |
| 160 | * [`GetObjectWithContext`](https://docs.min.io/docs/golang-client-api-reference#GetObjectWithContext) |
| 161 | * [`PutObjectWithContext`](https://docs.min.io/docs/golang-client-api-reference#PutObjectWithContext) |
| 162 | * [`PutObjectStreaming`](https://docs.min.io/docs/golang-client-api-reference#PutObjectStreaming) |
| 163 | * [`StatObject`](https://docs.min.io/docs/golang-client-api-reference#StatObject) |
| 164 | * [`CopyObject`](https://docs.min.io/docs/golang-client-api-reference#CopyObject) |
| 165 | * [`RemoveObject`](https://docs.min.io/docs/golang-client-api-reference#RemoveObject) |
| 166 | * [`RemoveObjects`](https://docs.min.io/docs/golang-client-api-reference#RemoveObjects) |
| 167 | * [`RemoveIncompleteUpload`](https://docs.min.io/docs/golang-client-api-reference#RemoveIncompleteUpload) |
| 168 | |
| 169 | ### API文档: 操作加密对象 |
| 170 | * [`GetEncryptedObject`](https://docs.min.io/docs/golang-client-api-reference#GetEncryptedObject) |
| 171 | * [`PutEncryptedObject`](https://docs.min.io/docs/golang-client-api-reference#PutEncryptedObject) |
| 172 | |
| 173 | ### API文档 : Presigned操作 |
| 174 | * [`PresignedGetObject`](https://docs.min.io/docs/golang-client-api-reference#PresignedGetObject) |
| 175 | * [`PresignedPutObject`](https://docs.min.io/docs/golang-client-api-reference#PresignedPutObject) |
| 176 | * [`PresignedHeadObject`](https://docs.min.io/docs/golang-client-api-reference#PresignedHeadObject) |
| 177 | * [`PresignedPostPolicy`](https://docs.min.io/docs/golang-client-api-reference#PresignedPostPolicy) |
| 178 | |
| 179 | ### API文档 : 客户端自定义设置 |
| 180 | * [`SetAppInfo`](http://docs.min.io/docs/golang-client-api-reference#SetAppInfo) |
| 181 | * [`SetCustomTransport`](http://docs.min.io/docs/golang-client-api-reference#SetCustomTransport) |
| 182 | * [`TraceOn`](http://docs.min.io/docs/golang-client-api-reference#TraceOn) |
| 183 | * [`TraceOff`](http://docs.min.io/docs/golang-client-api-reference#TraceOff) |
| 184 | |
| 185 | ## 完整示例 |
| 186 | |
| 187 | ### 完整示例 : 操作存储桶 |
| 188 | * [makebucket.go](https://github.com/minio/minio-go/blob/master/examples/s3/makebucket.go) |
| 189 | * [listbuckets.go](https://github.com/minio/minio-go/blob/master/examples/s3/listbuckets.go) |
| 190 | * [bucketexists.go](https://github.com/minio/minio-go/blob/master/examples/s3/bucketexists.go) |
| 191 | * [removebucket.go](https://github.com/minio/minio-go/blob/master/examples/s3/removebucket.go) |
| 192 | * [listobjects.go](https://github.com/minio/minio-go/blob/master/examples/s3/listobjects.go) |
| 193 | * [listobjectsV2.go](https://github.com/minio/minio-go/blob/master/examples/s3/listobjectsV2.go) |
| 194 | * [listincompleteuploads.go](https://github.com/minio/minio-go/blob/master/examples/s3/listincompleteuploads.go) |
| 195 | |
| 196 | ### 完整示例 : 存储桶策略 |
| 197 | * [setbucketpolicy.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketpolicy.go) |
| 198 | * [getbucketpolicy.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketpolicy.go) |
| 199 | * [listbucketpolicies.go](https://github.com/minio/minio-go/blob/master/examples/s3/listbucketpolicies.go) |
| 200 | |
| 201 | ### 完整示例 : 存储桶通知 |
| 202 | * [setbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketnotification.go) |
| 203 | * [getbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketnotification.go) |
| 204 | * [removeallbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeallbucketnotification.go) |
| 205 | * [listenbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/minio/listenbucketnotification.go) (MinIO扩展) |
| 206 | |
| 207 | ### 完整示例 : 操作文件对象 |
| 208 | * [fputobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/fputobject.go) |
| 209 | * [fgetobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/fgetobject.go) |
| 210 | * [fputobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/fputobject-context.go) |
| 211 | * [fgetobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/fgetobject-context.go) |
| 212 | |
| 213 | ### 完整示例 : 操作对象 |
| 214 | * [putobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/putobject.go) |
| 215 | * [getobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/getobject.go) |
| 216 | * [putobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/putobject-context.go) |
| 217 | * [getobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/getobject-context.go) |
| 218 | * [statobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/statobject.go) |
| 219 | * [copyobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/copyobject.go) |
| 220 | * [removeobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeobject.go) |
| 221 | * [removeincompleteupload.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeincompleteupload.go) |
| 222 | * [removeobjects.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeobjects.go) |
| 223 | |
| 224 | ### 完整示例 : 操作加密对象 |
| 225 | * [put-encrypted-object.go](https://github.com/minio/minio-go/blob/master/examples/s3/put-encrypted-object.go) |
| 226 | * [get-encrypted-object.go](https://github.com/minio/minio-go/blob/master/examples/s3/get-encrypted-object.go) |
| 227 | * [fput-encrypted-object.go](https://github.com/minio/minio-go/blob/master/examples/s3/fputencrypted-object.go) |
| 228 | |
| 229 | ### 完整示例 : Presigned操作 |
| 230 | * [presignedgetobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedgetobject.go) |
| 231 | * [presignedputobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedputobject.go) |
| 232 | * [presignedheadobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedheadobject.go) |
| 233 | * [presignedpostpolicy.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedpostpolicy.go) |
| 234 | |
| 235 | ## 了解更多 |
| 236 | * [完整文档](https://docs.min.io) |
| 237 | * [MinIO Go Client SDK API文档](https://docs.min.io/docs/golang-client-api-reference) |
| 238 | |
| 239 | ## 贡献 |
| 240 | [贡献指南](https://github.com/minio/minio-go/blob/master/docs/zh_CN/CONTRIBUTING.md) |
| 241 | |
| 242 | [](https://travis-ci.org/minio/minio-go) |
| 243 | [](https://ci.appveyor.com/project/harshavardhana/minio-go) |
| 244 |