fnsc/laravel-google-drive 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fnsc/laravel-google-drive

最新稳定版本:1.3.0

Composer 安装命令:

composer require fnsc/laravel-google-drive

包简介

Filesystem adapter for Google Drive

README 文档

README

Tests Status

A Laravel package to upload, download, and delete files on Google Drive using a service account.

Requirements

  • PHP ^8.2
  • Laravel ^10.0

Installation

composer require fnsc/laravel-google-drive

Publish the config file:

php artisan vendor:publish --provider="LaravelGoogleDrive\ServiceProvider"

Setup

1. Create a Google Service Account

Go to Google Cloud Console → Credentials, create a Service Account, and download the generated JSON key file.

Add the file to your project (e.g. storage/app/service-account.json) and never commit it to git.

2. Share the Google Drive folder

Open the target folder in Google Drive, click Share, and add the client_email from the JSON file with Editor access.

3. Configure environment variables

Add the following to your .env file:

GOOGLE_APPLICATION_CREDENTIALS=storage/app/service-account.json
GOOGLE_DRIVE_FOLDER_ID=your_folder_id_here

GOOGLE_APPLICATION_CREDENTIALS is the path to the JSON key file relative to the project root. GOOGLE_DRIVE_FOLDER_ID is the ID found in the Google Drive folder URL: https://drive.google.com/drive/folders/<FOLDER_ID>.

Usage

Inject LaravelGoogleDrive\GoogleDrive into your controller or route closure.

Upload a file

use LaravelGoogleDrive\GoogleDrive;
use Illuminate\Http\Request;

Route::post('/upload', function (Request $request, GoogleDrive $drive) {
    $result = $drive->upload($request->file('file'));

    return [
        'file_id'   => $result->getFileId(),
        'file_name' => $result->getFileName(),
        'folder_id' => $result->getFolderId(),
    ];
});

To upload to a specific folder instead of the default one, pass the folder ID as the second argument:

$drive->upload($request->file('file'), 'your_folder_id');

Upload multiple files

$results = $drive->uploadMany($request->file('files'));

Download a file

use Illuminate\Http\Response;

Route::get('/download', function (GoogleDrive $drive) {
    $file = $drive->get('filename.pdf', 'google_drive_file_id');

    return new Response($file->getContent(), 200, [
        'Content-Type'        => $file->getMimeType(),
        'Content-Disposition' => 'attachment; filename=' . $file->getName(),
    ]);
});

Delete a file

$deleted = $drive->delete('google_drive_file_id'); // returns bool

License

This package is free software distributed under the terms of the MIT license.

统计信息

  • 总下载量: 219
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固