从google storage上传或者下载文件
向Google storage上传文件
// Bucketを取得 private static function getStorageBucket() { if (!isset(self::$bucket)) { putenv(\Config::get('gcs.access_key')); $storage = new StorageClient(); self::$bucket = $storage->bucket(\Config::get('gcs.bucket')); } } //ファイルをstorageにアップロード private static function uploadToStorage($fileName) { self::getStorageBucket(); // Bucket確認 if (self::$bucket->exists()) { $fromPath = self::$current_jobno_path.DS.$fileName; if (file_exists($fromPath)) { $targePpath = "document_search".DS.self::$current_jobno.DS.$fileName; $options = [ 'name' => $targePpath ]; $handle = fopen($fromPath, 'r'); $object = self::$bucket->upload( $handle, $options ); } else { \Log::error("ファイル(".$fromPath.")が不存在です。"); } } else { \Log::error('GCS BUCKET NOT EXIST.'); } }
从Google storage下载文件
public function download($from_file_path, $to_file_path) : String { $url = ''; // manualファイル取得 $object = $this->bucket->object($from_file_path); // 署名付きURL取得 // if ($object->exists()) { $stream = $object->downloadToFile($to_file_path); // } return $stream; }
获取Google storage中文件URL
public function getUrl($file_path) : String { $url = ''; // manualファイル取得 $object = $this->bucket->object($file_path); // 署名付きURL取得 // if ($object->exists()) { $url = $object->signedUrl(new \DateTime('+60 minute')); // } return $url; }