Home >PHP Framework >Laravel >How to use laravel s3
With the development of cloud computing and object storage technology, more and more applications are beginning to use cloud storage to store and process files uploaded by users. Amazon S3 is a very popular object storage service that can store an almost unlimited number of files and provides a highly available, scalable, and secure storage solution.
Laravel is a widely used PHP framework that provides rich features and tools to easily build and manage web applications. In this article, we will discuss how to use Laravel framework and Amazon S3 storage service to manage uploaded and stored files.
Install AWS SDK
Before starting to use the Amazon S3 service, we need to install the AWS SDK in the Laravel application. We can use composer to install the SDK. Open a terminal or command line and navigate to the root directory of your Laravel application. Then run the following command:
composer require aws/aws-sdk-php
This will install the AWS SDK in your Laravel application and get it ready to interact with Amazon S3.
Set up Amazon S3
When using Amazon S3, we need to set up the credentials and configuration required to interact with Amazon S3. These credentials and configuration include the AWS access key ID and secret access key, the region name and bucket name that will be used. We can add these credentials and configuration to the Laravel application's .env file for use at runtime.
The following is an example .env file that contains AWS credentials and configuration information:
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
After setting these credentials and configuration in the .env file, we can use the config function in the Laravel application access them from within the program's code. For example, to get the name of the AWS bucket, we simply call the following code:
$bucketName = config('aws.bucket');
Uploading files to Amazon S3
Uploading files to Amazon S3 in Laravel application is the same as uploading files to local server Or other cloud storage services are very similar. We first need to create a form that allows the user to select a file to upload. We then save the file into the Laravel application and upload it to Amazon S3.
Below is a sample upload form that users can use to upload files:
<form action="/upload" method="post" enctype="multipart/form-data"> @csrf <input type="file" name="file"> <button type="submit">上传</button> </form>
After the user uploads the file, we can use the following code in the Laravel controller to upload the file to 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保存到数据库或其他位置 }
This code will create an S3Client instance using the AWS SDK and initialize the instance with AWS credentials and configuration. It will then read the file from the request, save it to the Laravel application, and upload it to Amazon S3 using the putObject method. The ACL option is set to public-read to ensure that the file is publicly accessible after uploading.
After uploading the file to Amazon S3, we can save the URL of the file to a database or other location to display the file in the application.
Downloading files from Amazon S3
To download files from Amazon S3, we can use the getObject method in the aws-sdk-php library. Here is a sample code for downloading a file and saving it to the user's computer:
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 . '"', ]); }
This code will create a getObject request with the filename in Amazon S3 and get the file from S3. It then saves the file contents into the $fileContent variable and sends it to the user's computer as a response. The response is the file content with a Content-Type header and an attachment with a Content-Disposition header to tell the browser that the file should be downloaded rather than opened in the browser.
Summary
In this article, we introduced how to use the Amazon S3 file storage service in a Laravel application. We discussed how to install the aws-sdk-php library and configure the credentials and configuration required to interact with Amazon S3. We also demonstrated how to upload and download files. With these techniques, you can easily upload files to Amazon S3 and download them from your Laravel application.
The above is the detailed content of How to use laravel s3. For more information, please follow other related articles on the PHP Chinese website!