Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

Google Cloud Storage issue with PHP

I am using this library: google/cloud-storage.

In my storage I made a bucket, called: mycustombucket. My goal is to upload a csv file in that bucket. Here is my code:

  $storage = new StorageClient([
     'projectId' => 'my project id',
     'keyFile' => json_decode('{
       "web":{
          "client_id":"my client id",
          "project_id":"my project id",
          "auth_uri":"https://accounts.google.com/o/oauth2/auth",
          "token_uri":"https://oauth2.googleapis.com/token",
          "auth_provider_x509_cert_url":"my auth value",
          "client_secret":"my client secret"
       }
      }', true)
  ]);
  $bucket = $storage->bucket('mycustombucket');
  $bucket->upload(
           fopen('path/to/my/file.csv', 'r')
  );

Unfortunately, I got this error message: json key is missing the type field . I found this similar POST, but it doesn't work for me. I think it is also outdated.

The content of the JSON, I downloaded it from here: enter image description here

Would you please suggest me and idea, how to fix this ?

Thank you!

question from:https://stackoverflow.com/questions/65848175/google-cloud-storage-issue-with-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the end, I managed to get this work, by generating a key from the service account. From: enter image description here

Please not that once you generated you will automatically download it. A download button will not be available anymore. So make sure you have the key after generated it. I would expect to have a download button for the service account, like I have for OAuth 2.0 Client IDs (see my post). But I don't :(. Finally my code:

    $storage = new StorageClient([
        'projectId' => 'projectId' => 'my project id',
        'keyFile' => json_decode('{
              "type": "service_account",
              "project_id": "my project id",
              "private_key_id": "my key id",
              "private_key": "my key",
              "client_email": "[email protected]",
              "client_id": "client id",
              "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_x509_cert_url": "url here"
        }', true)
    ]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...