通过Google Drive API上传文件时指定文件夹名称 [英] Specify folder name while upload file via Google Drive API

查看:212
本文介绍了通过Google Drive API上传文件时指定文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码段:

    $service=new Google_Service_Drive($client);
    $file=new Google_Service_Drive_DriveFile();
    $file->setTitle($title);
    $file->setDescription('some desciption');
    $file->setMimeType($mimeType);
    $createdFile=$service->files->insert($file,[
        'data'          => $data,
        'mimeType'      => $mimeType,
        'uploadType'    => 'media'
        ]);

这会将文件上传到Google驱动器的根目录.

This uploads the file to the root of the google drive.

我想将上传位置更改为另一个文件夹,如何实现?

I want to change the upload location to another folder, how can I achieve this?

推荐答案

没关系,我自己发现了它 似乎您需要实例化父参考服务并将其传递给DriveFIle的parent方法.

never mind i found it myself it seems that you need to instantiate a parent reference service and pass it to the parents method of your DriveFIle

$parent = new Google_Service_Drive_ParentReference(); 
$parent->setId('folderid');
$file->setParents(array($parent));

这篇关于通过Google Drive API上传文件时指定文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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