×ðÁú¿­Ê±¹ÙÍøµÇ¼

ÔõÑùʹÓÃ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ÎļþÉÏ´«ºÍÏÂÔزÙ×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ