Search code examples
laravelgoogle-drive-apijobs

Get Id file uploaded google drive job queue laravel


i am making an image storage function with laravel job queue via googledrive. i am trying to initialize and store my photo. It works with the code:

 $filePut = file_get_contents($this->path);
 Storage::cloud()->put($this->name, $filePut);

but i can't get the return id

i am changing to another method and here is my 2nd code:

public function handle(GoogleClient $googleDrive)
    
    {
        dd($googleDrive);
        $driveService = new \Google_Service_Drive($googleDrive);
        $fileMetadata = new \Google_Service_Drive_DriveFile([
            'name' => $this->name,
        ]);
        $file = $driveService->files->create($fileMetadata, [
            'data' => file_get_contents($this->path),
            'uploadType' => 'multipart',
            'fields' => 'id',
        ]);
        $driveService->getClient()->setUseBatch(true);

        try {
            $batch = $driveService->createBatch();
            $userPermission = new \Google_Service_Drive_Permission([
                'type' => 'anyone',
                'role' => 'reader',
            ]);
            $request = $driveService->permissions->create($file->id, $userPermission, ['fields' => 'id']);
            $batch->add($request, 'user');
            $results = $batch->execute();
        } catch (\Exception $e) {

        } finally {
            $driveService->getClient()->setUseBatch(false);
        }

I noticed the data received from dd($googleDrive) is no data :

App\Components\GoogleClient {#3225
  #client: Google\Client {#3203
    -auth: null
    -http: null
    -cache: null
    -token: null
    -config: array:29 [
      "application_name" => ""
      "base_path" => "https://www.googleapis.com"
      "client_id" => ""
      "client_secret" => ""
      "credentials" => null
      "scopes" => null
      "quota_project" => null
      "redirect_uri" => null
      "state" => null
      "developer_key" => ""
      "use_application_default_credentials" => false
      "signing_key" => null
      "signing_algorithm" => null
      "subject" => null
      "hd" => ""
      "prompt" => ""
      "openid.realm" => ""
      "include_granted_scopes" => null
      "login_hint" => ""
      "request_visible_actions" => ""

I try to store it before the queue then the $drivegoogle data I get is as follows:

Google\Client {#3146
  -auth: null
  -http: null
  -cache: null
  -token: array:6 [
    "access_token" => "*************************************************************************************"
    "expires_in" => "*************************************************************************************"
    "scope" => "*************************************************************************************"
    "token_type" => "*************************************************************************************"
    "created" => "*************************************************************************************"
    "refresh_token" => "*************************************************************************************"
  ]
  -config: array:29 [
    "application_name" => ""
    "base_path" => "https://www.googleapis.com"
    "client_id" => "*************************************************************************************"
    "client_secret" => "*************************************************************************************"
    "credentials" => null
    "scopes" => null
    "quota_project" => null
    "redirect_uri" => null
    "state" => null
    "developer_key" => ""
    "use_application_default_credentials" => false
    "signing_key" => null
    "signing_algorithm" => null
    "subject" => null
    "hd" => ""
    "prompt" => ""
    "openid.realm" => ""
    "include_granted_scopes" => null
    "login_hint" => ""
    "request_visible_actions" =>

is there any workaround to get the id from the storage::put method, or is there another way to fix my current error, the error appears at new \Google_Service_Drive($googleDrive); constructor must be array or instance of Google\Client {"exception":"[object] (TypeError(code: 0): constructor must be array or instance of Google\\Client


Solution

  • $googleDrive = $googleDrive->getClient()