laravel s3ÔõôÓÃ
Ëæ×ÅÔÆÅÌËãºÍ¹¤¾ß´æ´¢ÊÖÒÕµÄÉú³¤£¬Ô½À´Ô½¶àµÄÓ¦ÓóÌÐò×îÏȽÓÄÉÔÆ´æ´¢À´´æ´¢ºÍ´¦ÀíÓû§ÉÏ´«µÄÎļþ¡£amazon s3ÊÇÒ»ÖÖºÜÊÇÊ¢ÐеŤ¾ß´æ´¢Ð§ÀÍ£¬Ëü¿ÉÒÔ´æ´¢ÏÕЩÎÞÏÞÊýÄ¿µÄÎļþ£¬²¢ÌṩÁ˸߿ÉÓ㬿ÉÀ©Õ¹ºÍÇå¾²µÄ´æ´¢½â¾ö¼Æ»®¡£
LaravelÊÇÒ»ÖÖÆÕ±éʹÓõÄPHP¿ò¼Ü£¬ËüÌṩÁ˸»ºñµÄ¹¦Ð§ºÍ¹¤¾ß£¬¿ÉÇáËɹ¹½¨ºÍÖÎÀíWebÓ¦ÓóÌÐò¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛÔõÑùʹÓÃLaravel¿ò¼ÜºÍAmazon S3´æ´¢Ð§ÀÍÀ´ÖÎÀíÉÏ´«ºÍ´æ´¢µÄÎļþ¡£
×°ÖÃAWS SDK
ÔÚ×îÏÈʹÓÃAmazon S3ЧÀÍ֮ǰ£¬ÎÒÃÇÐèÒªÔÚLaravelÓ¦ÓóÌÐòÖÐ×°ÖÃAWS SDK¡£ÎÒÃÇ¿ÉÒÔʹÓÃcomposerÀ´×°ÖÃSDK¡£·¿ªÖն˻òÏÂÁîÐУ¬²¢µ¼º½µ½LaravelÓ¦ÓóÌÐòµÄ¸ùĿ¼¡£È»ºóÔËÐÐÒÔÏÂÏÂÁ
composer require aws/aws-sdk-php
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚLaravelÓ¦ÓóÌÐòÖÐ×°ÖÃAWS SDK£¬²¢×¼±¸ºÃÓëAmazon S3½»»¥¡£
ÉèÖÃAmazon S3
ÔÚʹÓÃAmazon S3ʱ£¬ÎÒÃÇÐèÒªÉèÖÃÓëAmazon S3½»»¥ËùÐèµÄƾ֤ºÍÉèÖá£ÕâЩƾ֤ºÍÉèÖðüÀ¨AWS»á¼ûÃÜÔ¿IDºÍÉñÃØ»á¼ûÃÜÔ¿£¬½«ÒªÊ¹ÓõÄÇøÓòÃû³ÆºÍÍ°Ãû³Æ¡£ÎÒÃÇ¿ÉÒÔ½«ÕâЩƾ֤ºÍÉèÖÃÌí¼Óµ½LaravelÓ¦ÓóÌÐòµÄ.envÎļþÖУ¬ÒÔ±ãÔÚÔËÐÐʱʹÓá£
ÏÂÃæÊÇÒ»¸öʾÀý.envÎļþ£¬ÆäÖаüÀ¨AWSƾ֤ºÍÉèÖÃÐÅÏ¢£º
AWS_ACCESS_KEY_ID=your_access_key_id AWS_SECRET_ACCESS_KEY=your_secret_access_key AWS_DEFAULT_REGION=us-west-2 AWS_BUCKET=your_bucket_name
µÇ¼ºó¸´ÖÆ
ÔÚ.envÎļþÖÐÉèÖúÃÕâЩƾ֤ºÍÉèÖúó£¬ÎÒÃÇ¿ÉÒÔʹÓÃconfigº¯ÊýÔÚLaravelÓ¦ÓóÌÐòµÄ´úÂëÖлá¼ûËüÃÇ¡£ÀýÈ磬Ҫ»ñÈ¡AWSÍ°µÄÃû³Æ£¬ÎÒÃÇÖ»ÐèŲÓÃÒÔÏ´úÂ룺
$bucketName = config('aws.bucket');
µÇ¼ºó¸´ÖÆ
ÉÏ´«Îļþµ½Amazon S3
ÔÚLaravelÓ¦ÓóÌÐòÖÐÉÏ´«Îļþµ½Amazon S3ÓëÉÏ´«Îļþµ½ÍâµØЧÀÍÆ÷»òÆäËûÔƴ洢ЧÀͺÜÊÇÏàËÆ¡£ÎÒÃÇÊ×ÏÈÐèÒª½¨ÉèÒ»¸ö±íµ¥£¬ÔÊÐíÓû§Ñ¡ÕªÒªÉÏ´«µÄÎļþ¡£È»ºó£¬ÎÒÃǽ«¸ÃÎļþÉúÑĵ½LaravelÓ¦ÓóÌÐòÖУ¬È»ºó½«ÆäÉÏ´«µ½Amazon S3¡£
ÏÂÃæÊÇÒ»¸öʾÀýÉÏ´«±íµ¥£¬Óû§¿ÉÒÔʹÓÃËüÀ´ÉÏ´«Îļþ£º
<form action="/upload" method="post" enctype="multipart/form-data"> @csrf <input type="file" name="file"> <button type="submit">ÉÏ´«</button> </form>
µÇ¼ºó¸´ÖÆ
ÔÚÓû§ÉÏ´«Îļþºó£¬ÎÒÃÇ¿ÉÒÔÔÚLaravel¿ØÖÆÆ÷ÖÐʹÓÃÒÔÏ´úÂ뽫ÎļþÉÏ´«µ½Amazon S3£º
use AwsS3S3Client; use IlluminateHttpRequest; public function upload(Request $request) { $file = $request->file('file'); $fileName = $file->getClientOriginalName(); $s3 = new S3Client([ 'version' => 'latest', 'region' => config('aws.region'), 'credentials' => [ 'key' => config('aws.key'), 'secret' => config('aws.secret'), ], ]); $bucketName = config('aws.bucket'); $result = $s3->putObject([ 'Bucket' => $bucketName, 'Key' => $fileName, 'Body' => file_get_contents($file), 'ACL' => 'public-read', ]); $fileUrl = $result['ObjectURL']; // ½«ÎļþURLÉúÑĵ½Êý¾Ý¿â»òÆäËûλÖà }
µÇ¼ºó¸´ÖÆ
´Ë´úÂ뽫ʹÓÃAWS SDK½¨ÉèÒ»¸öS3ClientʵÀý£¬²¢Ê¹ÓÃAWSƾ֤ºÍÉèÖóõʼ»¯¸ÃʵÀý¡£È»ºó£¬Ëü½«´ÓÇëÇóÖжÁÈ¡Îļþ£¬½«ÆäÉúÑĵ½LaravelÓ¦ÓóÌÐòÖУ¬²¢Ê¹ÓÃputObjectÒªÁ콫ÆäÉÏ´«µ½Amazon S3¡£ACLÑ¡ÏîÉèÖÃΪpublic-readÒÔÈ·±£¸ÃÎļþÔÚÉÏ´«ºó¿ÉÒÔ¹ûÕæ»á¼û¡£
ÔÚ½«ÎļþÉÏ´«µ½Amazon S3ºó£¬ÎÒÃÇ¿ÉÒÔ½«ÎļþµÄURLÉúÑĵ½Êý¾Ý¿â»òÆäËûλÖã¬ÒÔ±ãÔÚÓ¦ÓóÌÐòÖÐÏÔʾÎļþ¡£
ÏÂÔØÎļþ´ÓAmazon S3
Òª´ÓAmazon S3ÏÂÔØÎļþ£¬ÎÒÃÇ¿ÉÒÔʹÓÃaws-sdk-php¿âÖеÄgetObjectÒªÁì¡£ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룬ÓÃÓÚÏÂÔØÎļþ²¢½«ÆäÉúÑĵ½Óû§µÄÅÌËã»úÉÏ£º
use AwsS3S3Client; use IlluminateHttpRequest; public function download($fileName) { $s3 = new S3Client([ 'version' => 'latest', 'region' => config('aws.region'), 'credentials' => [ 'key' => config('aws.key'), 'secret' => config('aws.secret'), ], ]); $bucketName = config('aws.bucket'); $result = $s3->getObject([ 'Bucket' => $bucketName, 'Key' => $fileName, ]); $fileContent = $result['Body']->getContents(); return response($fileContent, 200, [ 'Content-Type' => $result['ContentType'], 'Content-Disposition' => 'attachment;filename="' . $fileName . '"', ]); }
µÇ¼ºó¸´ÖÆ
´Ë´úÂ뽫ʹÓÃAmazon S3ÖеÄÎļþÃû½¨ÉègetObjectÇëÇ󣬲¢´ÓS3ÖлñÈ¡¸ÃÎļþ¡£È»ºó£¬Ëü½«ÎļþÄÚÈÝÉúÑĵ½$fileContent±äÁ¿ÖУ¬²¢½«Æä×÷ΪÏìÓ¦·¢Ë͵½Óû§µÄÅÌËã»ú¡£ÏìÓ¦ÊÇ´øÓÐContent-Type±êÍ·µÄÎļþÄÚÈݺÍContent-Disposition±êÍ·µÄ¸½¼þ£¬ÒÔ¸æËßä¯ÀÀÆ÷¸ÃÎļþÓ¦¸ÃÏÂÔضø²»ÊÇÔÚä¯ÀÀÆ÷Öз¿ª¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùÔÚLaravelÓ¦ÓóÌÐòÖÐʹÓÃAmazon S3Îļþ´æ´¢Ð§ÀÍ¡£ÎÒÃÇÌÖÂÛÁËÔõÑù×°ÖÃaws-sdk-php¿â£¬²¢ÉèÖÃÓëAmazon S3½»»¥ËùÐèµÄƾ֤ºÍÉèÖá£ÎÒÃÇ»¹ÑÝʾÁËÔõÑùÉÏ´«ºÍÏÂÔØÎļþ¡£Í¨¹ýÕâЩÊÖÒÕ£¬Äú¿ÉÒÔÇáËɵؽ«ÎļþÉÏ´«µ½Amazon S3£¬²¢´ÓLaravelÓ¦ÓóÌÐòÖÐÏÂÔØËüÃÇ¡£
ÒÔÉϾÍÊÇlaravel s3ÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡