使用Google Drive API将照片上传到免费存储空间

时间:2018-12-01 08:30:57

标签: php google-drive-api drive

我可以使用以下代码将文件上传到Google驱动器:

//Insert a file
$file = new Google_Service_Drive_DriveFile();
$file->setName(uniqid().'.jpg');
$file->setDescription('A test document');
$file->setMimeType('image/jpeg');

$data = file_get_contents('a.jpg');

$createdFile = $service->files->create($file, array(
      'data' => $data,
      'mimeType' => 'image/jpeg',
      'uploadType' => 'multipart'
    ));

但是它将计入配额中。如何将其发送到Google图片等免费存储空间?

1 个答案:

答案 0 :(得分:0)

如果要使用Google相册,则应使用Google Photos APIs upload media

  

注意:通过API上传到Google相册的所有媒体项目均以完整分辨率存储在original quality。如果您每位用户上传的文件超过25MB,则您的应用程序应提醒用户,这些上传将计入其Google帐户中的存储空间。

使用上传请求将字节上传到Google。如果上传请求成功,则返回原始文本字符串形式的上传令牌。要创建媒体项目,请在batchCreate调用中使用这些标记。

// Create a new upload request

// Specify the filename that will be shown to the user in Google Photos
// and the path to the file that will be uploaded

// When specifying a filename, include the file extension
try {
    $uploadToken = $photosLibraryClient->upload(file_get_contents($localFilePath), $filename);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // Handle error
}