soarecostin / file-vault

A Laravel package for encrypting and decrypting files of any size
MIT License
185 stars 62 forks source link

Tips: encrypting local to remote(S3) #14

Open mikkame opened 3 years ago

mikkame commented 3 years ago
$crypt = new FileEncrypter(config('file-vault.key'), config('file-vault.cipher'));
$crypt->encrypt('local-path', 's3://bucket-name/path');

It working in my case.

alexc-wormhole commented 2 years ago

Additional tips if you're using multi-tenancy and don't want to use the Storage facade, This will take the file upload (/tmp/12345) and encrypt the contents into a temporary file for the request (/tmp/abcdef), then upload it afterwards.

use SoareCostin\FileVault\FileEncrypter;
use Illuminate\Support\Str;
use \Storage;

    $save_as = (string)Str::uuid() . '.' . strtolower($this->request->file('blob')->getClientOriginalExtension()); // my_photo.JPEG

    $temporary = tmpfile();

    $encrypter = new FileEncrypter (config('file-vault.key'), config('file-vault.cipher'));
    $encrypter->encrypt ($this->request->file('blob')->path(), stream_get_meta_data($temporary)['uri']);

    Storage::disk ($this->disk)->put ($this->folder_prefix.'/'.$save_as, stream_get_meta_data($temporary)['uri']);