aslamhus/google-drive-uploader

一个用于将文件上传到Google Drive的PHP类,提供基本和可恢复上传的方法,支持管理依赖项和设置Google Drive API服务帐户

v1.1.0 2024-03-26 19:17 UTC

This package is auto-updated.

Last update: 2024-09-26 20:43:12 UTC


README

Google Drive Uploader是一个PHP类,简化了使用Google Drive API上传文件的过程。它支持基本和可恢复上传,允许您通过异步编程选项以块的形式高效地上传大文件。

Google Drive API文档

此类依赖于Google API PHP客户端库,并需要一个启用了Google Drive API的Google服务帐户。请参考以下资源以获取更多信息和管理说明

设置Google Drive API服务帐户

要使用Google Drive Uploader,您需要设置Google Cloud Console服务帐户并获取必要的凭证。请按照以下步骤操作

  1. 创建Google Cloud项目:Google Cloud Console

  2. 启用Google Drive API:Google Workspace产品库

  3. 为您的应用设置OAuth:转到API和服务 > OAuth同意屏幕。

  4. 选择作用域:选择Google Drive API所需的范围。请参阅可用范围此处

  5. 创建访问凭证(API密钥):转到API和服务 > 凭证,然后点击“创建凭证” > “API密钥”。

  6. 创建访问凭证(OAuth客户端ID):转到API和服务 > 凭证,然后点击“创建凭证” > “OAuth客户端ID”。添加授权URL并下载客户端密钥JSON文件。

  7. 下载服务帐户密钥:转到服务帐户 > 密钥 > 创建密钥。

  8. 创建服务帐户:转到IAM和行政 > 服务帐户,然后点击“创建服务帐户”。

  9. 将服务帐户添加到Google Drive文件夹中,通过授予服务帐户电子邮件地址权限来实现。

现在,您可以使用Google Drive Uploader了!

使用方法

安装

composer require aslamhus/google-drive-uploader

配置

在您开始上传之前,您必须设置GOOGLE_APPLICATION_CREDENTIALS环境变量,并创建一个具有对您的服务帐户电子邮件地址授予权限的共享权限的drive文件夹。如果尚未在Google Cloud Console中创建服务帐户,请确保您已遵循上述步骤。

  1. 服务帐户凭证

GoogleDriveUploader通过以下方式通过环境变量设置默认凭证

putenv('GOOGLE_APPLICATION_CREDENTIALS=<path-to-service-account-credentials-json-file>');
  1. 驱动器文件夹ID

为了将文件上传到Google Drive帐户,在您希望上传文件的目标Google Drive帐户中创建一个文件夹。然后授予您的服务帐户电子邮件地址对文件夹的访问权限。您可以在Google Cloud Console中找到您的服务帐户电子邮件地址。

完成此操作后,只需从驱动器文件夹URL复制驱动器文件夹ID。您可以通过在Google Drive中转到相应的文件夹并从URL中复制ID字符串来找到驱动器文件夹URL。

https://drive.google.com/drive/folders/file-id-string

基本上传

用于小文件。

use Aslamhus\GoogleDrive\GoogleDriveUploader;
$uploader = new GoogleDriveUploader($driveFolderId);
$file = $uploader->uploadBasic($filePath, $fileName, $mimeType);

可恢复上传

用于大文件上传和/或上传过程有很大可能被中断的情况。

$uploader = new GoogleDriveUploader($driveFolderId);
$resumeUri = $uploader->initResumable($fileName, $mimeType);
$uploader->startResumable($filePath);

异步可恢复上传

要执行异步上传,请使用可恢复上传方法。

$uploader = new GoogleDriveUploader($driveFolderId);
$resumeUri = $uploader->initResumable($fileName, $mimeType);
// the second argument to upload resumable is the optional onChunkRead callback
$asyncTask = $uploader->startResumable($filePath, null, true);

foreach ($asyncTask as $response) {
    // perform other logic
    // you can abort the upload at any time by calling $uploader->abort()
    // $response will return false until the upload is complete
}

// Once the upload is complete, $response will contain a Google Drive File object
$fileId = $response['id'];

恢复中断的上传

$uploader = new GoogleDriveUploader($driveFolderId);
// init and store the resumable uri
$resumeUri = $uploader->initResumable($fileName, $mimeType);
$uploader->startResumable($filePath);
// If the upload is interrupted, resume it with the resume() method passing the resumeUri argument
$uploader->resume($resumeUri);

回调函数

onChunkRead

您可以将可选的 onChunkRead 回调函数传递给 startResumableresume 方法。

/**
 * On chunk read
 *
 * @param string $chunk - the chunk byte string
 * @param int $progress - the progress percentage, e.g. 10.00
 * @param int $fileSize - the file size in bytes
 * @param arary $byteRange - the range of bytes read [from, to], i.e. [0, 256000]
 **/
$onChunkRead = function($chunk,  $progress, $fileSize, $byteRange) {
    // do something like outputting the progress
    echo $progress . "%";
}
$uploader->startResumable($filePath, $onChunkRead);

内存限制

basicUpload 方法使用 file_get_contents 将文件加载到内存中,因此最好不要使用大文件。如果使用,可能会遇到内存限制错误。

可恢复上传通过将文件分割成默认大小为 262144 字节 的块来执行。这是谷歌推荐的块大小。您应该能够在不遇到任何内存限制的情况下上传非常大的文件。

异常处理

该类包含一个自定义异常类,GoogleDriveUploaderException,用于处理Google Drive上传过程中的错误。

测试

在测试之前,请确保您已安装开发依赖项,并设置好您的 .env 文件,包括 GOOGLE_APPLICATION_CREDENTIALSGOOGLE_DRIVE_FOLDER_ID

注意:运行测试会将 3 个小测试视频上传到您指定的 Google Drive 文件夹中

然后运行

composer run-script test

请随时在 GitHub 上贡献或报告问题。

上传愉快!