ivanvermeyen / laravel-google-drive-demo

Laravel & Google Drive Storage - Demo project with Laravel 5.4
403 stars 155 forks source link

How to upload to Google Drive directly without uploading file to server #87

Open sushilsth opened 4 years ago

sushilsth commented 4 years ago

Hi,

I want to upload file directly from client to Google drive.

I can upload to google drive after uploading to the server and then to drive.

Here is what I tried.

 if ($request->hasFile('file')) {
       $files = $request->file('file');
       for ($i = 0; $i < count($files); $i++) {
            if ($files[$i]->isValid()) {
                   $attachment = $files[$i];
                   $fileContent = File::get($attachment);
                  $name = explode('.', $attachment->getClientOriginalName());
                  $fileName = $name[0] . '.' . $attachment->getClientOriginalExtension();
                  $fileNameUnique = $name[0] . '_' . date('Ymdhis') . '.' . $attachment->getClientOriginalExtension();
                   //get drive folder
                  $homework_dir = $contents->where('type', '=', 'dir')
                                                                    ->where('filename', '=', 'homework')
                                                                    ->first();
                  $dir = '/' . $homework_dir["path"] . '/';
                  Storage::drive('google')->put($homework_dir["path"] .  '/' . $filename, $fileContent );
      }}}

Is this something possible?

Another approach is I tried to upload file to the server first and then moving to Google Drive by using Laravel Queue. All went correctly except I am only able to upload upto 4 images to the google drive. Suppose, I upload 6 images then the first four files in the loop gets uploaded and rest donot gets upload. Is this somethings that I need to makes changes to config file?

Thank you.

ivanvermeyen commented 4 years ago

Hi,

Take a look here: https://github.com/ivanvermeyen/laravel-google-drive-demo/issues/61

For multiple files, I always use some sort of JS drag and drop package that would send files one by one even if you select multiple.

sushilsth commented 4 years ago

Thank you for the reply. I tried the solution mentioned in #61 https://laravel.com/docs/5.8/requests#storing-uploaded-files. Upload works fine for only 1 file. I want to upload multiple files but I got the below error

 The stream or file "/var/www/storage/logs/laravel-2020-07-26.log" could not be opened: failed to open stream: Permission denied

Any Idea?

ivanvermeyen commented 4 years ago

Are the permissions set correctly?

Directory Permissions After installing Laravel, you may need to configure some permissions. Directories within the storage and the bootstrap/cache directories should be writable by your web server or Laravel will not run. If you are using the Homestead virtual machine, these permissions should already be set.