0

I got some problem while managing migration from PHP5 to PHP7.

Actually, what I was doing with php5, to get an URL where upload a file, was something like this:

$options = [ 'gs_bucket_name' => 'mybucket.appspot.com' ];
$myvar = $_GET['myvar'];

$upload_url = CloudStorageTools::createUploadUrl($myvar, $options);

return $upload_url;

I've uploaded the library and now I'm trying to use

Google\Cloud\Storage\StorageClient

I've converted the code above to something like this:

$bucketName = 'mybucket.appspot.com';
$objectName =  $_GET['myvar'];

$storage = new StorageClient([
        'scopes' => [
        'https://www.googleapis.com/auth/iam',
        'https://www.googleapis.com/auth/devstorage.full_control',
        ]
    ]);
    
 $bucket = $storage->bucket($bucketName);
 $object = $bucket->object($objectName);
 $upload_url = $object->signedUrl(
            new \DateTime('90 min'),
            [
                    'version' => 'v4',
            ]
    );

 return $upload_url;

I got a 403 FORBIDDEN error

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/myproject@appspot.gserviceaccount.com:signBlob?alt=json` resulted in a `403 Forbidden` response:
{
  "error": {
    "code": 403,
    "message": "IAM Service Account Credentials API has not been used in project 123456 (truncated...) 
....

Some suggestions? In the PHP5 version upload is working (with the older code), so I suppose my app angine service account has the correct permissions setted.

Thanks

tidpe
  • 325
  • 3
  • 15
  • Why did you truncate the `message` in your question? The solution is in the message. Go to the link in the message and enable the API. Or use the CLI: `gcloud services enable iamcredentials.googleapis.com`. – John Hanley May 20 '21 at 17:10
  • I didn't truncated, is the original message. (next lines are only about calls to specific line of specific file from library...). API are enabled. – tidpe May 21 '21 at 07:24

1 Answers1

0

Solved by passing .json file for authentication.

$storage = new StorageClient([
    'projectId' => "my_project",
    'keyFilePath' => 'key/serviceaccount.json', 
]);
tidpe
  • 325
  • 3
  • 15