ÔõÑùʹÓÃThinkPHP6ʵÏÖOSSÎļþÉÏ´«ºÍÏÂÔزÙ×÷£¿
Ëæ×Å»¥ÁªÍøÊÖÒÕµÄÒ»Ö±Éú³¤£¬Ôƴ洢ЧÀÍÔ½À´Ô½Êܵ½ÈËÃǵÄÖØÊÓ¡£°¢ÀïÔƹ¤¾ß´æ´¢£¨oss£©ÊÇÒ»¸öÇå¾²¡¢Îȹ̡¢¸ßÀ©Õ¹ÐÔµÄÔƶ˴洢ЧÀÍ£¬ÓÃÓÚ´æ´¢º£Á¿Êý¾Ý¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp6¿ò¼ÜʵÏÖ°¢ÀïÔÆossÎļþÉÏ´«ºÍÏÂÔزÙ×÷¡£
Ò»¡¢½¨Éè°¢ÀïÔÆOSS Bucket
Ê×ÏÈ£¬ÐèÒªÔÚ°¢ÀïÔƹÙÍøÉϽ¨ÉèÒ»¸öÓÃÓÚ´æ´¢ÎļþµÄBucket¡£BucketÏ൱ÓÚÔƶ˵ÄÎļþ¼Ð£¬ÓÃÓÚ´æ´¢ÉÏ´«µ½°¢ÀïÔÆOSSµÄÎļþ¡£BucketµÄ½¨Éè·½·¨²»ÔÚ±¾ÎĵÄÏÈÈݹæÄ£Ö®ÄÚ£¬¶ÁÕß¿ÉÔÚ°¢ÀïÔƹÙÍøƽ̨²éÔÄÏà¹Ø½Ì³Ì¡£
½¨ÉèÍê±Ïºó£¬ÐèÒª»ñÈ¡ÒÔÏÂÈý¸ö²ÎÊý£º
1¡¢AccessKeyId£º»á¼ûOSSµÄÓû§ID¡£
2¡¢AccessKeySecret£º»á¼ûOSSµÄÓû§ÃÜÔ¿¡£
3¡¢Endpoint£ºOSSЧÀ͵ĵص㡣
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÕâЩ²ÎÊý½«ÔÚºóÐø´úÂëʵÏÖÖÐʹÓá£
¶þ¡¢×°Öð¢ÀïÔÆOSS SDK
ÔÚʹÓð¢ÀïÔÆOSS SDK֮ǰ£¬ÐèҪװÖÃËü¡£¿ÉÒÔʹÓÃcomposer¾ÙÐÐ×°Öã¬ÏÂÁîÈçÏ£º
composer require aliyuncs/oss-sdk-php
×°ÖÃÍê±Ïºó£¬ÐèÒªÔÚconfigĿ¼Ï½¨Éèoss.phpÉèÖÃÎļþ¡£ÉèÖÃÎļþÖÐÐèÒª°üÀ¨ÒÔÏÂÈý¸ö²ÎÊý£º
‘accessKeyId’ => ‘°¢ÀïÔÆAccessKeyId’,
‘accessSecret’ => ‘°¢ÀïÔÆAccessKeySecret’,
‘endpoint’ => ‘OSSЧÀ͵صã’,
ΪÁËÀû±ãÉèÖòÎÊýµÄ»ñÈ¡£¬Ò²¿ÉÒÔÔÚ.envÎļþÖнçËÃ÷ÈÕâЩ²ÎÊý£¬ÔÚconfigĿ¼Ï½¨ÉèÒ»¸öoss.phpÉèÖÃÎļþ£¬ÈçÏÂËùʾ£º
‘accessKeyId’ => env(‘OSS_ACCESS_KEY_ID’),
‘accessSecret’ => env(‘OSS_ACCESS_KEY_SECRET’),
‘endpoint’ => env(‘OSS_ENDPOINT’),
Èý¡¢ÎļþÉÏ´«²Ù×÷
ÔÚÍê³ÉÁË°¢ÀïÔÆOSS SDKµÄ×°ÖúͲÎÊýµÄÉèÖú󣬾ͿÉÒÔ×îÏȾÙÐÐÎļþÉÏ´«²Ù×÷ÁË¡£ÔÚcontrollerÖн¨ÉèuploadÒªÁ죬´úÂëÈçÏ£º
use OSSOssClient;
use OSSCoreOssException;
public function upload()
{ $accessKeyId = config('oss.accessKeyId'); $accessKeySecret = config('oss.accessSecret'); $endpoint = config('oss.endpoint'); $bucket = 'your_bucket_name'; // ½¨ÉèOSSClientʵÀý try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); } catch (OssException $e) { printf(__FUNCTION__ . "°¢ÀïÔÆOSSÅþÁ¬Ê§°Ü£ºerror[%s]
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
“, $e->getMessage());
return; } // »ñÈ¡Îļþ $file = request()->file('file'); if (!$file) { return "ÉÏ´«Îļþ²»¿ÉΪ¿Õ"; } // ÉÏ´«Îļþ $fileName = $file->getOriginalName(); $filePath = $file->getRealPath(); try { $result = $ossClient->uploadFile($bucket, $fileName, $filePath); } catch (OssException $e) { return "ÎļþÉÏ´«Ê§°Ü"; } if (isset($result['oss-request-url'])) { return "ÎļþÉÏ´«ÀÖ³É"; } else { return "ÎļþÉÏ´«Ê§°Ü"; } }
µÇ¼ºó¸´ÖÆ
ÔÚÒªÁìÖÐÊ×ÏÈ»ñȡ֮ǰ½¨ÉèBucketʱµÄÈý¸ö²ÎÊý£¬È»ºó½¨ÉèOSSClientʵÀý¡£½Ó×Åͨ¹ýrequest()->file(‘file’)»ñÈ¡ÉÏ´«µÄÎļþ£¬Ê¹ÓÃgetOriginalName()ÒªÁì»ñÈ¡ÉÏ´«ÎļþµÄÔÃû³Æ£¬Ê¹ÓÃgetRealPath()ÒªÁì»ñÈ¡ÉÏ´«ÎļþµÄÔÝʱÎļþ·¾¶¡£×îºó£¬Ê¹ÓÃuploadFile()ÒªÁ콫ÎļþÉÏ´«µ½°¢ÀïÔÆOSS¡£
ËÄ¡¢ÎļþÏÂÔزÙ×÷
ÓëÎļþÉÏ´«²Ù×÷ÀàËÆ£¬ÎļþÏÂÔزÙ×÷Ò²ÐèҪʹÓð¢ÀïÔÆOSS SDK¡£ÔÚcontrollerÖн¨ÉèdownloadÒªÁ죬´úÂëÈçÏ£º
use OSSOssClient;
use OSSCoreOssException;
public function download()
{ $accessKeyId = config('oss.accessKeyId'); $accessKeySecret = config('oss.accessSecret'); $endpoint = config('oss.endpoint'); $bucket = 'your_bucket_name'; // ½¨ÉèOSSClientʵÀý try { $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint); } catch (OssException $e) { printf(__FUNCTION__ . "°¢ÀïÔÆOSSÅþÁ¬Ê§°Ü£ºerror[%s]
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
“, $e->getMessage());
return; } // »ñÈ¡ÒªÏÂÔصÄÎļþÃû³Æ $object = 'your_object_name'; // ÏÂÔØÎļþ $content = ''; try { $content = $ossClient->getObject($bucket, $object); } catch (OssException $e) { return "Ö¸¶¨µÄÎļþ²»±£´æ"; } if ($content !== '') { // ÎļþÏÂÔزÙ×÷ } else { return "ÎļþÏÂÔØʧ°Ü"; } }
µÇ¼ºó¸´ÖÆ
ÔÚÒªÁìÖÐͬÑùÐèÒª»ñȡ֮ǰ½¨ÉèBucketʱµÄÈý¸ö²ÎÊý£¬È»ºó½¨ÉèOSSClientʵÀý¡£Í¨¹ý$object»ñÈ¡ÒªÏÂÔصÄÎļþ£¬Ê¹ÓÃgetObject()ÒªÁ콫ÎļþÏÂÔص½ÍâµØ¡£ÎļþÏÂÔزÙ×÷¿ÉÒÔͨ¹ýheader()ÒªÁìÉèÖÃÎļþÀàÐÍ¡¢¾ÞϸµÈÐÅÏ¢£¬×îºóͨ¹ýechoÊä³öÎļþÄÚÈÝ£¬ÊµÏÖÎļþÏÂÔزÙ×÷¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6ʵÏÖ°¢ÀïÔÆOSSÎļþÉÏ´«ºÍÏÂÔزÙ×÷µÄËùÓÐÄÚÈÝ¡£Í¨¹ý±¾ÎĵÄÏÈÈÝ£¬¶ÁÕß¿ÉÒÔÕÆÎÕ°¢ÀïÔÆOSS SDKµÄʹÓÃÒªÁ죬¸üÉîÈëµØÏàʶÔƶ˴洢ЧÀÍ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6ʵÏÖOSSÎļþÉÏ´«ºÍÏÂÔزÙ×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡