【问题标题】:Google drive file upload and share it to public access谷歌驱动文件上传并分享给公众访问
【发布时间】:2014-06-29 04:56:48
【问题描述】:
我正在将文件上传到谷歌驱动器帐户,我想做的是使其可读,但不能对公众进行编辑。我该怎么做?
$file = new Google_Service_Drive_DriveFile();
$file->title = $_POST['name'];
$file->shared = "public"; //i need to do this
$file->folder = "/somefolder/"; // and this
[可选]
这实际上是三合一问题:我还想将该文件放入驱动器上的特定文件夹中,并且我想知道如何在谷歌驱动器上创建该文件夹。
【问题讨论】:
标签:
php
file-upload
google-drive-api
public
google-api-php-client
【解决方案1】:
可能对新手有用:
<?php
$client = new Google_Client();
// SET Client ID, Client Secret, Tokens as per your mechanism
?>
第 1 步:
- 创建文件夹。 API 将返回所创建文件夹的 ID。
第 2 步:
将文件上传到setParents([]);文件夹中
例子:
<?php
// Upload the file to efgh folder which is inside abcd folder which is in the root folder
$file = new Google_Service_Drive_DriveFile();
$file->setParents(["abcd123", "efgh456"]); // abcd123 and efgh456 are IDs returned by API for the respective folders
$service = new Google_Service_Drive($client);
$service->files->create($file);
?>
$fileID = ""; // file ID returned by Drive.
第 3 步:
将权限设置为可共享:
<?php
$permissionService = new Google_Service_Drive_Permission();
$permissionService->role = "reader";
$permissionService->type = "anyone"; // anyone with the link can view the file
$service->permissions->create($fileID, $permissionService);
?>
参考:https://developers.google.com/drive/api/v3/reference/permissions/create