📅  最后修改于: 2023-12-03 14:44:05.543000             🧑  作者: Mango
Maatwebsite Excel Store S3 is a package for Laravel framework that provides an easy way to store your Excel files on Amazon S3. With this package, you can upload and download Excel files directly to and from your Amazon S3 bucket.
You can install this package using composer:
composer require maatwebsite/excel-store-s3
After installation, you need to set up your Amazon S3 account credentials in your .env
file:
AWS_ACCESS_KEY_ID=your_access_key_here
AWS_SECRET_ACCESS_KEY=your_secret_key_here
AWS_DEFAULT_REGION=your_region_here
AWS_BUCKET=your_bucket_name_here
To upload an Excel file to Amazon S3, you can use the put()
method:
use Maatwebsite\Excel\Store\S3;
$s3 = new S3();
$file = $request->file('excel_file');
$name = $file->getClientOriginalName();
$path = 'path/to/your/file';
$s3->put($path . '/' . $name, file_get_contents($file));
To download an Excel file from Amazon S3, you can use the get()
method:
use Maatwebsite\Excel\Store\S3;
$s3 = new S3();
$path = 'path/to/your/file';
$file = $s3->get($path);
return response($file, 200, [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'attachment; filename=your_file_name.xlsx'
]);
If you want to upload encrypted Excel files, you can use the putEncrypted()
method:
use Maatwebsite\Excel\Store\S3;
$s3 = new S3();
$file = $request->file('excel_file');
$name = $file->getClientOriginalName();
$path = 'path/to/your/file';
$s3->putEncrypted($path . '/' . $name, file_get_contents($file), 'password');
And to download encrypted Excel files, you can use the getDecrypted()
method:
use Maatwebsite\Excel\Store\S3;
$s3 = new S3();
$path = 'path/to/your/file';
$file = $s3->getDecrypted($path, 'password');
return response($file, 200, [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => 'attachment; filename=your_file_name.xlsx'
]);
You can set file permissions and ACL for your Excel files uploaded to Amazon S3 by using the put()
method with optional parameters visibility
and ACL
. For example:
use Maatwebsite\Excel\Store\S3;
$s3 = new S3();
$file = $request->file('excel_file');
$name = $file->getClientOriginalName();
$path = 'path/to/your/file';
$s3->put($path . '/' . $name, file_get_contents($file), 'public', 'private');
In the example above, the visibility
parameter is set to public
, which means that the file can be accessed publicly, and the ACL
parameter is set to private
, which means that the owner has full control over the file.
Maatwebsite Excel Store S3 is a useful and easy-to-use package for Laravel framework that allows you to store and retrieve Excel files from Amazon S3. It also provides additional features such as encryption, file permissions, and access control lists for your Excel files.