Skip to content

Instantly share code, notes, and snippets.

@lucacastelnuovo
Created November 30, 2022 15:53
Show Gist options
  • Save lucacastelnuovo/780c00ac7d3ed7ee1c745e6f38fe240e to your computer and use it in GitHub Desktop.
Save lucacastelnuovo/780c00ac7d3ed7ee1c745e6f38fe240e to your computer and use it in GitHub Desktop.
!!! THIS DOES NOT WORK !!!
<?php
namespace App\Adapters;
use League\Flysystem\Config;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
class EncryptedS3Adapter extends AwsS3V3Adapter
{
public function write(string $path, string $contents, Config $config): void
{
$contents = encrypt($contents);
Parent::write($path, $contents, $config);
}
public function writeStream(string $path, $contents, Config $config): void
{
$contents = encrypt($contents);
// TODO: this does not work
Parent::writeStream($path, $contents, $config);
}
public function read(string $path): string
{
$contents = Parent::read($path);
return decrypt($contents);
}
public function readStream(string $path)
{
$resource = Parent::readStream($path);
// TODO: this also does not work
return $resource;
}
}
<?php
namespace App\Providers;
use App\Adapters\EncryptedS3Adapter;
use League\Flysystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
class EncryptedS3Provider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Storage::extend('s3-encrypted', function ($app, $config) {
$adapter = new EncryptedS3Adapter(
Storage::disk('s3')->getClient(),
$config['bucket'],
);
return new FilesystemAdapter(
new Filesystem($adapter, $config),
$adapter,
$config
);
});
Storage::disk('s3-encrypted')->buildTemporaryUrlsUsing(fn ($path) => route('file', $path));
}
}
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DISK', 'local'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been set up for each driver as an example of the required values.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
'throw' => false,
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'public',
'throw' => false,
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
],
's3-encrypted' => [
'driver' => 's3-encrypted',
'bucket' => env('AWS_BUCKET'),
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
],
];
@lucacastelnuovo
Copy link
Author

This is my failed attempt to create a laravel filesystem driver that encrypts files before uploading them to S3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment