PHP / Google Cloud Storage, curl?

2019-07-25 07:39发布

I've seen a lot of posts somewhat surrounding this topic, but can't seem to find one that really answers my question. I have an AngularJS-based site with a PHP backend and am hoping to use an existing form that uploads a file, then re-route it to Google Cloud Storage in the backend. I've previously used curl to send from the app to a separate Python app. So the question is:

What's the best way / is it possible to transmit the file to my Google Cloud Storage bucket using curl (including auth method)? Also needing clarity on whether using an API_KEY is possible, or if OAuth or another Auth method is required.

My latest attempt, though I'm really not sure the approach itself is even right, but since people like seeing the sad attempts:

$authheaders = array("Authorization: Bearer " . $API_KEY);
$request = curl_init();
$url = "https://www.googleapis.com/upload/storage/v1/b/".$bucket."/o?uploadType=resumable&name=" . $_FILES['file']['name'];
curl_setopt($request, CURLOPT_POSTFIELDS, array('file' => curl_file_create($filepath)));
curl_setopt($request, CURLOPT_HTTPHEADER, $authheaders);
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($request, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($request, CURLOPT_SSLVERSION, 1);
curl_setopt($request, CURLOPT_VERBOSE, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HEADER, true);
$result = curl_exec($request);

0条回答
登录 后发表回答