I have a bit of a strange problem. I'm trying to use Google's Storage Client to upload an image to a Google Cloud Storage bucket. But whenever I attempt to upload a picture, it gives me this error:
Parse error: syntax error, unexpected 'static' (T_STATIC) in /var/www/html/vendor/psr/cache/src/CacheItemInterface.php on line 75
Here's my code:
$privateKeyFileContent = '{--Key is here--}'
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
function uploadFile($bucketName, $fileContent, $cloudPath) {
$privateKeyFileContent = $GLOBALS['privateKeyFileContent'];
// connect to Google Cloud Storage using private key as authentication
try {
$storage = new StorageClient([
'keyFile' => json_decode($privateKeyFileContent, true)
]);
} catch (Exception $e) {
// maybe invalid private key ?
print $e;
return false;
}
// set which bucket to work in
$bucket = $storage->bucket($bucketName);
// upload/replace file
$storageObject = $bucket->upload(
$fileContent,
['name' => $cloudPath]
// if $cloudPath is existed then will be overwrite without confirmation
// NOTE:
// a. do not put prefix '/', '/' is a separate folder name !!
// b. private key MUST have 'storage.objects.delete' permission if want to replace file !
);
// Successful upload..?
return $storageObject != null;
}
if(isset($_FILES['file']['tmp_name'])) {
echo uploadFile("mybucket", file_get_contents($_FILES["file"]["tmp_name"]), "testing/" . $_FILES["file"]["name"]));
}
I'm a bit new to using the Google Cloud Storage API-- apologies if this is a silly mistake! I've been searching hours for a solution, but I haven't found any. Any help would be appreciated!