尊龙凯时人生就是搏

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ÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ