繁体   English   中英

Google云端存储-无需使用Google客户端即可上传“对象”

[英]Google Cloud Storage - Upload an 'Object' without using Google Client

在过去的几天里,我一直在寻找这个问题的答案,但是我一直无法找到想要的解决方案。

我有使用Google_Client进行此任务的完整版本,但是我希望能够在不使用Google_Client的情况下进行此操作。 我可以创建存储桶而不使用它,但是不能创建对象。 我根本无法理解Google文档 (非常差)。

因此,我下面有一个函数,该函数需要几个参数来尝试上载文件。 我不确定$authheaders$post_fields (帖子的正文)或url中需要包含什么内容。

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;

}

任何帮助,将不胜感激。

如果其他人希望不使用API​​来执行此操作,这就是我如何使用curl解决问题。

// Prepare authorization headers
        $authheaders = array(
            "Authorization: Bearer " . $this->authtoken,
            "Content-Type: application/pdf"
        );

        $url = self::FILE_UPLOAD_URL.$bucket.'/o/?uploadType=multipart&name='.$fileName.'';

        $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, "Goole Cloud Storage PHP Starter Application google-api-php-client/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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM