2016-08-23 184 views
0

我想上傳一個文件到谷歌雲存儲使用curl php。谷歌雲存儲捲曲php上傳

define("BASE","https://www.googleapis.com/upload/storage/v1/b/"); 
$key='XXXXX'; 
$authheaders = array(
"Authorization: Bearer " .$key, 
"Content-Type: image/jpeg" 
); 

$bucket='gs://storage-8c79a.appspot.com'; 

$file='1234'; 
$fileName='1.pdf'; 
$url=BASE.$bucket.'/o?name=name_of_file.jpg&uploadType=multipart'; 
$curl = curl_init(); 

curl_setopt($curl, CURLOPT_POSTFIELDS, $file); 

curl_setopt($curl, CURLOPT_HTTPHEADER, $authheaders); 
curl_setopt($curl, CURLOPT_URL, $url); 

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($curl, CURLOPT_USERAGENT, "HTTP/1.1.5"); 

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
// 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP. 
curl_setopt($curl, CURLOPT_SSLVERSION, 1); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, true); 


$response = curl_exec($curl); 
echo $response; 

輸出:

HTTP/1.1 404未找到X-GUploader-UPLOADID:AEnB2UriDzXPtSs2Gac2BVfR4b4abfkroNy5gBCg8D85az3MK69YubPCCp_k3cx64A2MsSCMu-N_4F-RfFe10Vh_GjUbrjkrkzZ4NJ3Tlry4I8js-pgUqtw有所不同:產地不同而不同:X-來源的Content-Type:text/html的; charset = UTF-8內容長度:9日期:2016年8月23日星期二09:57:23 GMT服務器:UploadServer備用協議:443:quic Alt-Svc:quic =「:443」; MA = 2592000; v =「35,34,33,32,31,30」未找到

請幫我解決這個問題。

+0

解釋你的根本問題,更具體到這個。 – pedrouan

+0

我編輯了您的問題,使其更具可讀性。在這個網站上,鼓勵用戶編輯和重新編輯問題以儘可能地改進問題,所以請在[問]的指導下閱讀指導,然後回顧一下您的問題。 –

回答

0

此代碼:

define("BASE","https://www.googleapis.com/upload/storage/v1/b/"); 
$bucket='gs://someproject.appspot.com'; 
$url=BASE.$bucket.'/o?name=name_of_file.jpg; 

導致URL是這樣的:

https://www.googleapis.com/upload/storage/v1/b/gs://someproject.appspot.com/o?name=name_of_file.jppg 

URL的桶名稱的部分應該只是 「someproject.appspot.com」。刪除「gs://」。

+0

對於原始上傳,您還沒有正確使用'uploadType = multipart',而是'uploadType = media'。我注意到'Content-Type:image/jpeg'不同意未使用的'$ fileName ='1.pdf';'但在這兩種情況下,這看起來像只是上傳內容而不是內容和元數據。有關不同上傳選項的更多信息,請參見https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload。 –