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