我正在使用后台运行的php代码将一个文件从Google存储桶上传到Google文件夹。我的问题是当文件的大小增加时,Google (或GAE)会给出一个500错误。
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.我使用下面的依赖项。
“nao/flysystem-google-驱动器”:"^1.1",
下面是给出此错误的代码部分。对于小型文件来说,这是非常有效的:
$fileN = $this->clean($att->ATT_TITLE);
$fileData = file_get_contents("https://storage.googleapis.com/xxxxxx/".$att->ATT_FILE);
$dir = sys_get_temp_dir();
$tmp = tempnam($dir, $fileN);
file_put_contents($tmp, $fileData);
Storage::cloud()->putFileAs( $folderPath, new File($tmp),$fileN);我已经在.env文件中设置了配置
FILESYSTEM_CLOUD=google
GOOGLE_DRIVE_CLIENT_ID=xxxxapps.googleusercontent.com
GOOGLE_DRIVE_CLIENT_SECRET=xxxxx
GOOGLE_DRIVE_REFRESH_TOKEN=xxxxxx
GOOGLE_DRIVE_FOLDER_ID=xxxx
#GOOGLE_DRIVE_TEAM_DRIVE_ID=xxx我已经在filesystem.php中设置了如下所示的磁盘
'google' => [
'driver' => 'google',
'clientId' => env('GOOGLE_DRIVE_CLIENT_ID'),
'clientSecret' => env('GOOGLE_DRIVE_CLIENT_SECRET'),
'refreshToken' => env('GOOGLE_DRIVE_REFRESH_TOKEN'),
'folderId' => env('GOOGLE_DRIVE_FOLDER_ID'),
// 'teamDriveId' => env('GOOGLE_DRIVE_TEAM_DRIVE_ID'),
],我已经将php.ini文件设置如下
upload_max_filesize = 300M
post_max_size = 300M
memory_limit = 3000MApp.yaml
runtime: php72
handlers:
- url: /assets
static_dir: public/assets
env_variables:
APP_KEY: xxxxx
APP_STORAGE: /tmp
CACHE_DRIVER: file
VIEW_COMPILED_PATH: /tmp
SESSION_DRIVER: database
DB_DATABASE: xxx
DB_USERNAME: xx
DB_PASSWORD: xxxx
DB_SOCKET: "/cloudsql/xxxxx"
runtime_config:
document_root: public有人能给我一个提示或者给我另一种方法来做这件事吗?我的要求基本上是通过Google的后台工作将一个大文件(比如250 My )上传到Google。
发布于 2020-09-01 07:19:54
在这种情况下,分组上传解决了这个问题。
参考Google https://github.com/googleapis/google-api-php-client/blob/master/examples/large-file-upload.php中的以下示例
工作守则样本:
$fileData = file_get_contents("https://storage.googleapis.com/xxxx/".$att->ATT_FILE);
$dir = sys_get_temp_dir();
$tmp = tempnam($dir, $fileN);
file_put_contents($tmp, $fileData);
$fileToUpload = new File($tmp);
$filename = $fileN;
$mimeType = mime_content_type($tmp);
$driveService = Storage::cloud();
$client = new \Google_Client();
$client->setClientId('xxxx');
$client->setClientSecret('xxx');
$client->refreshToken('xxx');
$client->setApplicationName('xxx');
$service = new \Google_Service_Drive($client);
// Test 1
$file = new \Google_Service_Drive_DriveFile();
$file->title = $fileN;
$file->name = $fileN;
$file->parents = array($basePath);
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new \Google_Http_MediaFileUpload(
$client,
$request,
$mimeType,
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($tmp));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen($tmp, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if($status != false) {
$result = $status;
}
fclose($handle);发布于 2020-09-01 06:17:48
由于您正在调用Cloud来上传文件,这些文件很大,可以在60秒内完成上传,这是的默认超时。您可以自行修改API超时。在这里,您可以参考stackoverflow case来设置您自己的API超时。
https://stackoverflow.com/questions/63674913
复制相似问题