安装composer拓展 composer require "superbalist/laravel-google-cloud-storage" /www/sir486 # composer require superbalist/laravel-google-cloud-storage Info from https://repo.packagist.org: #StandWithUkraine Using version ^2.2 for superbalist/laravel-google-cloud-storage ./composer.json has been updated Running composer update superbalist/laravel-google-cloud-storage Loading composer repositories with package information Info from https://repo.packagist.org: #StandWithUkraine Updating dependencies Nothing to modify in lock file Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 43 installs, 0 updates, 0 removals - Downloading scrivo/highlight.php (v9.18.1.9) - Downloading filp/whoops (2.14.5) - Downloading facade/ignition-contracts (1.0.2) - Downloading facade/flare-client-php (1.9.1) - Downloading facade/ignition (1.18.1) - Downloading fakerphp/faker (v1.19.0) - Downloading hamcrest/hamcrest-php (v2.0.1) - Downloading mockery/mockery (1.5.0) - Downloading php-parallel-lint/php-console-color (v0.3) - Downloading php-parallel-lint/php-console-highlighter (v0.5) - Downloading nunomaduro/collision (v3.2.0) - Downloading webmozart/assert (1.10.0) - Downloading phpdocumentor/reflection-common (2.2.0) - Downloading phpdocumentor/type-resolver (1.6.0) - Downloading phpdocumentor/reflection-docblock (5.3.0) - Downloading sebastian/version (3.0.2) - Downloading sebastian/resource-operations (3.0.3) - Downloading sebastian/recursion-context (4.0.4) - Downloading sebastian/object-reflector (2.0.4) - Downloading sebastian/object-enumerator (4.0.4) - Downloading sebastian/global-state (5.0.5) - Downloading sebastian/exporter (4.0.4) - Downloading sebastian/environment (5.1.3) - Downloading sebastian/diff (4.0.4) - Downloading sebastian/comparator (4.0.6) - Downloading sebastian/code-unit (1.0.8) - Downloading sebastian/cli-parser (1.0.1) - Downloading phpunit/php-timer (5.0.3) - Downloading phpunit/php-text-template (2.0.4) - Downloading phpunit/php-invoker (3.1.1) - Downloading phpunit/php-file-iterator (3.0.6) - Downloading theseer/tokenizer (1.2.1) - Downloading sebastian/lines-of-code (1.0.3) - Downloading sebastian/complexity (2.0.2) - Downloading sebastian/code-unit-reverse-lookup (2.0.3) - Downloading phpunit/php-code-coverage (9.2.15) - Downloading doctrine/instantiator (1.4.1) - Downloading phpspec/prophecy (v1.15.0) - Downloading phar-io/version (3.2.1) - Downloading phar-io/manifest (2.0.3) - Downloading myclabs/deep-copy (1.11.0) - Installing scrivo/highlight.php (v9.18.1.9): Extracting archive - Installing filp/whoops (2.14.5): Extracting archive - Installing facade/ignition-contracts (1.0.2): Extracting archive - Installing facade/flare-client-php (1.9.1): Extracting archive - Installing facade/ignition (1.18.1): Extracting archive - Installing fakerphp/faker (v1.19.0): Extracting archive - Installing hamcrest/hamcrest-php (v2.0.1): Extracting archive - Installing mockery/mockery (1.5.0): Extracting archive - Installing php-parallel-lint/php-console-color (v0.3): Extracting archive - Installing php-parallel-lint/php-console-highlighter (v0.5): Extracting archive - Installing nunomaduro/collision (v3.2.0): Extracting archive - Installing webmozart/assert (1.10.0): Extracting archive - Installing phpdocumentor/reflection-common (2.2.0): Extracting archive - Installing phpdocumentor/type-resolver (1.6.0): Extracting archive - Installing phpdocumentor/reflection-docblock (5.3.0): Extracting archive - Installing sebastian/version (3.0.2): Extracting archive - Installing sebastian/type (2.3.4): Extracting archive - Installing sebastian/resource-operations (3.0.3): Extracting archive - Installing sebastian/recursion-context (4.0.4): Extracting archive - Installing sebastian/object-reflector (2.0.4): Extracting archive - Installing sebastian/object-enumerator (4.0.4): Extracting archive - Installing sebastian/global-state (5.0.5): Extracting archive - Installing sebastian/exporter (4.0.4): Extracting archive - Installing sebastian/environment (5.1.3): Extracting archive - Installing sebastian/diff (4.0.4): Extracting archive - Installing sebastian/comparator (4.0.6): Extracting archive - Installing sebastian/code-unit (1.0.8): Extracting archive - Installing sebastian/cli-parser (1.0.1): Extracting archive - Installing phpunit/php-timer (5.0.3): Extracting archive - Installing phpunit/php-text-template (2.0.4): Extracting archive - Installing phpunit/php-invoker (3.1.1): Extracting archive - Installing phpunit/php-file-iterator (3.0.6): Extracting archive - Installing theseer/tokenizer (1.2.1): Extracting archive - Installing sebastian/lines-of-code (1.0.3): Extracting archive - Installing sebastian/complexity (2.0.2): Extracting archive - Installing sebastian/code-unit-reverse-lookup (2.0.3): Extracting archive - Installing phpunit/php-code-coverage (9.2.15): Extracting archive - Installing doctrine/instantiator (1.4.1): Extracting archive - Installing phpspec/prophecy (v1.15.0): Extracting archive - Installing phar-io/version (3.2.1): Extracting archive - Installing phar-io/manifest (2.0.3): Extracting archive - Installing myclabs/deep-copy (1.11.0): Extracting archive - Installing phpunit/phpunit (9.5.18): Extracting archive 配置存储驱动 config>filesysystem.php 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), ], 'gcs' => [ 'driver' => 'gcs', 'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'heartwarm'), 'key_file' => [ 'type' => 'service_account', 'project_id' => 'project_id', 'private_key_id' => '0f501aa6370517df7a74f34b10488a7da2e20d59', 'private_key' => '-----BEGIN PRIVATE KEY----- MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeO/rggF0Zvyho kptD4kiunDGao7an2m0WKi3Dt5341Cu6EqiFENJqnYQ+aZGXC5DCc+Iq34 WEjXVERMAI8H0LeXFlMeCCsJmMlOdj7uP4kN9H+plHUe7pPC5etABYb1FXhHnDBQ PIUES8zv/zDp/AoMnoyArRh9r0GYgxEZ46gvJ5BxNIe+nAdrimXbttc/ZqLl2HaG Y53Y4ieRzBNO5Pxb2/VG5FKsQURH192FWCstpet771qWtOEISSqAJR1cdPiVUt7h go1SiK42ygyb1aiu4fC/xvtB1R8vRHgJT1yZRSOJJToAug+fdwo4nu6L58N+I7rd AMHXGqD9AgMBAAECggEAKjydb06kS0qUyTEVA85Dk2Q682muyGhMBQYWOC9rme9c iqsPU5GVcQMBuT0psRVTuyv+m24XK8M8SI4TR3MFVfoyM6Z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Fv0Zz7o48cioEfXl4LtBxJ6oTeo bSm3h5TnKrlwE9/Coe8X6Po= -----END PRIVATE KEY----- ', 'client_email' => 'compute@developer.gserviceaccount.com', 'client_id' => '1136294240878320', 'auth_uri' => 'https://accounts.google.com/o/oauth2/auth', 'token_uri' => 'https://oauth2.googleapis.com/token', 'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs', 'client_x509_cert_url' => 'https://www.googleapis.com/robot/v1/metadata/x509/866710459170-compute%40developer.gserviceaccount.com', ], 'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'mr486'), 'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket 'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below 'visibility' => 'public', // optional: public|private ], ], 配置如上:配置内容来源于谷歌服务账号创建 使用方式: public function uploadFile(Request $request) { $params = $request->all(); $validator = Validator::make($params, [ 'file' => 'required', "type" => 'required' ]); if ($validator->fails()) { return Api::paramsError($validator->errors()->toArray()); } $ext = $request->file('file')->getClientOriginalExtension(); if(!in_array($ext,['jpeg','jpg','png'])){ return Api::fail("文件類型錯誤"); } $disk = IlluminateSupportFacadesStorage::disk('gcs'); $pathName = $params['type'].'/'.date("YmdH")."/".uniqid().'--'.$request->file('file')->getClientOriginalName(); $res = $disk->put($pathName, file_get_contents($request->file('file')->getRealPath())); if(!$res){ return Api::fail("上传失败"); } $url = $disk->url($pathName); return Api::success($url); }