谷歌驱动器文件上传并分享给公众访问


Google drive file upload and share it to public access

我正在上传文件到谷歌驱动器帐户,我想做的是使其可读,但不可编辑的公众。我怎么能做到呢?

$file = new Google_Service_Drive_DriveFile();
$file->title = $_POST['name'];
$file->shared = "public"; //i need to do this
$file->folder = "/somefolder/"; // and this
(可选的)

这实际上是一个三合一的问题:我也想把那个文件放在驱动器上的一个特定文件夹,我想知道如何在谷歌驱动器上创建那个文件夹。

可能对新手有用:


<?php
$client = new Google_Client();
// SET Client ID, Client Secret, Tokens as per your mechanism
?>

步骤1:

  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