Debian Codegraph @

docs/zh_CN/API.md

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