Search code examples
laravelgoogle-cloud-storageservice-accountsgoogle-iam

Uploading image to google cloud I got error A keyfile was given


Under https://console.cloud.google.com/ I created new app and created json file with credentials and in .env I added line :

GOOGLE_CLOUD_KEY_FILE="client_secret_NNNNMYFILE.apps.googleusercontent.com.json"

but I got error tring to upload my image to google cloud:

A keyfile was given, but it does not contain a project ID. This can indicate an old and obsolete keyfile, in which case you should create a new one. To suppress this message, set `suppressKeyFileNotice` to `true` in your client configuration. To learn more about generating new keys, see this URL: https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys

What kind of project ID is it ? Is it some option under console.cloud.google ? Where can I set it ? I opened the link above but I did not find any “suppressKeyFileNotice” mentioned there...

File Project/public/client_secret_278938643693-44adrhckvqgbcikr943lj29tvg89hojr.apps.googleusercontent.com.json has content :

{"web":{"client_id":"NNNNNNhojr.apps.googleusercontent.com","project_id":"tads-gsc-test","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"NNNN-NNNNNNNN","redirect_uris":["https://my-site-ref.com/authorized_redirect"],"javascript_origins":["https:///my-site-ref.com"]}}

Control action :

$fileContents= Storage::url('public/todo_list_app/1/logo-file.jpeg'); ;

$disk = Storage::disk('gcs');

$disk->put('avatars/1', $fileContents);
$exists = $disk->exists('logo-file.jpeg');

$time = $disk->lastModified('file1.jpg');

In config/filesystems.php :

'gcs' => [
    'driver' => 'gcs',
    'key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
    'key_file' => [], // optional: Array of data that substitutes the .json file (see below)
    'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'), // optional: is included in key file
    'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
    'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', ''), // optional: /default/path/to/apply/in/bucket
    'apiEndpoint' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
    'visibility' => 'public', // optional: public|private
    'metadata' => ['cacheControl'=> 'public,max-age=86400'], // optional: default metadata
],

How can I fix it?

Thanks in advance!


Solution

  • The general process to build an application using Google Cloud APIs, follow these general steps:

    • Choose and use the provided Google Cloud Client Libraries
    • Determine the correct authentication flow for your application
    • Find or create the application credentials needed for your application
    • Pass the application credentials to the client libraries at application startup time, ideally through Application Default Credentials [4] (ADC)

    You should choose application credentials based on what your application needs and where it runs.

    To use service accounts with the Google Cloud CLI, you need to set an environment variable [5]where your code runs.

    Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable applies only to your current shell session. If you want the variable to apply to future shell sessions, set the variable in your shell startup file, for example in the ~/.bashrc or ~/.profile file.

    Also, as per comments and suggestions ,you are using the OAuth 2.0 Client IDs secret json file,whereas you should instead be using the right service account keys json file[1].

    key_file_path' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json

    You may refer to the following for more information
    [1]:https://cloud.google.com/iam/docs/creating-managing-service-account-keys#get-key
    [2] : https://cloud.google.com/docs/authentication/getting-started
    [3] :https://cloud.google.com/docs/authentication/best-practices-applications
    [4] : https://cloud.google.com/docs/authentication/production#automatically
    [5] : https://en.wikipedia.org/wiki/Environment_variable