Reputation: 3162
I am new to GCP. I have a function which outputs a CSV file to the GCP Cloud Storage. I am trying to access the file using PHP.
What I have done so far:
I have created a service account using GCP IAM and have given it access as Storage Object Viewer.
I have also obtained the json key from the IAM.
What commands would I need to use in my PHP script (hosted on a different web server) to retrieve the file using CURL and how do I use the json authentication key?
I apologize in advance if this is in the documentation somewhere, I found it very convoluted and over whelming. Any advice or direction is appreciated.
Update:
Based on the comments below here is a link to the google-cloud-php github which I found. I am not sure if this is the best resource to begin.
Upvotes: 0
Views: 1279
Reputation: 1
First of all you have to get an accesstoken for Authentification. For this you need the json authentification key.
This page was a great help for me: https://www.it-swarm.dev/de/curl/wie-kann-man-mit-curl-eine-verbindung-zum-google-drive-api-herstellen/806069468/
Maybe this PHP-code helps you a little bit:
function get_Google_accesstoken($scope,$credfile,$proxy,$timetoexpiration){
#Developers Info at developers.google.com/identity/protocols/oauth2/service-account
$GoogleApiKeyInfo=GoogleApiKeyInfo($credfile);
$Header=array();
$Header["alg"]="RS256";
$Header["typ"]="JWT";
$ClaimSet["iss"]=$GoogleApiKeyInfo["client_email"];
$ClaimSet["scope"]=$scope;
$ClaimSet["aud"]=$GoogleApiKeyInfo["token_uri"];
$ClaimSet["iat"]=time();
$ClaimSet["exp"]=$ClaimSet["iat"]+($timetoexpiration);
$Jws=base64_encode(json_encode($Header)).".".base64_encode(json_encode($ClaimSet));
$OpenSslRslts=openssl_sign($Jws,$Signature,$GoogleApiKeyInfo["private_key"],OPENSSL_ALGO_SHA256);
$Jwt=$Jws.".".base64_encode($Signature);
$SendVars=array();
$SendVars["grant_type"]=("urn:ietf:params:oauth:grant-type:jwt-bearer");
$SendVars["assertion"]=$Jwt;
$SendVars=http_build_query($SendVars);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $GoogleApiKeyInfo["token_uri"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $SendVars);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)){
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response=json_decode($response);
return $response;
}
you find hints for $scope at developers.google.com/identity/protocols/oauth2/scopes $proxy is only used if you need a proxy and $timetoexpiration has no influence, because your accesstoken is always valid for 60 minutes
Upvotes: 0
Reputation: 39
You can make use of the Cloud Storage library for php, more specifically, how to download objects.
Upvotes: 0