谷歌云存储卷曲php上传

时间:2016-08-23 10:00:05

标签: php curl google-cloud-storage

我正在尝试使用Curl php将文件上传到Google云端存储。

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 Not Found X-GUploader-UploadID:AEnB2UriDzXPtSs2Gac2BVfR4b4abfkroNy5gBCg8D85az3MK69YubPCCp_k3cx64A2MsSCMu-N_4F-RfFe10Vh_GjUbrjkrkzZ4NJ3Tlry4I8js-pgUqtw Vary:Origin Vary:X-Origin Content-Type:text / html; charset = UTF-8 Content-Length:9 Date:2016年8月23日星期二09:57:23 GMT服务器:UploadServer Alternate-Protocol:443:quic Alt-Svc:quic =“:443”; MA = 2592000; v =“35,34,33,32,31,30”未找到

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

此代码:

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

会产生如下网址:

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

URL的存储桶名称部分应该只是" someproject.appspot.com"。删除" gs://"。