Laravel 5: How do you copy a local file to Amazon S3? - laravel

I'm writing code in Laravel 5 to periodically backup a MySQL database. My code thus far looks like this:
$filename = 'database_backup_'.date('G_a_m_d_y').'.sql';
$destination = storage_path() . '/backups/';
$database = \Config::get('database.connections.mysql.database');
$username = \Config::get('database.connections.mysql.username');
$password = \Config::get('database.connections.mysql.password');
$sql = "mysqldump $database --password=$password --user=$username --single-transaction >$destination" . $filename;
$result = exec($sql, $output); // TODO: check $result
// Copy database dump to S3
$disk = \Storage::disk('s3');
// ????????????????????????????????
// What goes here?
// ????????????????????????????????
I've seen solutions online that would suggest I do something like:
$disk->put('my/bucket/' . $filename, file_get_contents($destination . $filename));
However, for large files, isn't it wasteful to use file_get_contents()? Are there any better solutions?

There is a way to copy files without needing to load the file contents into memory using MountManager.
You will also need to import the following:
use League\Flysystem\MountManager;
Now you can copy the file like so:
$mountManager = new MountManager([
's3' => \Storage::disk('s3')->getDriver(),
'local' => \Storage::disk('local')->getDriver(),
]);
$mountManager->copy('s3://path/to/file.txt', 'local://path/to/output/file.txt');

You can always use a file resource to stream the file (advisable for large files) by doing something like this:
Storage::disk('s3')->put('my/bucket/' . $filename, fopen('path/to/local/file', 'r+'));
An alternative suggestion is proposed here. It uses Laravel's Storage facade to read the stream. The basic idea is something like this:
$inputStream = Storage::disk('local')->getDriver()->readStream('/path/to/file');
$destination = Storage::disk('s3')->getDriver()->getAdapter()->getPathPrefix().'/my/bucket/';
Storage::disk('s3')->getDriver()->putStream($destination, $inputStream);

You can try this code
$contents = Storage::get($file);
Storage::disk('s3')->put($newfile,$contents);
As Laravel document this is the easy way I found to copy data between two disks

Laravel has now putFile and putFileAs method to allow stream of file.
Automatic Streaming
If you would like Laravel to automatically manage
streaming a given file to your storage location, you may use the
putFile or putFileAs method. This method accepts either a
Illuminate\Http\File or Illuminate\Http\UploadedFile instance and will
automatically stream the file to your desired location:
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));
// Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
Link to doc: https://laravel.com/docs/5.8/filesystem (Automatic Streaming)
Hope it helps

Looking at the documentation the only way is using method put which needs file content. There is no method to copy file between 2 file systems so probably the solution you gave is at the moment the only one.
If you think about it, finally when copying file from local file system to s3, you need to have file content to put it in S3, so indeed it's not so wasteful in my opinion.

I solved it in the following way:
$contents = \File::get($destination);
\Storage::disk('s3')
->put($s3Destination,$contents);
Sometimes we don't get the data using $contents = Storage::get($file); - storage function so we have to give root path of the data using Laravel File instead of storage path using Storage.

Related

Set Filename Storage::put

I cant set a filename with Storage::put in Laravel 5.8
I tried the following:
Storage::put(private/foo/bar , $file, 'private');
This is creating the folder structure with a random generated filename inside. (Like: private/foo/bar/uidjasbknfsdoiruewjnfsdai.pdf)
Storage::put(private/foo/bar/file.pdf , $file, 'private');
This is creating this: private/foo/bar/file.pdf/uidjasbknfsdoiruewjnfsdai.pdf
I expect my own given filename on this private file. The file should not be public.
According to Laravel docs:
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
You may also use the putFileAs method on the Storage facade, which will perform the same file manipulation as the example above:
$path = Storage::putFileAs(
'avatars', $request->file('avatar'), $request->user()->id
);
The third argument for the putFileAs is the visibility of the file. If you would like every users can access it, use public instead of $request->user()->id.
You can read more here.

renaming a file before uploading to digital ocean spaces using laravel

To upload a file I use
Storage::disk('spaces')->putFile('uploads', $request->file, 'public');
The file is saved successfully on digital ocean spaces. But I want to rename it to something like this user_1_some_random_string.jpg. And then save it.
How can I do it?
The move method may be used to rename or move an existing file to a new location:
Storage::move('hodor/oldfile-name.jpg', 'hodor/newfile-name.jpg');
Also:
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
More: https://laravel.com/docs/5.6/filesystem
try use rand()
$ext = $request->file('file')->getClientOriginalExtension();
$name = rand(11111,99999).'.'.$ext;
Storage::disk('spaces')->putFile('uploads', $name, 'public');
This is pretty old but I found an answer for anyone still looking.
You need to use the method putFileAs, as far as I can see
the first parameter is the bucket/location. I tested this and it will create a new folder if you use 'uploads/testz' it created the 'testz' in the uploads folder on spaces.
the second parameter is the request file object, in my case $request->file('file')
the third parameter is the filename that you WANT to store the file as. I tested and if you 'testz/<yourspecialfilename.extension>' it will create the same folder as in parameter 1, which suggests to me that the method concats param 1 and 3.
So the full snippet in my controller is
public function create(Request $request){ Storage::disk('spaces')->putFileAs('uploads/testz', $request->file('file'), 'mychosenfilename.mydesiredextension');
return redirect()->back();}

Adding Email Attachments From Local Storage

I am using laravel/lumen. I am able to save files using Storage::disk("local")->put(); in my storage directory. However now I want to attach a few of those files to an email and send, this is done with a job the error I get is
lumen.ERROR: exception 'Swift_IoException' with message 'Unable to
open file for reading
Now I read something about symbolic linking which I tried but that simply did not change the result, I was still unable to attach files i n my storage folder to my emails.
This is my directory structure:
/home/xxxxxx/:
-example.app
--storage
---app
----public
-public_html
--example.app
---storage
Attaching the file like this:
foreach ($params["attachments"] as $attachment) {
$mail->attach($attachment["file"], [
'as' => $attachment["name"],
'mime' => $attachment["mime"]
]);
}
You've already realized that attach() method expects the full path to the file. However, others may find useful know how to achieve that. So, in case you're using the default settings for local store, you may be able to get the full path by calling the storage_path() helper. This way:
/*
* This will return the full path to the file:
*
* /path/to/laravel/storage/app/attachment/path
*/
storage_path('app/' . $attachment['file']);
Okay so apparently swift mailer fails when you pass a full url instead of the real path to the file. Didn't know this, don't know if it's a bug or it was done by design.

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

Laravel image upload creating folder instead of file

When I try to upload image from the form , Laravel is creating directory out of image name. Here is my basic code:
$file = Input::file("image");
$file = $file->move(public_path(). "/img/".$file->getClientOriginalName());
//file name is IPC_diagram.png
When die and dump I got this:
'/var/www/php/frbit/l4blog/public/img/IPC_diagram.png/phpvEb9zk'
Now name of image is name of new folder and image is renamed to some random php string, and placed in that folder.
What is the problem. Maybe something related to Linux specific handling of files. Also I was looking into symfony code for this, and symfony is trying to crete new folder every time file is moved but I don't understand how it is related to my code.
Thanks for help.
Do not use dot. Use comma, like this:
$name = time() . $file->getClientOriginalName();
$file->move('uploads/posts/',$name);

Resources