addMediaFromDisk is too slow. #2973
-
I'm having trouble with First, let me explain my implementation. We're working on a mobile application where the files are uploaded to When a user submit the form, the paths of the uploaded files are attached in the form, so we can move them from Because /**
* Create a new job instance.
*
* @return void
*/
public function __construct($model, $files, $collection = 'media')
{
$this->model = $model;
$this->files = $files;
$this->collection = $collection;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$files = new Collection($this->files ?: []);
try {
$files->each(fn ($file) => $this->add(
$this->model, $file, $this->collection
));
} catch (FileDoesNotExist $e) {
report($e);
return false;
}
}
/**
* Move media from temporary directory to the original directory
*
* @var \Illuminate\Database\Eloquent\Model $model
* @var string $filename
* @var string $collection
* @return void
*/
protected function add($model, string $filename, $collection)
{
$model->addMediaFromDisk('temp/'.$filename, 's3')
->toMediaCollection($collection, 's3');
} So what is the problem?The problem with this approach that it takes a minimum of 1 minute! Even though it takes 2 or 3 seconds when moving the file using: Storage::disk('s3')->move('temp/filename.ext', 'uploads/filename.ext') What I find out when I took a deep dive into the MediaLibrary source code that, it copies the file and then it deletes it, often this is cause of the time consumption. So my question now, is there anyway to reduce the time of this process by moving the file instead of copying? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Storage::disk('s3')->move('temp/filename.ext', 'uploads/filename.ext') On that example you only move a file on s3 First php upload file to server then move to If you know a solution feel free to make a PR |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
On that example you only move a file on s3
First php upload file to server then move to
s3
, ands3
is a external server, it takes time to upload(more with heavy files),Second
Storage::copy
comes from laravel wich uses thephpleague/flysystem, so the question must go for these packagesIf you know a solution feel free to make a PR