{"id":989,"date":"2024-08-22T19:58:28","date_gmt":"2024-08-22T11:58:28","guid":{"rendered":"https:\/\/www.madbull.site\/?p=989"},"modified":"2024-09-24T12:59:00","modified_gmt":"2024-09-24T04:59:00","slug":"python%e8%bf%9e%e6%8e%a5minio%ef%bc%8c%e5%ae%9e%e7%8e%b0%e5%88%9b%e5%bb%ba%e6%a1%b6%e3%80%81%e4%b8%8a%e4%bc%a0%e5%92%8c%e4%b8%8b%e8%bd%bd%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=989","title":{"rendered":"python\u8fde\u63a5minio\uff0c\u5b9e\u73b0\u521b\u5efa\u6876\u3001\u4e0a\u4f20\u548c\u4e0b\u8f7d\u529f\u80fd"},"content":{"rendered":"\n<p>minio\u5173\u4e8epython\u7684sdk\uff0c\u53c2\u770b\uff1a<a href=\"https:\/\/min.io\/docs\/minio\/linux\/developers\/python\/API.html\">https:\/\/min.io\/docs\/minio\/linux\/developers\/python\/API.html<\/a><\/p>\n\n\n\n<p>minio\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=792\">https:\/\/www.madbull.site\/?p=792<\/a><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"has-large-font-size\"><strong>\u6587\u4ef6\u540d minio_client.py<\/strong><\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff0c\u9700\u8981\u66ff\u6362 endpoint\u3001accessKeyId\u3001secretAccessKey\u3001usessl \u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n# minio_client.py\n\nfrom minio import Minio\nimport io\nimport os\n\n\/\/ bucket\u524d\u7f00\uff0c\u6839\u636e\u65e5\u671f\u81ea\u52a8\u751f\u6210bucket\u540d\u5b57\nbucket_name_pre = \"test-\"\n\nendpoint = \"HOST:PORT\"\naccessKeyId = \"accesskey\"\nsecretAccessKey = \"secretkey\"\nusessl=False\n\n\/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\nminio_client=Minio(endpoint, access_key=accessKeyId, secret_key=secretAccessKey, secure=usessl )\n\n\/\/ \u5982\u679c\u6ca1\u6709bucket\u5219\u81ea\u52a8\u751f\u6210bucket\ndef make_bucket_if_not_exists(bucket_name) :\n    if minio_client.bucket_exists(bucket_name) :\n        print(\"found bucket success : \"+bucket_name)\n        return True\n\n    minio_client.make_bucket(bucket_name)\n    print(\"make bucket : \"+bucket_name)\n    return True\n\n\/\/ \u4fdd\u5b58\u6587\u4ef6\u5230minio\ndef save_to_minio( filename: str, date: str, filecontent: bytes ) :\n    bucket_name = bucket_name_pre + date&#91;:6]\n    if make_bucket_if_not_exists(bucket_name) :\n        rslt = minio_client.put_object(bucket_name, filename, data=io.BytesIO(filecontent), length=len(filecontent))\n        print(rslt.object_name + \" save success!\")\n        return True\n    else :\n        return False\n\n\/\/ \u4e0b\u8f7d\u6587\u4ef6\u5230\u6307\u5b9a\u76ee\u5f55\ndef download_from_minio( filename: str, date: str, downloadpath: str ) :\n    bucket_name = bucket_name_pre + date&#91;:6]\n    fullpath = os.path.join(downloadpath, filename)\n    rslt = minio_client.fget_object(bucket_name, filename, file_path=fullpath)\n    print(rslt.object_name + \" download success!\")\n    return True\n\n\/\/ \u6d4b\u8bd5\nif __name__ == \"__main__\" :\n    \n    save_to_minio(\"tt.txt\", \"20240822191919\", b\"aaaaaaaabbbbbbbbbbbccccccccdddddddd\\n\")\n    download_from_minio(\"tt.txt\", \"20240822191919\", \"\/tmp\")<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"240\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-1.png\" alt=\"\" class=\"wp-image-990\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-1.png 606w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/08\/22-1-300x119.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--40);margin-bottom:var(--wp--preset--spacing--40);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5bf9\u4e8e\u52a0\u5bc6\u901a\u8baf\u7684minio\u670d\u52a1\u5904\u7406\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>1\u3001\u5ffd\u7565\u8bc1\u4e66<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\nminio_client=Minio(endpoint, access_key=accessKeyId, secret_key=secretAccessKey, secure=True, cert_check=False )<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>2\u3001\u914d\u7f6e\u4fe1\u4efb\u8bc1\u4e66<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>ssl_context = ssl.create_default_context(cafile=\"\/xxx\/xxx\/myCA.crt\")\nhttp_context = urllib3.PoolManager(ssl_context=ssl_context)\n\/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\nminio_client=Minio(endpoint, access_key=accessKeyId, secret_key=secretAccessKey, secure=True, http_client=http_context, cert_check=True )<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>python\u8fde\u63a5minio\uff0c\u5b9e\u73b0\u521b\u5efa\u6876\u3001\u4e0a\u4f20\u548c\u4e0b\u8f7d\u529f\u80fd\u3002\u6587\u4ef6\u540d minio_client.py\uff0c\u4ee3\u7801\u5982\u4e0b\uff0c\u9700\u8981\u66ff\u6362 endpoint\u3001accessKeyId\u3001secretAccessKey\u3001usessl \u7684\u503c\u3002<\/p>\n","protected":false},"author":1,"featured_media":993,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[155,154],"tags":[404,324,142,405],"class_list":["post-989","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-154","tag-download","tag-minio","tag-python","tag-save"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=989"}],"version-history":[{"count":6,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/989\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/989\/revisions\/1339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/993"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=989"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}