SwitchFilesystem with S3 storage & league/flysystem-aws-s3-v3 ./. Caching #335
-
Hello together, I am facing the challenge to use S3 storage in multi-tenant manner instead of local storage. ChallengeSwitching between tenants to provide dedicated storage per tenant. Solution attempt 1The same procedure as for local storage. Simply overwrite the configuration parameter "" with a task. Why it is not a solution?The package Solution attempt 2Instead of changing only the root path dynamically, I modified the task "" to create a new disk with the prefix of the tenantId when using s3 as driver. Additionally I now use Why it is a solution?The disk is still cached, but since it is accessed dynamically, the correct disk is used - even if the tenant is changed. So from a technical point of view, this is a valid, working solution. QuestionIs this an acceptable solution or is there a pitfall I have not considered? More details
...
#'tenant' => [
# 'driver' => 'local',
# 'root' => null,
#],
'tenant' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => null,
'multitenant_bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
'root' => null,
],
...
<?php
namespace App\Multitenancy;
use Illuminate\Support\Str;
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
use Spatie\Multitenancy\Exceptions\InvalidConfiguration;
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
class SwitchTenantFilesystemDiskTask implements SwitchTenantTask
{
use UsesMultitenancyConfig;
public function makeCurrent(Tenant $tenant): void
{
$this->setTenantFilesystemDisk($tenant->id);
}
public function forgetCurrent(): void
{
$this->setTenantFilesystemDisk(null);
}
protected function setTenantFilesystemDisk(?string $tenantId)
{
$tenantDiskName = 'tenant';
if (is_null(config("filesystems.disks.{$tenantDiskName}"))) {
throw InvalidConfiguration::tenantConnectionDoesNotExist($tenantDiskName);
}
if ("s3" === config("filesystems.disks.{$tenantDiskName}.driver")) {
if (is_null($tenantId)) {
foreach (config('filesystems.disks') as $k => $v) {
if (Str::startsWith($k, $tenantDiskName . "_")) {
config(["filesystems.disks.{$k}" => null]);
}
}
} else {
$tenantStoragePath = "data/" . $tenantId;
$dynDisk = $tenantDiskName . "_" . $tenantId;
config([
"filesystems.disks.{$dynDisk}" => config("filesystems.disks.{$tenantDiskName}"),
]);
config([
"filesystems.disks.{$dynDisk}.root" => $tenantStoragePath,
"filesystems.disks.{$dynDisk}.bucket" => config("filesystems.disks.{$tenantDiskName}.multitenant_bucket"),
]);
}
} else {
$tenantStoragePath = is_null($tenantId)
? null
: storage_path('app/' . $tenantId);
config([
"filesystems.disks.{$tenantDiskName}.root" => $tenantStoragePath,
]);
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm using a solution like your |
Beta Was this translation helpful? Give feedback.
I'm using a solution like your
SwitchTenantFilesystemDiskTask.php
with no issue, at least not for now 🥸.