The installation and setting of SFTP is beyond the scope of this article. If you want to know more, it is recommended to read another article
https://www.ccc.tc/article/Laravel-SFTP-Driver-Configuration
In this article, I will use a very fast Sample Demo, how I can use the self-built artisan command to synchronize the data from the remote end to the local end.
Some people may ask why rsync is not used. This article is to introduce the use of Storage to synchronize files. 🤪
Here, we can see the benefits brought by the Laravel framework,
For example, Storage can also be replaced with your own Google Drive. I will write another article when I have a chance.
In terms of design, when the local file is uploaded, it is also automatically uploaded to the cloud (Google Drive) through the queue mechanism.
Delete files on the local side and delete cloud files synchronously. When the user downloads a file, it will be downloaded from the cloud, and if there is no file in the cloud, it will be downloaded by the local side.
But from my own experience, users have to log in to Google to download my Google Drive files.
Let's look at the code first. I set up an sftp disk called ccc in config/filesystems.php.
'ccc' => [
'driver' => 'sftp',
'host' => '192.168.99.2',
'username' => 'dlaravel',
'privateKey' => '/home/dlaravel/.ssh/id_ed25519',
'port' => 2258,
'root' => '/var/www/html/storage/app/public',
'url'=>'https://www.ccc.tc/storage',
],
Then I added a new artisan command in routes/console.php
//同步遠端資料
Artisan::command("fs:sync {disk} {directory}", function ($disk, $directory) {
//Specify the disk and list the contents of the file. In this article, the files in the images directory of my remote host will be listed.
$files = Storage::disk($disk)->files($directory);
foreach ($files as $file) {
//Because there will be a time difference when the file is captured from the remote end to the local end, through this function, download the file and adjust the file to the modification time of the remote host
$download_to_local = function () use ($disk, $file) {
$file_content = Storage::disk($disk)->get($file);//Get remote file content
Storage::disk("local")->put('public/' . $file, $file_content); //Save the content of the remote file to the local with the same file name
$lastModifiedTime = Storage::disk($disk)->lastModified($file); //The last modification time of the remote file
$localFile = Storage::disk("local")->path("public/" . $file); //Get the local file path
//Adjust the modification time to be the same as the remote file
touch($localFile, $lastModifiedTime);
};
//synchronous processing
if (!Storage::disk("local")->exists('public/' . $file)) {
//The above condition, check if the local file exists and download it in the file
$download_to_local(); //Download and correct time
echo sprintf("%s created.\n", $file);
} elseif (Storage::disk($disk)->lastModified($file) > Storage::disk("local")->lastModified('public/' . $file)) {
//The above conditions, because the remote and local have the same file, compare the modification time, if the time of the remote host is greater than that of the local,
//Represents that the remote file has been updated, and the file is updated.
$download_to_local(); //Download and correct time
echo sprintf("%s updated.\n", $file);
} else {
//echo sprintf("sync skip %s, already up to date.\n", $file);
}
}
});
If you have noticed the above code, I use the get and put functions provided by Laravel's Storage to directly capture files on the remote host and save files on the local host, which is really easy to use. 😆
The following screen, the synchronization is complete
We directly log on to these two websites to check, and the modification time is the same, which is what I want.
If we casually touhc the remote file, update the modification time to simulate that the remote file has been modified.
Let's execute the synchronization command again from the local side, because I am painting and posting the map, so the three picture files I posted above are added to the picture below created,
, but you can see that the file of my touch has been updated, and it will show updated.🤣
images/sqGaCIj8WkZFKvifC1TITn81QocunOyHYQq0m7Ae.png updated.
Actual operation screen
Anyway, he really works. 😁
No Comment
Post your comment