Superbalist / laravel-google-cloud-storage

A Google Cloud Storage filesystem for Laravel
MIT License
509 stars 145 forks source link

supplied key param cannot be coerced into a private key #103

Open AmG2000 opened 3 years ago

AmG2000 commented 3 years ago

My config

'gcs' => [
          'driver' => 'gcs',
          'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
          'key_file' => [
             'type' => env('GOOGLE_CLOUD_ACCOUNT_TYPE'),
             'private_key_id' => env('GOOGLE_CLOUD_PRIVATE_KEY_ID'),
             'private_key' => env('GOOGLE_CLOUD_PRIVATE_KEY'),
             'client_email' => env('GOOGLE_CLOUD_CLIENT_EMAIL'),
             'client_id' => env('GOOGLE_CLOUD_CLIENT_ID'),
             'auth_uri' => env('GOOGLE_CLOUD_AUTH_URI'),
             'token_uri' => env('GOOGLE_CLOUD_TOKEN_URI'),
             'auth_provider_x509_cert_url' => env('GOOGLE_CLOUD_AUTH_PROVIDER_CERT_URL'),
             'client_x509_cert_url' => env('GOOGLE_CLOUD_CLIENT_CERT_URL'),
           ], // optional: /path/to/service-account.json
          'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET'),
          'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
          'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
          'visibility' => 'public', // optional: public|private
],

When I try storing a file using Storage:disk('gcs')->putFile('folder', $request->file('example_file')) I get the error "Supplied key param cannot be coerced into private key. Does anyone know how to get around this? I'm on laravel 7.22.2

Jakatut commented 3 years ago

Hey,

Any chance you've found a resolution to this? I've run into the same issue.

AmG2000 commented 3 years ago

I realized the issue only occured when I stored the key in an environment variable, as opposed to just referencing the json file. So I just referenced the json file instead of supplying an array of its values

Jakatut commented 3 years ago

Ah I see. I ended up using the private key still, but it turns out Laravel escaped the new lines it read. I just replaced these escaped lines with new lines after reading the env.

Thanks for the reply :)

AmG2000 commented 3 years ago

I just replaced these escaped lines with new lines after reading the env

How'd you do this?

Jakatut commented 3 years ago

like this: str_replace('\n', "\n", env('GOOGLE_CLOUD_PRIVATE_KEY')),

Make sure your private key is wrapped in quotes in the .env file too.

AmG2000 commented 3 years ago

Right. Your solution's much more elegant than mine. Thankyou

st-man-hori commented 3 years ago

I had the same problem. but this issue solved it. Thanks!