我想使用 google drive API curl 将文件上传到特定文件夹 [英] I want to upload file to specific folder with google drive API curl

查看:166
本文介绍了我想使用 google drive API curl 将文件上传到特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用 curl API 将文件上传到谷歌驱动器.但我想将文件上传到特定文件夹.我还尝试在 API url 中使用文件夹 id,例如:

尝试使用 &addParents=[folderId]

示例:

https://www.googleapis.com/upload/drive/v3/files?uploadType=media&addParents=1bpHmln41UI-CRe5idKvxrkIpGKh57T32

如果您最初在根目录中为文件创建了元数据,然后尝试将文件上传到其他目录,我怀疑上传会创建新的元数据.这两个调用需要做同样的事情.您的创建应该设置父项.

帖子正文

$data = array("name" => "test.jpg", "parents" => array("123456"));$data_string = json_encode($data);curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

I have uploaded file to google drive with curl API. But I want to upload file to specific folder. I have also tried with folder id in API url like: https://www.googleapis.com/upload/drive/v3/files/folder_id?uploadType=media

add_action('wpcf7_before_send_mail', 'save_application_form');

function save_application_form($WPCF7_ContactForm) {

    $wpcf7 = WPCF7_ContactForm :: get_current();
    $submission = WPCF7_Submission :: get_instance();
    if ($WPCF7_ContactForm->id == 8578) {
        if ($submission) {
            $submited = array();
            //$submited['title'] = $wpcf7->title();
            $submited['posted_data'] = $submission->get_posted_data();
            $uploaded_files = $submission->uploaded_files();
            $name = $submited['posted_data']["Name"];
            $position_test = $submited['posted_data']["Position"];
            $email = $submited['posted_data']["Email"];
            $phone = $submited['posted_data']["phone"];
            $message = $submited['posted_data']["Message"];
            $position = $submited['posted_data']["AttachYourCV"];

           // $test2 = $_FILES['AttachYourCV']['name'];

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $cf7_file_field_name = 'AttachYourCV';

            $image_location = $uploaded_files[$cf7_file_field_name];
            $mime_type = finfo_file($finfo, $image_location);
            $token = GetRefreshedAccessToken('client_id', 'refresh_token', 'client_secret');
            $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media',
                CURLOPT_HTTPHEADER => array(
                    'Content-Type:' . $mime_type, // todo: runtime detection?
                    'Authorization: Bearer ' . $token
                ),
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => file_get_contents($image_location),
                CURLOPT_RETURNTRANSFER => 1
            ));
            $response = curl_exec($ch);
            $id = json_decode($response, TRUE);
            $get_id = $id['id'];

    curl_close($ch);
   if (isset($id['id'])) {
            $get_id = $id['id'];
            $post_fields = array();
            $folder_id = '1-9r8oVsAfV_iJmYh1cZYPMvE9Qhv8RLA';
            // remove extension  
            $this_file_name = explode('.', $position);

            // submit name field  
            $post_fields['name'] .= $this_file_name[0];
            $post_fields['parents'] = $folder_id[1];
            $ch2 = curl_init();
            curl_setopt_array($ch2, array(
                CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/' . $get_id,
                CURLOPT_HTTPHEADER => array(
                    'Content-Type:application/json', // todo: runtime detection?
                    'Authorization: Bearer ' . $token
                ),
                CURLOPT_POST => 1,
                CURLOPT_CUSTOMREQUEST => 'PATCH',
                CURLOPT_POSTFIELDS => json_encode($post_fields),
                CURLOPT_RETURNTRANSFER => 1
            ));
            $response = curl_exec($ch2);
            if ($response === false) {
                $output = 'ERROR: ' . curl_error($ch2);
            } else {
                $output = $response;
            }

            // close second request handler  
            curl_close($ch2);


        }
}

I have added another curl call but still didn't get file in folder. My file untitled issue solved with this way.

解决方案

Uploading files to google drive is a two part thing. (note it can be done as a single call but if you want to add the corect data its best to do it as two)

create Adds the initial meta data for the file. Name, media type, and location that being its parent directory.

Second the actual upload of the file itself.

Note: a file uploaded without first sending the metadata will create a dummy metadata most often with a title of the file being "unknown."

Create

When you do your create of the inertial metadata. You need to add the parents in this inital call the field is called parents[] you need to add the file id there. I cant help you do this as your not adding the code for it.

Upload

By default the file is uploaded into the root folder unless you add a parent folder id for the file.

If you check the documentation you will find the optional query parms

Try using &addParents=[folderId]

Example:

https://www.googleapis.com/upload/drive/v3/files?uploadType=media&addParents=1bpHmln41UI-CRe5idKvxrkIpGKh57T32

If you originally created the metadata for the file in the root directory and then try to upload the file to a different directory i suspect that the upload will create new metadata. These two calls need to be done the same. Your create should have parents set.

post body

$data = array("name" => "test.jpg", "parents" => array("123456"));                                                                    
$data_string = json_encode($data);                                                                                   

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 

这篇关于我想使用 google drive API curl 将文件上传到特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆