README_zh_CN.md (12452B)
1 # 适用于与Amazon S3兼容云存储的MinIO Go SDK [![Slack](https://slack.min.io/slack?type=svg)](https://slack.min.io) [![Sourcegraph](https://sourcegraph.com/github.com/minio/minio-go/-/badge.svg)](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://min.io/docs/minio/linux/developers/go/API.html)。 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 "log" 42 43 "github.com/minio/minio-go/v7" 44 "github.com/minio/minio-go/v7/pkg/credentials" 45 ) 46 47 func main() { 48 endpoint := "play.min.io" 49 accessKeyID := "Q3AM3UQ867SPQQA43P2F" 50 secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" 51 useSSL := true 52 53 // 初使化 minio client对象。 54 minioClient, err := minio.New(endpoint, &minio.Options{ 55 Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), 56 Secure: useSSL, 57 }) 58 if err != nil { 59 log.Fatalln(err) 60 } 61 62 log.Printf("%#v\n", minioClient) // minioClient初使化成功 63 } 64 ``` 65 66 ## 示例-文件上传 67 本示例连接到一个对象存储服务,创建一个存储桶并上传一个文件到存储桶中。 68 69 我们在本示例中使用运行在 [https://play.min.io](https://play.min.io) 上的MinIO服务,你可以用这个服务来开发和测试。示例中的访问凭据是公开的。 70 71 ### FileUploader.go 72 ```go 73 package main 74 75 import ( 76 "context" 77 "log" 78 79 "github.com/minio/minio-go/v7" 80 "github.com/minio/minio-go/v7/pkg/credentials" 81 ) 82 83 func main() { 84 ctx := context.Background() 85 endpoint := "play.min.io" 86 accessKeyID := "Q3AM3UQ867SPQQA43P2F" 87 secretAccessKey := "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" 88 useSSL := true 89 90 // 初使化 minio client对象。 91 minioClient, err := minio.New(endpoint, &minio.Options{ 92 Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), 93 Secure: useSSL, 94 }) 95 if err != nil { 96 log.Fatalln(err) 97 } 98 99 // 创建一个叫mymusic的存储桶。 100 bucketName := "mymusic" 101 location := "us-east-1" 102 103 err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location}) 104 if err != nil { 105 // 检查存储桶是否已经存在。 106 exists, errBucketExists := minioClient.BucketExists(ctx, bucketName) 107 if errBucketExists == nil && exists { 108 log.Printf("We already own %s\n", bucketName) 109 } else { 110 log.Fatalln(err) 111 } 112 } else { 113 log.Printf("Successfully created %s\n", bucketName) 114 } 115 116 // 上传一个zip文件。 117 objectName := "golden-oldies.zip" 118 filePath := "/tmp/golden-oldies.zip" 119 contentType := "application/zip" 120 121 // 使用FPutObject上传一个zip文件。 122 n, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType}) 123 if err != nil { 124 log.Fatalln(err) 125 } 126 127 log.Printf("Successfully uploaded %s of size %d\n", objectName, n) 128 } 129 ``` 130 131 ### 运行FileUploader 132 ```sh 133 go run file-uploader.go 134 2016/08/13 17:03:28 Successfully created mymusic 135 2016/08/13 17:03:40 Successfully uploaded golden-oldies.zip of size 16253413 136 137 mc ls play/mymusic/ 138 [2016-05-27 16:02:16 PDT] 17MiB golden-oldies.zip 139 ``` 140 141 ## API文档 142 完整的API文档在这里。 143 * [完整API文档](https://min.io/docs/minio/linux/developers/go/API.html) 144 145 ### API文档 : 操作存储桶 146 * [`MakeBucket`](https://min.io/docs/minio/linux/developers/go/API.html#MakeBucket) 147 * [`ListBuckets`](https://min.io/docs/minio/linux/developers/go/API.html#ListBuckets) 148 * [`BucketExists`](https://min.io/docs/minio/linux/developers/go/API.html#BucketExists) 149 * [`RemoveBucket`](https://min.io/docs/minio/linux/developers/go/API.html#RemoveBucket) 150 * [`ListObjects`](https://min.io/docs/minio/linux/developers/go/API.html#ListObjects) 151 * [`ListIncompleteUploads`](https://min.io/docs/minio/linux/developers/go/API.html#ListIncompleteUploads) 152 153 ### API文档 : 存储桶策略 154 * [`SetBucketPolicy`](https://min.io/docs/minio/linux/developers/go/API.html#SetBucketPolicy) 155 * [`GetBucketPolicy`](https://min.io/docs/minio/linux/developers/go/API.html#GetBucketPolicy) 156 157 ### API文档 : 存储桶通知 158 * [`SetBucketNotification`](https://min.io/docs/minio/linux/developers/go/API.html#SetBucketNotification) 159 * [`GetBucketNotification`](https://min.io/docs/minio/linux/developers/go/API.html#GetBucketNotification) 160 * [`RemoveAllBucketNotification`](https://min.io/docs/minio/linux/developers/go/API.html#RemoveAllBucketNotification) 161 * [`ListenBucketNotification`](https://min.io/docs/minio/linux/developers/go/API.html#ListenBucketNotification) (MinIO 扩展) 162 * [`ListenNotification`](https://min.io/docs/minio/linux/developers/go/API.html#ListenNotification) (MinIO 扩展) 163 164 ### API文档 : 操作文件对象 165 * [`FPutObject`](https://min.io/docs/minio/linux/developers/go/API.html#FPutObject) 166 * [`FGetObject`](https://min.io/docs/minio/linux/developers/go/API.html#FPutObject) 167 168 ### API文档 : 操作对象 169 * [`GetObject`](https://min.io/docs/minio/linux/developers/go/API.html#GetObject) 170 * [`PutObject`](https://min.io/docs/minio/linux/developers/go/API.html#PutObject) 171 * [`PutObjectStreaming`](https://min.io/docs/minio/linux/developers/go/API.html#PutObjectStreaming) 172 * [`StatObject`](https://min.io/docs/minio/linux/developers/go/API.html#StatObject) 173 * [`CopyObject`](https://min.io/docs/minio/linux/developers/go/API.html#CopyObject) 174 * [`RemoveObject`](https://min.io/docs/minio/linux/developers/go/API.html#RemoveObject) 175 * [`RemoveObjects`](https://min.io/docs/minio/linux/developers/go/API.html#RemoveObjects) 176 * [`RemoveIncompleteUpload`](https://min.io/docs/minio/linux/developers/go/API.html#RemoveIncompleteUpload) 177 * [`SelectObjectContent`](https://min.io/docs/minio/linux/developers/go/API.html#SelectObjectContent) 178 179 ### API文档 : Presigned操作 180 * [`PresignedGetObject`](https://min.io/docs/minio/linux/developers/go/API.html#PresignedGetObject) 181 * [`PresignedPutObject`](https://min.io/docs/minio/linux/developers/go/API.html#PresignedPutObject) 182 * [`PresignedHeadObject`](https://min.io/docs/minio/linux/developers/go/API.html#PresignedHeadObject) 183 * [`PresignedPostPolicy`](https://min.io/docs/minio/linux/developers/go/API.html#PresignedPostPolicy) 184 185 ### API文档 : 客户端自定义设置 186 * [`SetAppInfo`](https://min.io/docs/minio/linux/developers/go/API.html#SetAppInfo) 187 * [`TraceOn`](https://min.io/docs/minio/linux/developers/go/API.html#TraceOn) 188 * [`TraceOff`](https://min.io/docs/minio/linux/developers/go/API.html#TraceOff) 189 190 ## 完整示例 191 192 ### 完整示例 : 操作存储桶 193 * [makebucket.go](https://github.com/minio/minio-go/blob/master/examples/s3/makebucket.go) 194 * [listbuckets.go](https://github.com/minio/minio-go/blob/master/examples/s3/listbuckets.go) 195 * [bucketexists.go](https://github.com/minio/minio-go/blob/master/examples/s3/bucketexists.go) 196 * [removebucket.go](https://github.com/minio/minio-go/blob/master/examples/s3/removebucket.go) 197 * [listobjects.go](https://github.com/minio/minio-go/blob/master/examples/s3/listobjects.go) 198 * [listobjectsV2.go](https://github.com/minio/minio-go/blob/master/examples/s3/listobjectsV2.go) 199 * [listincompleteuploads.go](https://github.com/minio/minio-go/blob/master/examples/s3/listincompleteuploads.go) 200 201 ### 完整示例 : 存储桶策略 202 * [setbucketpolicy.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketpolicy.go) 203 * [getbucketpolicy.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketpolicy.go) 204 * [listbucketpolicies.go](https://github.com/minio/minio-go/blob/master/examples/s3/listbucketpolicies.go) 205 206 ### 完整示例 : 存储桶生命周期 207 * [setbucketlifecycle.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketlifecycle.go) 208 * [getbucketlifecycle.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketlifecycle.go) 209 210 ### 完整示例 : 存储桶加密 211 * [setbucketencryption.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketencryption.go) 212 * [getbucketencryption.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketencryption.go) 213 * [deletebucketencryption.go](https://github.com/minio/minio-go/blob/master/examples/s3/deletebucketencryption.go) 214 215 ### 完整示例 : 存储桶复制 216 * [setbucketreplication.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketreplication.go) 217 * [getbucketreplication.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketreplication.go) 218 * [removebucketreplication.go](https://github.com/minio/minio-go/blob/master/examples/s3/removebucketreplication.go) 219 220 ### 完整示例 : 存储桶通知 221 * [setbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/s3/setbucketnotification.go) 222 * [getbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/s3/getbucketnotification.go) 223 * [removeallbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeallbucketnotification.go) 224 * [listenbucketnotification.go](https://github.com/minio/minio-go/blob/master/examples/minio/listenbucketnotification.go) (MinIO扩展) 225 * [listennotification.go](https://github.com/minio/minio-go/blob/master/examples/minio/listen-notification.go) (MinIO 扩展) 226 227 ### 完整示例 : 操作文件对象 228 * [fputobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/fputobject.go) 229 * [fgetobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/fgetobject.go) 230 * [fputobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/fputobject-context.go) 231 * [fgetobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/fgetobject-context.go) 232 233 ### 完整示例 : 操作对象 234 * [putobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/putobject.go) 235 * [getobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/getobject.go) 236 * [putobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/putobject-context.go) 237 * [getobject-context.go](https://github.com/minio/minio-go/blob/master/examples/s3/getobject-context.go) 238 * [statobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/statobject.go) 239 * [copyobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/copyobject.go) 240 * [removeobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeobject.go) 241 * [removeincompleteupload.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeincompleteupload.go) 242 * [removeobjects.go](https://github.com/minio/minio-go/blob/master/examples/s3/removeobjects.go) 243 244 ### 完整示例 : 操作加密对象 245 * [put-encrypted-object.go](https://github.com/minio/minio-go/blob/master/examples/s3/put-encrypted-object.go) 246 * [get-encrypted-object.go](https://github.com/minio/minio-go/blob/master/examples/s3/get-encrypted-object.go) 247 * [fput-encrypted-object.go](https://github.com/minio/minio-go/blob/master/examples/s3/fputencrypted-object.go) 248 249 ### 完整示例 : Presigned操作 250 * [presignedgetobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedgetobject.go) 251 * [presignedputobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedputobject.go) 252 * [presignedheadobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedheadobject.go) 253 * [presignedpostpolicy.go](https://github.com/minio/minio-go/blob/master/examples/s3/presignedpostpolicy.go) 254 255 ## 了解更多 256 * [完整文档](https://min.io/docs/minio/kubernetes/upstream/index.html) 257 * [MinIO Go Client SDK API文档](https://min.io/docs/minio/linux/developers/go/API.html) 258 259 ## 贡献 260 [贡献指南](https://github.com/minio/minio-go/blob/master/docs/zh_CN/CONTRIBUTING.md)