{"id":915,"date":"2018-01-19T12:28:00","date_gmt":"2018-01-19T04:28:00","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=915"},"modified":"2023-08-07T20:57:26","modified_gmt":"2023-08-07T12:57:26","slug":"article111","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2018\/01\/19\/article111\/","title":{"rendered":"thinkphp5\u8c03\u7528\u4e03\u725b\u4e91SDK\u4e0a\u4f20\u6587\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4"},"content":{"rendered":"<h1>thinkphp5\u8c03\u7528\u4e03\u725b\u4e91SDK\u4e0a\u4f20\u6587\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4<\/h1>\n<h3>\u4eca\u5929\u9700\u8981\u5c06\u56fe\u7247\u4e0a\u4f20\u5230\u4e03\u725b\u4e91\u4e0a\uff0c\u5728\u7f51\u4e0a\u641c\u4e86\u4e9b\u6559\u7a0b\uff0c\u90fd\u4e0d\u600e\u4e48\u5168\uff0c\u6709\u7684\u8fd8\u7f3a\u4e1c\u897f\uff0c\u6240\u4ee5\u6211\u628a\u81ea\u5df1\u5f04\u6210\u529f\u7684\u6b65\u9aa4\u5199\u51fa\u6765\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u5176\u4ed6\u4eba\uff0c\u8fd9\u4e2a\u6587\u7ae0\u53ea\u9002\u5408thinkphp5\uff0c\u5176\u4ed6\u7248\u672c\u7684\u8bf7\u53c2\u8003\u5176\u4ed6\u6587\u6863\uff0c\u6587\u6863\u4e2d\u9700\u8981\u7684\u4e03\u725b\u4e91PHP-SDK\uff0c\u8bf7\u5728\u4e03\u725b\u4e91\u5b98\u65b9\u4e0b\u8f7d\uff0c\u6216\u8005\u70b9\u6211\u7684\u6587\u4ef6\u5e8a\u4e0b\u8f7d\u4e5f\u53ef\u4ee5\uff0c\u5730\u5740\u662fhttp:\/\/filebed.rain1024.com\/<\/h3>\n<h3>1.\u5148\u5728verdor\u91cc\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939\uff1a Qiniu<\/h3>\n<h3>2.\u5c06SDK\u538b\u7f29\u5305\u4e2d\u7684\u6587\u4ef6\u5168\u90e8\u89e3\u538b\u5230Qiniu\u6587\u4ef6\u5939\u4e2d<\/h3>\n<h3>3.\u5728Controller\u4e2d\uff0c\u5f15\u7528\u7c7b<\/h3>\n<pre><code>vendor('Qiniu.autoload');\nuse Qiniu\\Auth as Auth;\nuse Qiniu\\Storage\\BucketManager;\nuse Qiniu\\Storage\\UploadManager;\n<\/code><\/pre>\n<h3>4.\u5728config.php\u4e2d\u6dfb\u52a0\u914d\u7f6e<\/h3>\n<pre><code>'ACCESSKEY' =&gt; '',\/\/\u4f60\u7684accessKey\n'SECRETKEY' =&gt; '',\/\/\u4f60\u7684secretKey\n'BUCKET' =&gt; '',\/\/\u4e0a\u4f20\u7684\u7a7a\u95f4\n'DOMAIN'=&gt;'',\/\/\u7a7a\u95f4\u7ed1\u5b9a\u7684\u57df\u540d\n<\/code><\/pre>\n<h3>5.\u4e0a\u4f20\u64cd\u4f5c\u7684\u51fd\u6570<\/h3>\n<pre><code>$file = request()-&gt;file('image');\n\/\/ \u8981\u4e0a\u4f20\u56fe\u7247\u7684\u672c\u5730\u8def\u5f84\n$filePath = $file-&gt;getRealPath();\n$ext = pathinfo($file-&gt;getInfo('name'), PATHINFO_EXTENSION);  \/\/\u540e\u7f00\n\/\/\u83b7\u53d6\u5f53\u524d\u63a7\u5236\u5668\u540d\u79f0\n$controllerName = 'index';\n\/\/ \u4e0a\u4f20\u5230\u4e03\u725b\u540e\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\n$key =substr(md5($file-&gt;getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;\n\/\/ \u9700\u8981\u586b\u5199\u4f60\u7684 Access Key \u548c Secret Key\n$accessKey = config('ACCESSKEY');\n$secretKey = config('SECRETKEY');\n\/\/ \u6784\u5efa\u9274\u6743\u5bf9\u8c61\n$auth = new Auth($accessKey, $secretKey);\n\/\/ \u8981\u4e0a\u4f20\u7684\u7a7a\u95f4\n$bucket = config('BUCKET');\n$domain = config('DOMAINImage');\n$token = $auth-&gt;uploadToken($bucket);\n\/\/ \u521d\u59cb\u5316 UploadManager \u5bf9\u8c61\u5e76\u8fdb\u884c\u6587\u4ef6\u7684\u4e0a\u4f20\n$uploadMgr = new UploadManager();\n\/\/ \u8c03\u7528 UploadManager \u7684 putFile \u65b9\u6cd5\u8fdb\u884c\u6587\u4ef6\u7684\u4e0a\u4f20\nlist($ret, $err) = $uploadMgr-&gt;putFile($token, $key, $filePath);\nif ($err !== null) {\n    echo [\"err\"=&gt;1,\"msg\"=&gt;$err,\"data\"=&gt;\"\"];\n} else {\n    \/\/\u8fd4\u56de\u56fe\u7247\u7684\u5b8c\u6574URL\n    var_dump($ret);\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>thinkphp5\u8c03\u7528\u4e03\u725b\u4e91SDK\u4e0a\u4f20\u6587\u4ef6\u7684\u8be6\u7ec6\u6b65\u9aa4 \u4eca\u5929\u9700\u8981\u5c06\u56fe\u7247\u4e0a\u4f20\u5230\u4e03\u725b\u4e91\u4e0a\uff0c\u5728\u7f51\u4e0a\u641c\u4e86\u4e9b\u6559\u7a0b\uff0c\u90fd\u4e0d\u600e\u4e48\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2018\/01\/19\/article111\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[57],"class_list":["post-915","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=915"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/915\/revisions"}],"predecessor-version":[{"id":1422,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/915\/revisions\/1422"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=915"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}