【发布时间】:2015-12-08 03:45:30
【问题描述】:
过去几天我一直在四处寻找这个问题的答案,但一直找不到我想要的解决方案。
我有一个使用 Google_Client 执行此任务的完整版本,但是我希望能够在不使用 Google_Client 的情况下执行此操作。我可以在不使用它的情况下创建存储桶,但不能创建对象。我根本无法理解Google Documentation(这很糟糕)。
所以,我在下面有一个函数,它接受几个参数来try 并上传一个文件。我不确定$authheaders、$post_fields(帖子正文)或网址中需要输入什么内容。
public function upload_file($bucket, $fileName, $file, $fileType) {
// Check if we have auth token
if(empty($this->authtoken)) {
echo "Please login to Google";
exit;
}
// Prepare authorization headers
$authheaders = array(
"Authorization: Bearer " . $this->authtoken
);
$postbody = array();
//Http call for creating a file
$response = $this->http_call(self::FILE_UPLOAD_URL.$bucket.'/o?uploadType=multipart&name='.$fileName, $postbody, $authheaders);
// Has the file been created successfully?
if($response->success=="1") {
return array('status' => 'success', 'errorcode' =>'', 'errormessage'=>"", 'id' => $response->job->id);
}
else {
return $response;
}
}
http_call 函数:
public function http_call($url, $post_fields, $authheaders) {
// Make http call
$this->httpRequest->setUrl($url);
$this->httpRequest->setPostData(json_encode($post_fields));
$this->httpRequest->setHeaders($authheaders);
$this->httpRequest->send();
$response = json_decode($this->httpRequest->getResponse());
return $response;
}
对此的任何帮助将不胜感激。
【问题讨论】:
-
是的。但它使用 Google 客户端,我不想这样做。我已经有一个使用 Google 客户端的工作版本,但我现在正在尝试在没有它的情况下完成这项任务。
-
您查看过插入对象的原始 json api 文档吗? cloud.google.com/storage/docs/json_api/v1/objects/insert
-
我做过,但我不是很明白。不确定帖子正文或标题部分的内容,或者文件的输入位置。
标签: php google-cloud-storage json-api