📌  相关文章
📜  maatwebsite excel store s3 (1)

📅  最后修改于: 2023-12-03 14:44:05.543000             🧑  作者: Mango

Maatwebsite Excel Store S3

Introduction

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.

Features
  • Simple and easy to install package
  • Upload and download Excel files to and from Amazon S3
  • Ability to set file permissions and access control lists
  • Encrypt and decrypt Excel files
  • Multiple storage drivers support
Requirements
  • PHP 7.2 or later
  • Laravel 5.7 or later
  • Amazon S3 account with a bucket
Installation

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
Usage
Uploading Excel Files

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));
Downloading Excel Files

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'
]);
Encryption and Decryption

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'
]);
Setting File Permissions and ACL

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.

Conclusion

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.