1# Minio Go Client API文档 [![Slack](https://slack.minio.io/slack?type=svg)](https://slack.minio.io)
2
3## 初使化Minio Client对象。
4
5##  Minio
6
7```go
8package main
9
10import (
11    "fmt"
12
13    "github.com/minio/minio-go"
14)
15
16func main() {
17        // 使用ssl
18        ssl := true
19
20        // 初使化minio client对象。
21        minioClient, err := minio.New("play.minio.io:9000", "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", ssl)
22        if err != nil {
23                fmt.Println(err)
24                return
25        }
26}
27```
28
29## AWS S3
30
31```go
32package main
33
34import (
35    "fmt"
36
37    "github.com/minio/minio-go"
38)
39
40func main() {
41        // 使用ssl
42        ssl := true
43
44        // 初使化minio client对象。
45        s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", ssl)
46        if err != nil {
47                fmt.Println(err)
48                return
49        }
50}
51```
52
53| 操作存储桶                                 | 操作对象                                  | 操作加密对象                 | Presigned操作                          | 存储桶策略/通知                         | 客户端自定义设置 |
54| :---                                              | :---                                                | :---                                        | :---                                          | :---                                                          | :---                                                  |
55| [`MakeBucket`](#MakeBucket)                       | [`GetObject`](#GetObject)                           | [`NewSymmetricKey`](#NewSymmetricKey)       | [`PresignedGetObject`](#PresignedGetObject)   | [`SetBucketPolicy`](#SetBucketPolicy)                         | [`SetAppInfo`](#SetAppInfo)                           |
56| [`ListBuckets`](#ListBuckets)                     | [`PutObject`](#PutObject)                           | [`NewAsymmetricKey`](#NewAsymmetricKey)     | [`PresignedPutObject`](#PresignedPutObject)   | [`GetBucketPolicy`](#GetBucketPolicy)                         | [`SetCustomTransport`](#SetCustomTransport)           |
57| [`BucketExists`](#BucketExists)                   | [`CopyObject`](#CopyObject)                         | [`GetEncryptedObject`](#GetEncryptedObject) | [`PresignedPostPolicy`](#PresignedPostPolicy) | [`SetBucketNotification`](#SetBucketNotification)                     | [`TraceOn`](#TraceOn)                                 |
58| [`RemoveBucket`](#RemoveBucket)                   | [`StatObject`](#StatObject)                         | [`PutEncryptedObject`](#PutEncryptedObject) |                                               | [`GetBucketNotification`](#GetBucketNotification)           | [`TraceOff`](#TraceOff)                               |
59| [`ListObjects`](#ListObjects)                     | [`RemoveObject`](#RemoveObject)                     | [`NewSSEInfo`](#NewSSEInfo)               |                                               | [`RemoveAllBucketNotification`](#RemoveAllBucketNotification)             | [`SetS3TransferAccelerate`](#SetS3TransferAccelerate) |
60| [`ListObjectsV2`](#ListObjectsV2)                 | [`RemoveObjects`](#RemoveObjects)                   | [`FPutEncryptedObject`](#FPutEncryptedObject)    |                                               | [`ListenBucketNotification`](#ListenBucketNotification) |                                                       |
61| [`ListIncompleteUploads`](#ListIncompleteUploads) | [`RemoveIncompleteUpload`](#RemoveIncompleteUpload) |                                             |                                               |        |                                                       |
62|                                                   | [`FPutObject`](#FPutObject)                         |                                             |                                               |                                                               |                                                       |
63|                                                   | [`FGetObject`](#FGetObject)                         |                                             |                                               |                                                               |                                                       |
64|                                                   | [`ComposeObject`](#ComposeObject)                   |                                             |                                               |                                                               |                                                       |
65|                                                   | [`NewSourceInfo`](#NewSourceInfo)                   |                                             |                                               |                                                               |                                                       |
66|                                                   | [`NewDestinationInfo`](#NewDestinationInfo)         |                                             |                                               |                                                               |                                                       |
67|   | [`PutObjectWithContext`](#PutObjectWithContext)  | |   |   |
68|   | [`GetObjectWithContext`](#GetObjectWithContext)  | |   |   |
69|   | [`FPutObjectWithContext`](#FPutObjectWithContext)  | |   |   |
70|   | [`FGetObjectWithContext`](#FGetObjectWithContext)  | |   |   |
71## 1. 构造函数
72<a name="Minio"></a>
73
74### New(endpoint, accessKeyID, secretAccessKey string, ssl bool) (*Client, error)
75初使化一个新的client对象。
76
77__参数__
78
79|参数   | 类型   |描述   |
80|:---|:---| :---|
81|`endpoint`   | _string_  |S3兼容对象存储服务endpoint   |
82|`accessKeyID`  |_string_   |对象存储的Access key |
83|`secretAccessKey`  | _string_  |对象存储的Secret key |
84|`ssl`   | _bool_  |true代表使用HTTPS |
85
86### NewWithRegion(endpoint, accessKeyID, secretAccessKey string, ssl bool, region string) (*Client, error)
87初使化minio client,带有region配置。和New()不同的是,NewWithRegion避免了bucket-location操作,所以会快那么一丢丢。如果你的应用只使用一个region的话可以用这个方法。
88
89__参数__
90
91|参数   |类型   |描述   |
92|:---|:---| :---|
93|`endpoint`   | _string_  |S3兼容对象存储服务endpoint |
94|`accessKeyID`  |_string_   |对象存储的Access key |
95|`secretAccessKey`  | _string_  |对象存储的Secret key |
96|`ssl` | _bool_  |true代表使用HTTPS |
97|`region`| _string_ | 对象存储的region |
98
99## 2. 操作存储桶
100
101<a name="MakeBucket"></a>
102### MakeBucket(bucketName, location string) error
103创建一个存储桶。
104
105__参数__
106
107| 参数  | 类型  | 描述  |
108|---|---|---|
109|`bucketName`  | _string_  | 存储桶名称 |
110| `location`  |  _string_ | 存储桶被创建的region(地区),默认是us-east-1(美国东一区),下面列举的是其它合法的值。注意:如果用的是minio服务的话,resion是在它的配置文件中,(默认是us-east-1)。|
111| | |us-east-1 |
112| | |us-west-1 |
113| | |us-west-2 |
114| | |eu-west-1 |
115| | | eu-central-1|
116| | | ap-southeast-1|
117| | | ap-northeast-1|
118| | | ap-southeast-2|
119| | | sa-east-1|
120
121
122__示例__
123
124
125```go
126err = minioClient.MakeBucket("mybucket", "us-east-1")
127if err != nil {
128    fmt.Println(err)
129    return
130}
131fmt.Println("Successfully created mybucket.")
132```
133
134<a name="ListBuckets"></a>
135### ListBuckets() ([]BucketInfo, error)
136列出所有的存储桶。
137
138| 参数  | 类型   | 描述  |
139|---|---|---|
140|`bucketList`  | _[]minio.BucketInfo_  | 所有存储桶的list。 |
141
142
143__minio.BucketInfo__
144
145| 参数  | 类型   | 描述  |
146|---|---|---|
147|`bucket.Name`  | _string_  | 存储桶名称 |
148|`bucket.CreationDate`  | _time.Time_  | 存储桶的创建时间 |
149
150
151__示例__
152
153
154```go
155buckets, err := minioClient.ListBuckets()
156if err != nil {
157    fmt.Println(err)
158    return
159}
160for _, bucket := range buckets {
161    fmt.Println(bucket)
162}
163```
164
165<a name="BucketExists"></a>
166### BucketExists(bucketName string) (found bool, err error)
167检查存储桶是否存在。
168
169__参数__
170
171
172|参数   |类型   |描述   |
173|:---|:---| :---|
174|`bucketName`  | _string_  |存储桶名称 |
175
176
177__返回值__
178
179|参数   |类型   |描述   |
180|:---|:---| :---|
181|`found`  | _bool_ | 存储桶是否存在  |
182|`err` | _error_  | 标准Error  |
183
184
185__示例__
186
187
188```go
189found, err := minioClient.BucketExists("mybucket")
190if err != nil {
191    fmt.Println(err)
192    return
193}
194if found {
195    fmt.Println("Bucket found")
196}
197```
198
199<a name="RemoveBucket"></a>
200### RemoveBucket(bucketName string) error
201删除一个存储桶,存储桶必须为空才能被成功删除。
202
203__参数__
204
205
206|参数   |类型   |描述   |
207|:---|:---| :---|
208|`bucketName`  | _string_  |存储桶名称   |
209
210__示例__
211
212
213```go
214err = minioClient.RemoveBucket("mybucket")
215if err != nil {
216    fmt.Println(err)
217    return
218}
219```
220
221<a name="ListObjects"></a>
222### ListObjects(bucketName, prefix string, recursive bool, doneCh chan struct{}) <-chan ObjectInfo
223列举存储桶里的对象。
224
225__参数__
226
227
228|参数   |类型   |描述   |
229|:---|:---| :---|
230|`bucketName` | _string_  |存储桶名称   |
231|`objectPrefix` |_string_   | 要列举的对象前缀 |
232|`recursive`  | _bool_  |`true`代表递归查找,`false`代表类似文件夹查找,以'/'分隔,不查子文件夹。  |
233|`doneCh`  | _chan struct{}_ | 在该channel上结束ListObjects iterator的一个message。 |
234
235
236__返回值__
237
238|参数   |类型   |描述   |
239|:---|:---| :---|
240|`objectInfo`  | _chan minio.ObjectInfo_ |存储桶中所有对象的read channel,对象的格式如下: |
241
242__minio.ObjectInfo__
243
244|属性   |类型   |描述   |
245|:---|:---| :---|
246|`objectInfo.Key`  | _string_ |对象的名称 |
247|`objectInfo.Size`  | _int64_ |对象的大小 |
248|`objectInfo.ETag`  | _string_ |对象的MD5校验码 |
249|`objectInfo.LastModified`  | _time.Time_ |对象的最后修改时间 |
250
251
252```go
253// Create a done channel to control 'ListObjects' go routine.
254doneCh := make(chan struct{})
255
256// Indicate to our routine to exit cleanly upon return.
257defer close(doneCh)
258
259isRecursive := true
260objectCh := minioClient.ListObjects("mybucket", "myprefix", isRecursive, doneCh)
261for object := range objectCh {
262    if object.Err != nil {
263        fmt.Println(object.Err)
264        return
265    }
266    fmt.Println(object)
267}
268```
269
270
271<a name="ListObjectsV2"></a>
272### ListObjectsV2(bucketName, prefix string, recursive bool, doneCh chan struct{}) <-chan ObjectInfo
273使用listing API v2版本列举存储桶中的对象。
274
275__参数__
276
277
278|参数   |类型   |描述   |
279|:---|:---| :---|
280|`bucketName`  | _string_  |存储桶名称 |
281| `objectPrefix` |_string_   | 要列举的对象前缀 |
282| `recursive`  | _bool_  |`true`代表递归查找,`false`代表类似文件夹查找,以'/'分隔,不查子文件夹。  |
283|`doneCh`  | _chan struct{}_ | 在该channel上结束ListObjects iterator的一个message。  |
284
285
286__返回值__
287
288|参数   |类型   |描述   |
289|:---|:---| :---|
290|`objectInfo`  | _chan minio.ObjectInfo_ |存储桶中所有对象的read channel |
291
292
293```go
294// Create a done channel to control 'ListObjectsV2' go routine.
295doneCh := make(chan struct{})
296
297// Indicate to our routine to exit cleanly upon return.
298defer close(doneCh)
299
300isRecursive := true
301objectCh := minioClient.ListObjectsV2("mybucket", "myprefix", isRecursive, doneCh)
302for object := range objectCh {
303    if object.Err != nil {
304        fmt.Println(object.Err)
305        return
306    }
307    fmt.Println(object)
308}
309```
310
311<a name="ListIncompleteUploads"></a>
312### ListIncompleteUploads(bucketName, prefix string, recursive bool, doneCh chan struct{}) <- chan ObjectMultipartInfo
313列举存储桶中未完整上传的对象。
314
315__参数__
316
317
318|参数   |类型   |描述   |
319|:---|:---| :---|
320|`bucketName`  | _string_  |存储桶名称 |
321| `prefix` |_string_   | 不完整上传的对象的前缀 |
322| `recursive`  | _bool_  |`true`代表递归查找,`false`代表类似文件夹查找,以'/'分隔,不查子文件夹。 |
323|`doneCh`  | _chan struct{}_ | 在该channel上结束ListIncompleteUploads iterator的一个message。   |
324
325
326__返回值__
327
328|参数   |类型   |描述   |
329|:---|:---| :---|
330|`multiPartInfo`  | _chan minio.ObjectMultipartInfo_  |multipart对象格式如下: |
331
332__minio.ObjectMultipartInfo__
333
334|属性   |类型   |描述   |
335|:---|:---| :---|
336|`multiPartObjInfo.Key`  | _string_  |未完整上传的对象的名称 |
337|`multiPartObjInfo.UploadID` | _string_ |未完整上传的对象的Upload ID |
338|`multiPartObjInfo.Size` | _int64_ |未完整上传的对象的大小 |
339
340__示例__
341
342
343```go
344// Create a done channel to control 'ListObjects' go routine.
345doneCh := make(chan struct{})
346
347// Indicate to our routine to exit cleanly upon return.
348defer close(doneCh)
349
350isRecursive := true // Recursively list everything at 'myprefix'
351multiPartObjectCh := minioClient.ListIncompleteUploads("mybucket", "myprefix", isRecursive, doneCh)
352for multiPartObject := range multiPartObjectCh {
353    if multiPartObject.Err != nil {
354        fmt.Println(multiPartObject.Err)
355        return
356    }
357    fmt.Println(multiPartObject)
358}
359```
360
361## 3. 操作对象
362
363<a name="GetObject"></a>
364### GetObject(bucketName, objectName string, opts GetObjectOptions) (*Object, error)
365返回对象数据的流,error是读流时经常抛的那些错。
366
367
368__参数__
369
370
371|参数   |类型   |描述   |
372|:---|:---| :---|
373|`bucketName`  | _string_  |存储桶名称  |
374|`objectName` | _string_  |对象的名称  |
375|`opts` | _minio.GetObjectOptions_ | GET请求的一些额外参数,像encryption,If-Match |
376
377
378__minio.GetObjectOptions__
379
380|参数 | 类型 | 描述 |
381|:---|:---|:---|
382| `opts.Materials` | _encrypt.Materials_ | `encrypt`包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go) |
383
384__返回值__
385
386
387|参数   |类型   |描述   |
388|:---|:---| :---|
389|`object`  | _*minio.Object_ |_minio.Object_代表了一个object reader。它实现了io.Reader, io.Seeker, io.ReaderAt and io.Closer接口。 |
390
391
392__示例__
393
394
395```go
396object, err := minioClient.GetObject("mybucket", "myobject", minio.GetObjectOptions{})
397if err != nil {
398    fmt.Println(err)
399    return
400}
401localFile, err := os.Create("/tmp/local-file.jpg")
402if err != nil {
403    fmt.Println(err)
404    return
405}
406if _, err = io.Copy(localFile, object); err != nil {
407    fmt.Println(err)
408    return
409}
410```
411
412<a name="FGetObject"></a>
413### FGetObject(bucketName, objectName, filePath string, opts GetObjectOptions) error
414下载并将文件保存到本地文件系统。
415
416__参数__
417
418
419|参数   |类型   |描述   |
420|:---|:---| :---|
421|`bucketName`  | _string_  |存储桶名称 |
422|`objectName` | _string_  |对象的名称  |
423|`filePath` | _string_  |下载后保存的路径 |
424|`opts` | _minio.GetObjectOptions_ | GET请求的一些额外参数,像encryption,If-Match |
425
426
427__示例__
428
429
430```go
431err = minioClient.FGetObject("mybucket", "myobject", "/tmp/myobject", minio.GetObjectOptions{})
432if err != nil {
433    fmt.Println(err)
434    return
435}
436```
437<a name="GetObjectWithContext"></a>
438### GetObjectWithContext(ctx context.Context, bucketName, objectName string, opts GetObjectOptions) (*Object, error)
439和GetObject操作是一样的,不过传入了取消请求的context。
440
441__参数__
442
443
444|参数   |类型   |描述   |
445|:---|:---| :---|
446|`ctx`  | _context.Context_  |请求上下文(Request context) |
447|`bucketName`  | _string_  |存储桶名称  |
448|`objectName` | _string_  |对象的名称  |
449|`opts` | _minio.GetObjectOptions_ |  GET请求的一些额外参数,像encryption,If-Match |
450
451
452__返回值__
453
454
455|参数   |类型   |描述   |
456|:---|:---| :---|
457|`object`  | _*minio.Object_ |_minio.Object_代表了一个object reader。它实现了io.Reader, io.Seeker, io.ReaderAt and io.Closer接口。 |
458
459
460__示例__
461
462
463```go
464ctx, cancel := context.WithTimeout(context.Background(), 100 * time.Second)
465defer cancel()
466
467object, err := minioClient.GetObjectWithContext(ctx, "mybucket", "myobject", minio.GetObjectOptions{})
468if err != nil {
469    fmt.Println(err)
470    return
471}
472
473localFile, err := os.Create("/tmp/local-file.jpg")
474if err != nil {
475    fmt.Println(err)
476    return
477}
478
479if _, err = io.Copy(localFile, object); err != nil {
480    fmt.Println(err)
481    return
482}
483```
484
485<a name="FGetObjectWithContext"></a>
486### FGetObjectWithContext(ctx context.Context, bucketName, objectName, filePath string, opts GetObjectOptions) error
487和FGetObject操作是一样的,不过允许取消请求。
488
489__参数__
490
491
492|参数   |类型   |描述   |
493|:---|:---| :---|
494|`ctx`  | _context.Context_  |请求上下文 |
495|`bucketName`  | _string_  |存储桶名称 |
496|`objectName` | _string_  |对象的名称  |
497|`filePath` | _string_  |下载后保存的路径 |
498|`opts` | _minio.GetObjectOptions_ | GET请求的一些额外参数,像encryption,If-Match  |
499
500
501__示例__
502
503
504```go
505ctx, cancel := context.WithTimeout(context.Background(), 100 * time.Second)
506defer cancel()
507
508err = minioClient.FGetObjectWithContext(ctx, "mybucket", "myobject", "/tmp/myobject", minio.GetObjectOptions{})
509if err != nil {
510    fmt.Println(err)
511    return
512}
513```
514
515<a name="FGetEncryptedObject"></a>
516### FGetEncryptedObject(bucketName, objectName, filePath string, materials encrypt.Materials) error
517和FGetObject操作是一样的,不过会对加密请求进行解密。
518
519__参数__
520
521
522|参数   |类型   |描述   |
523|:---|:---| :---|
524|`bucketName`  | _string_  |存储桶名称 |
525|`objectName` | _string_  |对象的名称  |
526|`filePath` | _string_  |下载后保存的路径|
527|`materials` | _encrypt.Materials_ | `encrypt`包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go) |
528
529
530__示例__
531
532
533```go
534// Generate a master symmetric key
535key := encrypt.NewSymmetricKey([]byte("my-secret-key-00"))
536
537// Build the CBC encryption material
538cbcMaterials, err := encrypt.NewCBCSecureMaterials(key)
539if err != nil {
540    fmt.Println(err)
541    return
542}
543
544err = minioClient.FGetEncryptedObject("mybucket", "myobject", "/tmp/myobject", cbcMaterials)
545if err != nil {
546    fmt.Println(err)
547    return
548}
549```
550
551<a name="PutObject"></a>
552### PutObject(bucketName, objectName string, reader io.Reader, objectSize int64,opts PutObjectOptions) (n int, err error)
553当对象小于64MiB时,直接在一次PUT请求里进行上传。当大于64MiB时,根据文件的实际大小,PutObject会自动地将对象进行拆分成64MiB一块或更大一些进行上传。对象的最大大小是5TB。
554
555__参数__
556
557
558|参数   |类型   |描述   |
559|:---|:---| :---|
560|`bucketName`  | _string_  |存储桶名称  |
561|`objectName` | _string_  |对象的名称   |
562|`reader` | _io.Reader_  |任意实现了io.Reader的GO类型 |
563|`objectSize`| _int64_ |上传的对象的大小,-1代表未知。 |
564|`opts` | _minio.PutObjectOptions_  |  允许用户设置可选的自定义元数据,内容标题,加密密钥和用于分段上传操作的线程数量。 |
565
566__minio.PutObjectOptions__
567
568|属性 | 类型 | 描述 |
569|:--- |:--- | :--- |
570| `opts.UserMetadata` | _map[string]string_ | 用户元数据的Map|
571| `opts.Progress` | _io.Reader_ | 获取上传进度的Reader |
572| `opts.ContentType` | _string_ | 对象的Content type, 例如"application/text" |
573| `opts.ContentEncoding` | _string_ | 对象的Content encoding,例如"gzip" |
574| `opts.ContentDisposition` | _string_ | 对象的Content disposition, "inline" |
575| `opts.CacheControl` | _string_ | 指定针对请求和响应的缓存机制,例如"max-age=600"|
576| `opts.EncryptMaterials` | _encrypt.Materials_ | `encrypt`包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go) |
577
578
579__示例__
580
581
582```go
583file, err := os.Open("my-testfile")
584if err != nil {
585    fmt.Println(err)
586    return
587}
588defer file.Close()
589
590fileStat, err := file.Stat()
591if err != nil {
592    fmt.Println(err)
593    return
594}
595
596n, err := minioClient.PutObject("mybucket", "myobject", file, fileStat.Size(), minio.PutObjectOptions{ContentType:"application/octet-stream"})
597if err != nil {
598    fmt.Println(err)
599    return
600}
601fmt.Println("Successfully uploaded bytes: ", n)
602```
603
604API方法在minio-go SDK版本v3.0.3中提供的PutObjectWithSize,PutObjectWithMetadata,PutObjectStreaming和PutObjectWithProgress被替换为接受指向PutObjectOptions struct的指针的新的PutObject调用变体。
605
606<a name="PutObjectWithContext"></a>
607### PutObjectWithContext(ctx context.Context, bucketName, objectName string, reader io.Reader, objectSize int64, opts PutObjectOptions) (n int, err error)
608和PutObject是一样的,不过允许取消请求。
609
610__参数__
611
612
613|参数   |类型   |描述   |
614|:---|:---| :---|
615|`ctx`  | _context.Context_  |请求上下文 |
616|`bucketName`  | _string_  |存储桶名称  |
617|`objectName` | _string_  |对象的名称   |
618|`reader` | _io.Reader_  |任何实现io.Reader的Go类型 |
619|`objectSize`| _int64_ | 上传的对象的大小,-1代表未知 |
620|`opts` | _minio.PutObjectOptions_  |允许用户设置可选的自定义元数据,content-type,content-encoding,content-disposition以及cache-control headers,传递加密模块以加密对象,并可选地设置multipart put操作的线程数量。|
621
622
623__示例__
624
625
626```go
627ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
628defer cancel()
629
630file, err := os.Open("my-testfile")
631if err != nil {
632    fmt.Println(err)
633    return
634}
635defer file.Close()
636
637fileStat, err := file.Stat()
638if err != nil {
639    fmt.Println(err)
640    return
641}
642
643n, err := minioClient.PutObjectWithContext(ctx, "my-bucketname", "my-objectname", file, fileStat.Size(), minio.PutObjectOptions{
644	ContentType: "application/octet-stream",
645})
646if err != nil {
647    fmt.Println(err)
648    return
649}
650fmt.Println("Successfully uploaded bytes: ", n)
651```
652
653<a name="CopyObject"></a>
654### CopyObject(dst DestinationInfo, src SourceInfo) error
655通过在服务端对已存在的对象进行拷贝,实现新建或者替换对象。它支持有条件的拷贝,拷贝对象的一部分,以及在服务端的加解密。请查看`SourceInfo`和`DestinationInfo`两个类型来了解更多细节。
656
657拷贝多个源文件到一个目标对象,请查看`ComposeObject` API。
658
659__参数__
660
661
662|参数   |类型   |描述   |
663|:---|:---| :---|
664|`dst`  | _minio.DestinationInfo_  |目标对象 |
665|`src` | _minio.SourceInfo_  |源对象 |
666
667
668__示例__
669
670
671```go
672// Use-case 1: Simple copy object with no conditions.
673// Source object
674src := minio.NewSourceInfo("my-sourcebucketname", "my-sourceobjectname", nil)
675
676// Destination object
677dst, err := minio.NewDestinationInfo("my-bucketname", "my-objectname", nil, nil)
678if err != nil {
679    fmt.Println(err)
680    return
681}
682
683// Copy object call
684err = minioClient.CopyObject(dst, src)
685if err != nil {
686    fmt.Println(err)
687    return
688}
689```
690
691```go
692// Use-case 2:
693// Copy object with copy-conditions, and copying only part of the source object.
694// 1. that matches a given ETag
695// 2. and modified after 1st April 2014
696// 3. but unmodified since 23rd April 2014
697// 4. copy only first 1MiB of object.
698
699// Source object
700src := minio.NewSourceInfo("my-sourcebucketname", "my-sourceobjectname", nil)
701
702// Set matching ETag condition, copy object which matches the following ETag.
703src.SetMatchETagCond("31624deb84149d2f8ef9c385918b653a")
704
705// Set modified condition, copy object modified since 2014 April 1.
706src.SetModifiedSinceCond(time.Date(2014, time.April, 1, 0, 0, 0, 0, time.UTC))
707
708// Set unmodified condition, copy object unmodified since 2014 April 23.
709src.SetUnmodifiedSinceCond(time.Date(2014, time.April, 23, 0, 0, 0, 0, time.UTC))
710
711// Set copy-range of only first 1MiB of file.
712src.SetRange(0, 1024*1024-1)
713
714// Destination object
715dst, err := minio.NewDestinationInfo("my-bucketname", "my-objectname", nil, nil)
716if err != nil {
717    fmt.Println(err)
718    return
719}
720
721// Copy object call
722err = minioClient.CopyObject(dst, src)
723if err != nil {
724    fmt.Println(err)
725    return
726}
727```
728
729<a name="ComposeObject"></a>
730### ComposeObject(dst minio.DestinationInfo, srcs []minio.SourceInfo) error
731通过使用服务端拷贝实现钭多个源对象合并创建成一个新的对象。
732
733__参数__
734
735
736|参数   |类型   |描述   |
737|:---|:---|:---|
738|`dst`  | _minio.DestinationInfo_  |要被创建的目标对象 |
739|`srcs` | _[]minio.SourceInfo_  |要合并的多个源对象 |
740
741
742__示例__
743
744
745```go
746// Prepare source decryption key (here we assume same key to
747// decrypt all source objects.)
748decKey := minio.NewSSEInfo([]byte{1, 2, 3}, "")
749
750// Source objects to concatenate. We also specify decryption
751// key for each
752src1 := minio.NewSourceInfo("bucket1", "object1", &decKey)
753src1.SetMatchETagCond("31624deb84149d2f8ef9c385918b653a")
754
755src2 := minio.NewSourceInfo("bucket2", "object2", &decKey)
756src2.SetMatchETagCond("f8ef9c385918b653a31624deb84149d2")
757
758src3 := minio.NewSourceInfo("bucket3", "object3", &decKey)
759src3.SetMatchETagCond("5918b653a31624deb84149d2f8ef9c38")
760
761// Create slice of sources.
762srcs := []minio.SourceInfo{src1, src2, src3}
763
764// Prepare destination encryption key
765encKey := minio.NewSSEInfo([]byte{8, 9, 0}, "")
766
767// Create destination info
768dst, err := minio.NewDestinationInfo("bucket", "object", &encKey, nil)
769if err != nil {
770    fmt.Println(err)
771    return
772}
773
774// Compose object call by concatenating multiple source files.
775err = minioClient.ComposeObject(dst, srcs)
776if err != nil {
777    fmt.Println(err)
778    return
779}
780
781fmt.Println("Composed object successfully.")
782```
783
784<a name="NewSourceInfo"></a>
785### NewSourceInfo(bucket, object string, decryptSSEC *SSEInfo) SourceInfo
786构建一个可用于服务端拷贝操作(像`CopyObject`和`ComposeObject`)的`SourceInfo`对象。该对象可用于给源对象设置拷贝条件。
787
788__参数__
789
790| 参数         | 类型             | 描述                                                      |
791| :---          | :---             | :---                                                             |
792| `bucket`      | _string_         | 源存储桶                                       |
793| `object`      | _string_         | 源对象                                     |
794| `decryptSSEC` | _*minio.SSEInfo_ | 源对象的解密信息 (`nil`代表不用解密) |
795
796__示例__
797
798```go
799// No decryption parameter.
800src := minio.NewSourceInfo("bucket", "object", nil)
801
802// Destination object
803dst, err := minio.NewDestinationInfo("my-bucketname", "my-objectname", nil, nil)
804if err != nil {
805    fmt.Println(err)
806    return
807}
808
809// Copy object call
810err = minioClient.CopyObject(dst, src)
811if err != nil {
812    fmt.Println(err)
813    return
814}
815```
816
817```go
818// With decryption parameter.
819decKey := minio.NewSSEInfo([]byte{1,2,3}, "")
820src := minio.NewSourceInfo("bucket", "object", &decKey)
821
822// Destination object
823dst, err := minio.NewDestinationInfo("my-bucketname", "my-objectname", nil, nil)
824if err != nil {
825    fmt.Println(err)
826    return
827}
828
829// Copy object call
830err = minioClient.CopyObject(dst, src)
831if err != nil {
832    fmt.Println(err)
833    return
834}
835```
836
837<a name="NewDestinationInfo"></a>
838### NewDestinationInfo(bucket, object string, encryptSSEC *SSEInfo, userMeta map[string]string) (DestinationInfo, error)
839构建一个用于服务端拷贝操作(像`CopyObject`和`ComposeObject`)的用作目标对象的`DestinationInfo`。
840
841__参数__
842
843| 参数         | 类型                | 描述                                                                                                    |
844| :---          | :---                | :---                                                                                                           |
845| `bucket`      | _string_            | 目标存储桶名称                                                                                 |
846| `object`      | _string_            | 目标对象名称                                                                                |
847| `encryptSSEC` | _*minio.SSEInfo_    | 源对象的加密信息 (`nil`代表不用加密)                                               |
848| `userMeta`    | _map[string]string_ | 给目标对象的用户元数据,如果是nil,并只有一个源对象,则将源对象的用户元数据拷贝给目标对象。|
849
850__示例__
851
852```go
853// No encryption parameter.
854src := minio.NewSourceInfo("bucket", "object", nil)
855dst, err := minio.NewDestinationInfo("bucket", "object", nil, nil)
856if err != nil {
857    fmt.Println(err)
858    return
859}
860
861// Copy object call
862err = minioClient.CopyObject(dst, src)
863if err != nil {
864    fmt.Println(err)
865    return
866}
867```
868
869```go
870src := minio.NewSourceInfo("bucket", "object", nil)
871
872// With encryption parameter.
873encKey := minio.NewSSEInfo([]byte{1,2,3}, "")
874dst, err := minio.NewDestinationInfo("bucket", "object", &encKey, nil)
875if err != nil {
876    fmt.Println(err)
877    return
878}
879
880// Copy object call
881err = minioClient.CopyObject(dst, src)
882if err != nil {
883    fmt.Println(err)
884    return
885}
886```
887
888<a name="FPutObject"></a>
889### FPutObject(bucketName, objectName, filePath, opts PutObjectOptions) (length int64, err error)
890将filePath对应的文件内容上传到一个对象中。
891
892当对象小于64MiB时,FPutObject直接在一次PUT请求里进行上传。当大于64MiB时,根据文件的实际大小,FPutObject会自动地将对象进行拆分成64MiB一块或更大一些进行上传。对象的最大大小是5TB。
893
894__参数__
895
896
897|参数   |类型   |描述   |
898|:---|:---| :---|
899|`bucketName`  | _string_  |存储桶名称  |
900|`objectName` | _string_  |对象的名称 |
901|`filePath` | _string_  |要上传的文件的路径 |
902|`opts` | _minio.PutObjectOptions_  |允许用户设置可选的自定义元数据,content-type,content-encoding,content-disposition以及cache-control headers,传递加密模块以加密对象,并可选地设置multipart put操作的线程数量。 |
903
904
905__示例__
906
907
908```go
909n, err := minioClient.FPutObject("my-bucketname", "my-objectname", "my-filename.csv", minio.PutObjectOptions{
910	ContentType: "application/csv",
911});
912if err != nil {
913    fmt.Println(err)
914    return
915}
916fmt.Println("Successfully uploaded bytes: ", n)
917```
918
919<a name="FPutObjectWithContext"></a>
920### FPutObjectWithContext(ctx context.Context, bucketName, objectName, filePath, opts PutObjectOptions) (length int64, err error)
921和FPutObject操作是一样的,不过允许取消请求。
922
923__参数__
924
925
926|参数   |类型   |描述   |
927|:---|:---| :---|
928|`ctx`  | _context.Context_  |请求上下文  |
929|`bucketName`  | _string_  |存储桶名称  |
930|`objectName` | _string_  |对象的名称 |
931|`filePath` | _string_  |要上传的文件的路径 |
932|`opts` | _minio.PutObjectOptions_  |允许用户设置可选的自定义元数据,content-type,content-encoding,content-disposition以及cache-control headers,传递加密模块以加密对象,并可选地设置multipart put操作的线程数量。 |
933
934__示例__
935
936
937```go
938ctx, cancel := context.WithTimeout(context.Background(), 100 * time.Second)
939defer cancel()
940
941n, err := minioClient.FPutObjectWithContext(ctx, "mybucket", "myobject.csv", "/tmp/otherobject.csv", minio.PutObjectOptions{ContentType:"application/csv"})
942if err != nil {
943    fmt.Println(err)
944    return
945}
946fmt.Println("Successfully uploaded bytes: ", n)
947```
948
949<a name="StatObject"></a>
950### StatObject(bucketName, objectName string, opts StatObjectOptions) (ObjectInfo, error)
951获取对象的元数据。
952
953__参数__
954
955
956|参数   |类型   |描述   |
957|:---|:---| :---|
958|`bucketName`  | _string_  |存储桶名称  |
959|`objectName` | _string_  |对象的名称   |
960|`opts` | _minio.StatObjectOptions_ | GET info/stat请求的一些额外参数,像encryption,If-Match |
961
962
963__返回值__
964
965|参数   |类型   |描述   |
966|:---|:---| :---|
967|`objInfo`  | _minio.ObjectInfo_  |对象stat信息 |
968
969
970__minio.ObjectInfo__
971
972|属性   |类型   |描述   |
973|:---|:---| :---|
974|`objInfo.LastModified`  | _time.Time_  |对象的最后修改时间 |
975|`objInfo.ETag` | _string_ |对象的MD5校验码|
976|`objInfo.ContentType` | _string_ |对象的Content type|
977|`objInfo.Size` | _int64_ |对象的大小|
978
979
980__示例__
981
982
983```go
984objInfo, err := minioClient.StatObject("mybucket", "myobject", minio.StatObjectOptions{})
985if err != nil {
986    fmt.Println(err)
987    return
988}
989fmt.Println(objInfo)
990```
991
992<a name="RemoveObject"></a>
993### RemoveObject(bucketName, objectName string) error
994删除一个对象。
995
996__参数__
997
998
999|参数   |类型   |描述   |
1000|:---|:---| :---|
1001|`bucketName`  | _string_  |存储桶名称  |
1002|`objectName` | _string_  |对象的名称 |
1003
1004
1005```go
1006err = minioClient.RemoveObject("mybucket", "myobject")
1007if err != nil {
1008    fmt.Println(err)
1009    return
1010}
1011```
1012
1013<a name="RemoveObjects"></a>
1014### RemoveObjects(bucketName string, objectsCh chan string) (errorCh <-chan RemoveObjectError)
1015
1016从一个input channel里删除一个对象集合。一次发送到服务端的删除请求最多可删除1000个对象。通过error channel返回的错误信息。
1017
1018__参数__
1019
1020|参数   |类型   |描述   |
1021|:---|:---| :---|
1022|`bucketName`  | _string_  |存储桶名称  |
1023|`objectsCh` | _chan string_  | 要删除的对象的channel   |
1024
1025
1026__返回值__
1027
1028|参数   |类型   |描述   |
1029|:---|:---| :---|
1030|`errorCh` | _<-chan minio.RemoveObjectError_  | 删除时观察到的错误的Receive-only channel。 |
1031
1032
1033```go
1034objectsCh := make(chan string)
1035
1036// Send object names that are needed to be removed to objectsCh
1037go func() {
1038	defer close(objectsCh)
1039	// List all objects from a bucket-name with a matching prefix.
1040	for object := range minioClient.ListObjects("my-bucketname", "my-prefixname", true, nil) {
1041		if object.Err != nil {
1042			log.Fatalln(object.Err)
1043		}
1044		objectsCh <- object.Key
1045	}
1046}()
1047
1048for rErr := range minioClient.RemoveObjects("mybucket", objectsCh) {
1049    fmt.Println("Error detected during deletion: ", rErr)
1050}
1051```
1052
1053<a name="RemoveIncompleteUpload"></a>
1054### RemoveIncompleteUpload(bucketName, objectName string) error
1055删除一个未完整上传的对象。
1056
1057__参数__
1058
1059
1060|参数   |类型   |描述   |
1061|:---|:---| :---|
1062|`bucketName`  | _string_  |存储桶名称   |
1063|`objectName` | _string_  |对象的名称   |
1064
1065__示例__
1066
1067
1068```go
1069err = minioClient.RemoveIncompleteUpload("mybucket", "myobject")
1070if err != nil {
1071    fmt.Println(err)
1072    return
1073}
1074```
1075
1076## 4. 操作加密对象
1077
1078<a name="NewSymmetricKey"></a>
1079### NewSymmetricKey(key []byte) *encrypt.SymmetricKey
1080
1081__参数__
1082
1083|参数   |类型   |描述   |
1084|:---|:---| :---|
1085|`key`  | _string_  |存储桶名称  |
1086
1087
1088__返回值__
1089
1090|参数   |类型   |描述   |
1091|:---|:---| :---|
1092|`symmetricKey`  | _*encrypt.SymmetricKey_ | 加密解密的对称秘钥 |
1093
1094```go
1095symKey := encrypt.NewSymmetricKey([]byte("my-secret-key-00"))
1096
1097// Build the CBC encryption material with symmetric key.
1098cbcMaterials, err := encrypt.NewCBCSecureMaterials(symKey)
1099if err != nil {
1100    fmt.Println(err)
1101    return
1102}
1103fmt.Println("Successfully initialized Symmetric key CBC materials", cbcMaterials)
1104
1105object, err := minioClient.GetEncryptedObject("mybucket", "myobject", cbcMaterials)
1106if err != nil {
1107    fmt.Println(err)
1108    return
1109}
1110defer object.Close()
1111```
1112
1113<a name="NewAsymmetricKey"></a>
1114### NewAsymmetricKey(privateKey []byte, publicKey[]byte) (*encrypt.AsymmetricKey, error)
1115
1116__参数__
1117
1118|参数   |类型   |描述   |
1119|:---|:---| :---|
1120|`privateKey` | _[]byte_ | Private key数据  |
1121|`publicKey`  | _[]byte_ | Public key数据  |
1122
1123
1124__返回值__
1125
1126|参数   |类型   |描述   |
1127|:---|:---| :---|
1128|`asymmetricKey`  | _*encrypt.AsymmetricKey_ | 加密解密的非对称秘钥 |
1129|`err`  | _error_ |  标准Error |
1130
1131
1132```go
1133privateKey, err := ioutil.ReadFile("private.key")
1134if err != nil {
1135    fmt.Println(err)
1136    return
1137}
1138
1139publicKey, err := ioutil.ReadFile("public.key")
1140if err != nil {
1141    fmt.Println(err)
1142    return
1143}
1144
1145// Initialize the asymmetric key
1146asymmetricKey, err := encrypt.NewAsymmetricKey(privateKey, publicKey)
1147if err != nil {
1148    fmt.Println(err)
1149    return
1150}
1151
1152// Build the CBC encryption material for asymmetric key.
1153cbcMaterials, err := encrypt.NewCBCSecureMaterials(asymmetricKey)
1154if err != nil {
1155    fmt.Println(err)
1156    return
1157}
1158fmt.Println("Successfully initialized Asymmetric key CBC materials", cbcMaterials)
1159
1160object, err := minioClient.GetEncryptedObject("mybucket", "myobject", cbcMaterials)
1161if err != nil {
1162    fmt.Println(err)
1163    return
1164}
1165defer object.Close()
1166```
1167
1168<a name="GetEncryptedObject"></a>
1169### GetEncryptedObject(bucketName, objectName string, encryptMaterials encrypt.Materials) (io.ReadCloser, error)
1170
1171返回对象的解密流。读流时的常见错误。
1172
1173__参数__
1174
1175|参数   |类型   |描述   |
1176|:---|:---| :---|
1177|`bucketName`  | _string_  | 存储桶名称  |
1178|`objectName` | _string_  | 对象的名称  |
1179|`encryptMaterials` | _encrypt.Materials_ | `encrypt`包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go) |
1180
1181
1182__返回值__
1183
1184|参数   |类型   |描述   |
1185|:---|:---| :---|
1186|`stream`  | _io.ReadCloser_ | 返回对象的reader,调用者需要在读取之后进行关闭。 |
1187|`err`  | _error | 错误信息 |
1188
1189
1190__示例__
1191
1192
1193```go
1194// Generate a master symmetric key
1195key := encrypt.NewSymmetricKey([]byte("my-secret-key-00"))
1196
1197// Build the CBC encryption material
1198cbcMaterials, err := encrypt.NewCBCSecureMaterials(key)
1199if err != nil {
1200    fmt.Println(err)
1201    return
1202}
1203
1204object, err := minioClient.GetEncryptedObject("mybucket", "myobject", cbcMaterials)
1205if err != nil {
1206    fmt.Println(err)
1207    return
1208}
1209defer object.Close()
1210
1211localFile, err := os.Create("/tmp/local-file.jpg")
1212if err != nil {
1213    fmt.Println(err)
1214    return
1215}
1216defer localFile.Close()
1217
1218if _, err = io.Copy(localFile, object); err != nil {
1219    fmt.Println(err)
1220    return
1221}
1222```
1223
1224<a name="PutEncryptedObject"></a>
1225
1226### PutEncryptedObject(bucketName, objectName string, reader io.Reader, encryptMaterials encrypt.Materials) (n int, err error)
1227加密并上传对象。
1228
1229__参数__
1230
1231|参数   |类型   |描述   |
1232|:---|:---| :---|
1233|`bucketName`  | _string_  |存储桶名称  |
1234|`objectName` | _string_  |对象的名称   |
1235|`reader` | _io.Reader_  |任何实现io.Reader的Go类型 |
1236|`encryptMaterials` | _encrypt.Materials_  | `encrypt`包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go) |
1237
1238__示例__
1239
1240```go
1241// Load a private key
1242privateKey, err := ioutil.ReadFile("private.key")
1243if err != nil {
1244    fmt.Println(err)
1245    return
1246}
1247
1248// Load a public key
1249publicKey, err := ioutil.ReadFile("public.key")
1250if err != nil {
1251    fmt.Println(err)
1252    return
1253}
1254
1255// Build an asymmetric key
1256key, err := encrypt.NewAsymmetricKey(privateKey, publicKey)
1257if err != nil {
1258    fmt.Println(err)
1259    return
1260}
1261
1262// Build the CBC encryption module
1263cbcMaterials, err := encrypt.NewCBCSecureMaterials(key)
1264if err != nil {
1265    fmt.Println(err)
1266    return
1267}
1268
1269// Open a file to upload
1270file, err := os.Open("my-testfile")
1271if err != nil {
1272    fmt.Println(err)
1273    return
1274}
1275defer file.Close()
1276
1277// Upload the encrypted form of the file
1278n, err := minioClient.PutEncryptedObject("mybucket", "myobject", file, cbcMaterials)
1279if err != nil {
1280    fmt.Println(err)
1281    return
1282}
1283fmt.Println("Successfully uploaded encrypted bytes: ", n)
1284```
1285
1286<a name="FPutEncryptedObject"></a>
1287### FPutEncryptedObject(bucketName, objectName, filePath, encryptMaterials encrypt.Materials) (n int, err error)
1288通过一个文件进行加密并上传到对象。
1289
1290__参数__
1291
1292
1293|参数   |类型   |描述   |
1294|:---|:---| :---|
1295|`bucketName`  | _string_  |存储桶名称  |
1296|`objectName` | _string_  |对象的名称 |
1297|`filePath` | _string_  |要上传的文件的路径 |
1298|`encryptMaterials` | _encrypt.Materials_  | `encrypt`包提供的对流加密的接口,(更多信息,请看https://godoc.org/github.com/minio/minio-go) |
1299
1300__示例__
1301
1302
1303```go
1304// Load a private key
1305privateKey, err := ioutil.ReadFile("private.key")
1306if err != nil {
1307    fmt.Println(err)
1308    return
1309}
1310
1311// Load a public key
1312publicKey, err := ioutil.ReadFile("public.key")
1313if err != nil {
1314    fmt.Println(err)
1315    return
1316}
1317
1318// Build an asymmetric key
1319key, err := encrypt.NewAsymmetricKey(privateKey, publicKey)
1320if err != nil {
1321    fmt.Println(err)
1322    return
1323}
1324
1325// Build the CBC encryption module
1326cbcMaterials, err := encrypt.NewCBCSecureMaterials(key)
1327if err != nil {
1328    fmt.Println(err)
1329    return
1330}
1331
1332n, err := minioClient.FPutEncryptedObject("mybucket", "myobject.csv", "/tmp/otherobject.csv", cbcMaterials)
1333if err != nil {
1334    fmt.Println(err)
1335    return
1336}
1337fmt.Println("Successfully uploaded encrypted bytes: ", n)
1338```
1339
1340<a name="NewSSEInfo"></a>
1341
1342### NewSSEInfo(key []byte, algo string) SSEInfo
1343创建一个通过用户提供的key(SSE-C),进行服务端加解密操作的key对象。
1344
1345__参数__
1346
1347| 参数  | 类型     | 描述                                                                                          |
1348| :---   | :---     | :---                                                                                                 |
1349| `key`  | _[]byte_ | 未编码的二进制key数组                                                        |
1350| `algo` | _string_ | 加密算法,可以为空(默认是`AES256`) |
1351
1352
1353## 5. Presigned操作
1354
1355<a name="PresignedGetObject"></a>
1356### PresignedGetObject(bucketName, objectName string, expiry time.Duration, reqParams url.Values) (*url.URL, error)
1357生成一个用于HTTP GET操作的presigned URL。浏览器/移动客户端可以在即使存储桶为私有的情况下也可以通过这个URL进行下载。这个presigned URL可以有一个过期时间,默认是7天。
1358
1359__参数__
1360
1361
1362|参数   |类型   |描述   |
1363|:---|:---| :---|
1364|`bucketName`  | _string_  |存储桶名称   |
1365|`objectName` | _string_  |对象的名称   |
1366|`expiry` | _time.Duration_  |presigned URL的过期时间,单位是秒   |
1367|`reqParams` | _url.Values_  |额外的响应头,支持_response-expires_, _response-content-type_, _response-cache-control_, _response-content-disposition_。  |
1368
1369
1370__示例__
1371
1372
1373```go
1374// Set request parameters for content-disposition.
1375reqParams := make(url.Values)
1376reqParams.Set("response-content-disposition", "attachment; filename=\"your-filename.txt\"")
1377
1378// Generates a presigned url which expires in a day.
1379presignedURL, err := minioClient.PresignedGetObject("mybucket", "myobject", time.Second * 24 * 60 * 60, reqParams)
1380if err != nil {
1381    fmt.Println(err)
1382    return
1383}
1384fmt.Println("Successfully generated presigned URL", presignedURL)
1385```
1386
1387<a name="PresignedPutObject"></a>
1388### PresignedPutObject(bucketName, objectName string, expiry time.Duration) (*url.URL, error)
1389生成一个用于HTTP GET操作的presigned URL。浏览器/移动客户端可以在即使存储桶为私有的情况下也可以通过这个URL进行下载。这个presigned URL可以有一个过期时间,默认是7天。
1390
1391注意:你可以通过只指定对象名称上传到S3。
1392
1393__参数__
1394
1395
1396|参数   |类型   |描述   |
1397|:---|:---| :---|
1398|`bucketName`  | _string_  |存储桶名称   |
1399|`objectName` | _string_  |对象的名称   |
1400|`expiry` | _time.Duration_  |presigned URL的过期时间,单位是秒 |
1401
1402
1403__示例__
1404
1405
1406```go
1407// Generates a url which expires in a day.
1408expiry := time.Second * 24 * 60 * 60 // 1 day.
1409presignedURL, err := minioClient.PresignedPutObject("mybucket", "myobject", expiry)
1410if err != nil {
1411    fmt.Println(err)
1412    return
1413}
1414fmt.Println("Successfully generated presigned URL", presignedURL)
1415```
1416
1417<a name="PresignedHeadObject"></a>
1418### PresignedHeadObject(bucketName, objectName string, expiry time.Duration, reqParams url.Values) (*url.URL, error)
1419生成一个用于HTTP GET操作的presigned URL。浏览器/移动客户端可以在即使存储桶为私有的情况下也可以通过这个URL进行下载。这个presigned URL可以有一个过期时间,默认是7天。
1420
1421__参数__
1422
1423|参数   |类型   |描述   |
1424|:---|:---| :---|
1425|`bucketName`  | _string_  |存储桶名称   |
1426|`objectName` | _string_  |对象的名称   |
1427|`expiry` | _time.Duration_  |presigned URL的过期时间,单位是秒   |
1428|`reqParams` | _url.Values_  |额外的响应头,支持_response-expires_, _response-content-type_, _response-cache-control_, _response-content-disposition_。  |
1429
1430
1431__示例__
1432
1433
1434```go
1435// Set request parameters for content-disposition.
1436reqParams := make(url.Values)
1437reqParams.Set("response-content-disposition", "attachment; filename=\"your-filename.txt\"")
1438
1439// Generates a presigned url which expires in a day.
1440presignedURL, err := minioClient.PresignedHeadObject("mybucket", "myobject", time.Second * 24 * 60 * 60, reqParams)
1441if err != nil {
1442    fmt.Println(err)
1443    return
1444}
1445fmt.Println("Successfully generated presigned URL", presignedURL)
1446```
1447
1448<a name="PresignedPostPolicy"></a>
1449### PresignedPostPolicy(PostPolicy) (*url.URL, map[string]string, error)
1450允许给POST操作的presigned URL设置策略条件。这些策略包括比如,接收对象上传的存储桶名称,名称前缀,过期策略。
1451
1452```go
1453// Initialize policy condition config.
1454policy := minio.NewPostPolicy()
1455
1456// Apply upload policy restrictions:
1457policy.SetBucket("mybucket")
1458policy.SetKey("myobject")
1459policy.SetExpires(time.Now().UTC().AddDate(0, 0, 10)) // expires in 10 days
1460
1461// Only allow 'png' images.
1462policy.SetContentType("image/png")
1463
1464// Only allow content size in range 1KB to 1MB.
1465policy.SetContentLengthRange(1024, 1024*1024)
1466
1467// Add a user metadata using the key "custom" and value "user"
1468policy.SetUserMetadata("custom", "user")
1469
1470// Get the POST form key/value object:
1471url, formData, err := minioClient.PresignedPostPolicy(policy)
1472if err != nil {
1473    fmt.Println(err)
1474    return
1475}
1476
1477// POST your content from the command line using `curl`
1478fmt.Printf("curl ")
1479for k, v := range formData {
1480    fmt.Printf("-F %s=%s ", k, v)
1481}
1482fmt.Printf("-F file=@/etc/bash.bashrc ")
1483fmt.Printf("%s\n", url)
1484```
1485
1486## 6. 存储桶策略/通知
1487
1488<a name="SetBucketPolicy"></a>
1489### SetBucketPolicy(bucketname, objectPrefix string, policy policy.BucketPolicy) error
1490给存储桶或者对象前缀设置访问权限。
1491
1492必须引入`github.com/minio/minio-go/pkg/policy`包。
1493
1494__参数__
1495
1496
1497|参数   |类型   |描述   |
1498|:---|:---| :---|
1499|`bucketName` | _string_  |存储桶名称|
1500|`objectPrefix` | _string_  |对象的名称前缀|
1501|`policy` | _policy.BucketPolicy_  |Policy的取值如下: |
1502| |  | _policy.BucketPolicyNone_ |
1503| |  | _policy.BucketPolicyReadOnly_ |
1504| |  | _policy.BucketPolicyReadWrite_ |
1505| |  | _policy.BucketPolicyWriteOnly_ |
1506
1507
1508__返回值__
1509
1510
1511|参数   |类型   |描述   |
1512|:---|:---| :---|
1513|`err` | _error_  |标准Error   |
1514
1515
1516__示例__
1517
1518
1519```go
1520// Sets 'mybucket' with a sub-directory 'myprefix' to be anonymously accessible for
1521// both read and write operations.
1522err = minioClient.SetBucketPolicy("mybucket", "myprefix", policy.BucketPolicyReadWrite)
1523if err != nil {
1524    fmt.Println(err)
1525    return
1526}
1527```
1528
1529<a name="GetBucketPolicy"></a>
1530### GetBucketPolicy(bucketName, objectPrefix string) (policy.BucketPolicy, error)
1531获取存储桶或者对象前缀的访问权限。
1532
1533必须引入`github.com/minio/minio-go/pkg/policy`包。
1534
1535__参数__
1536
1537
1538|参数   |类型   |描述   |
1539|:---|:---| :---|
1540|`bucketName`  | _string_  |存储桶名称   |
1541|`objectPrefix` | _string_  |该存储桶下的对象前缀 |
1542
1543__返回值__
1544
1545
1546|参数   |类型   |描述   |
1547|:---|:---| :---|
1548|`bucketPolicy`  | _policy.BucketPolicy_ |取值如下: `none`, `readonly`, `readwrite`,或者`writeonly`   |
1549|`err` | _error_  |标准Error  |
1550
1551__示例__
1552
1553
1554```go
1555bucketPolicy, err := minioClient.GetBucketPolicy("mybucket", "")
1556if err != nil {
1557    fmt.Println(err)
1558    return
1559}
1560fmt.Println("Access permissions for mybucket is", bucketPolicy)
1561```
1562
1563<a name="GetBucketNotification"></a>
1564### GetBucketNotification(bucketName string) (BucketNotification, error)
1565获取存储桶的通知配置
1566
1567__参数__
1568
1569
1570|参数   |类型   |描述   |
1571|:---|:---| :---|
1572|`bucketName`  | _string_  |存储桶名称 |
1573
1574__返回值__
1575
1576
1577|参数   |类型   |描述   |
1578|:---|:---| :---|
1579|`bucketNotification`  | _minio.BucketNotification_ |含有所有通知配置的数据结构|
1580|`err` | _error_  |标准Error  |
1581
1582__示例__
1583
1584
1585```go
1586bucketNotification, err := minioClient.GetBucketNotification("mybucket")
1587if err != nil {
1588    fmt.Println("Failed to get bucket notification configurations for mybucket", err)
1589    return
1590}
1591
1592for _, queueConfig := range bucketNotification.QueueConfigs {
1593    for _, e := range queueConfig.Events {
1594        fmt.Println(e + " event is enabled")
1595    }
1596}
1597```
1598
1599<a name="SetBucketNotification"></a>
1600### SetBucketNotification(bucketName string, bucketNotification BucketNotification) error
1601给存储桶设置新的通知
1602
1603__参数__
1604
1605
1606|参数   |类型   |描述   |
1607|:---|:---| :---|
1608|`bucketName`  | _string_  |存储桶名称   |
1609|`bucketNotification`  | _minio.BucketNotification_  |发送给配置的web service的XML  |
1610
1611__返回值__
1612
1613
1614|参数   |类型   |描述   |
1615|:---|:---| :---|
1616|`err` | _error_  |标准Error  |
1617
1618__示例__
1619
1620
1621```go
1622queueArn := minio.NewArn("aws", "sqs", "us-east-1", "804605494417", "PhotoUpdate")
1623
1624queueConfig := minio.NewNotificationConfig(queueArn)
1625queueConfig.AddEvents(minio.ObjectCreatedAll, minio.ObjectRemovedAll)
1626queueConfig.AddFilterPrefix("photos/")
1627queueConfig.AddFilterSuffix(".jpg")
1628
1629bucketNotification := minio.BucketNotification{}
1630bucketNotification.AddQueue(queueConfig)
1631
1632err = minioClient.SetBucketNotification("mybucket", bucketNotification)
1633if err != nil {
1634    fmt.Println("Unable to set the bucket notification: ", err)
1635    return
1636}
1637```
1638
1639<a name="RemoveAllBucketNotification"></a>
1640### RemoveAllBucketNotification(bucketName string) error
1641删除存储桶上所有配置的通知
1642
1643__参数__
1644
1645
1646|参数   |类型   |描述   |
1647|:---|:---| :---|
1648|`bucketName`  | _string_  |存储桶名称   |
1649
1650__返回值__
1651
1652
1653|参数   |类型   |描述   |
1654|:---|:---| :---|
1655|`err` | _error_  |标准Error  |
1656
1657__示例__
1658
1659
1660```go
1661err = minioClient.RemoveAllBucketNotification("mybucket")
1662if err != nil {
1663    fmt.Println("Unable to remove bucket notifications.", err)
1664    return
1665}
1666```
1667
1668<a name="ListenBucketNotification"></a>
1669### ListenBucketNotification(bucketName, prefix, suffix string, events []string, doneCh <-chan struct{}) <-chan NotificationInfo
1670ListenBucketNotification API通过notification channel接收存储桶通知事件。返回的notification channel有两个属性,'Records'和'Err'。
1671
1672- 'Records'持有从服务器返回的通知信息。
1673- 'Err'表示的是处理接收到的通知时报的任何错误。
1674
1675注意:一旦报错,notification channel就会关闭。
1676
1677__参数__
1678
1679
1680|参数   |类型   |描述   |
1681|:---|:---| :---|
1682|`bucketName`  | _string_  | 被监听通知的存储桶   |
1683|`prefix`  | _string_ | 过滤通知的对象前缀  |
1684|`suffix`  | _string_ | 过滤通知的对象后缀  |
1685|`events`  | _[]string_ | 开启指定事件类型的通知 |
1686|`doneCh`  | _chan struct{}_ | 在该channel上结束ListenBucketNotification iterator的一个message。  |
1687
1688__返回值__
1689
1690|参数   |类型   |描述   |
1691|:---|:---| :---|
1692|`notificationInfo` | _chan minio.NotificationInfo_ | 存储桶通知的channel |
1693
1694__minio.NotificationInfo__
1695
1696|属性   |类型   |描述   |
1697|`notificationInfo.Records` | _[]minio.NotificationEvent_ | 通知事件的集合 |
1698|`notificationInfo.Err` | _error_ | 操作时报的任何错误(标准Error) |
1699
1700
1701__示例__
1702
1703
1704```go
1705// Create a done channel to control 'ListenBucketNotification' go routine.
1706doneCh := make(chan struct{})
1707
1708// Indicate a background go-routine to exit cleanly upon return.
1709defer close(doneCh)
1710
1711// Listen for bucket notifications on "mybucket" filtered by prefix, suffix and events.
1712for notificationInfo := range minioClient.ListenBucketNotification("mybucket", "myprefix/", ".mysuffix", []string{
1713    "s3:ObjectCreated:*",
1714    "s3:ObjectAccessed:*",
1715    "s3:ObjectRemoved:*",
1716    }, doneCh) {
1717    if notificationInfo.Err != nil {
1718        fmt.Println(notificationInfo.Err)
1719    }
1720    fmt.Println(notificationInfo)
1721}
1722```
1723
1724## 7. 客户端自定义设置
1725
1726<a name="SetAppInfo"></a>
1727### SetAppInfo(appName, appVersion string)
1728给User-Agent添加的自定义应用信息。
1729
1730__参数__
1731
1732| 参数  | 类型  | 描述  |
1733|---|---|---|
1734|`appName`  | _string_  | 发请求的应用名称 |
1735| `appVersion`| _string_ | 发请求的应用版本 |
1736
1737
1738__示例__
1739
1740
1741```go
1742// Set Application name and version to be used in subsequent API requests.
1743minioClient.SetAppInfo("myCloudApp", "1.0.0")
1744```
1745
1746<a name="SetCustomTransport"></a>
1747### SetCustomTransport(customHTTPTransport http.RoundTripper)
1748重写默认的HTTP transport,通常用于调试或者添加自定义的TLS证书。
1749
1750__参数__
1751
1752| 参数  | 类型  | 描述  |
1753|---|---|---|
1754|`customHTTPTransport`  | _http.RoundTripper_  | 自定义的transport,例如:为了调试对API请求响应进行追踪。|
1755
1756
1757<a name="TraceOn"></a>
1758### TraceOn(outputStream io.Writer)
1759开启HTTP tracing。追踪信息输出到io.Writer,如果outputstream为nil,则trace写入到os.Stdout标准输出。
1760
1761__参数__
1762
1763| 参数  | 类型  | 描述  |
1764|---|---|---|
1765|`outputStream`  | _io.Writer_  | HTTP trace写入到outputStream |
1766
1767
1768<a name="TraceOff"></a>
1769### TraceOff()
1770关闭HTTP tracing。
1771
1772<a name="SetS3TransferAccelerate"></a>
1773### SetS3TransferAccelerate(acceleratedEndpoint string)
1774给后续所有API请求设置ASW S3传输加速endpoint。
1775注意:此API仅对AWS S3有效,对其它S3兼容的对象存储服务不生效。
1776
1777__参数__
1778
1779| 参数  | 类型  | 描述  |
1780|---|---|---|
1781|`acceleratedEndpoint`  | _string_  | 设置新的S3传输加速endpoint。|
1782
1783
1784## 8. 了解更多
1785
1786- [用Go语言创建属于你的音乐播放器APP示例](https://docs.minio.io/docs/go-music-player-app)
1787