我正在分派作业以将我的视频文件排队,这些文件存储在s3上。一切正常,除非我上载一个20mb的视频文件,当我在存储桶中查看该文件为120b时,一切正常。因此,这使我认为我将路径和文件名作为字符串而不是文件对象上传。出于某种原因,当我尝试使用Storage::get()
or或File::get()
dd获取文件时,结果显示了一堆或随机且疯狂的字符。看来我只能获得这些奇怪的字符或字符串,由于某种原因我无法获得文件对象。
在控制器中,我还将其存储在公用磁盘中(稍后,我将在Jobs / UploadVideos.php文件中删除该文件)。
CandidateProfileController.php:
$candidateProfile = new CandidateProfile();
$candidateProfile->disk = config('site.upload_disk');
// Video One
if($file = $request->file('video_one')) {
$file_path = $file->getPathname();
$name = time() . $file->getClientOriginalName();
$name = preg_replace('/\s+/', '-', $name);
$file->storePubliclyAs('videos', $name, 'public');
$candidateProfile->video_one = $name;
}
if($candidateProfile->save()) {
// dispatch a job to handle the image manipulation
$this->dispatch(new UploadVideos($candidateProfile));
return response()->json($candidateProfile, 200);
} else {
return response()->json([
'message' => 'Some error occurred, please try again.',
'status' => 500
], 500);
}
Jobs / UploadVideos.php:
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $candidateprofile;
public $timeout = 120;
public $tries = 5;
/**
* Create a new job instance.
*
* @param CandidateProfile $candidateProfile
*/
public function __construct(CandidateProfile $candidateProfile)
{
$this->candidateprofile = $candidateProfile;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$disk = $this->candidateprofile->disk;
$filename = $this->candidateprofile->video_one;
$original_file = storage_path() . '/videos/' . $filename;
try {
// Video One
Storage::disk($disk)
->put('videos/'.$filename, $original_file, 'public');
// Update the database record with successful flag
$this->candidateprofile->update([
'upload_successful' => true
]);
} catch(\Exception $e){
Log::error($e->getMessage());
}
}
的第二个参数put()
应该是文件的内容,而不是文件的路径。另外,除非你已经更新了public
硬盘,否则config/filesystem.php
视频将不会存储在中storage_path() . '/videos/...'
。
为了使它起作用,你只需要更新你的Job
代码即可:
$filename = 'videos/' . $this->candidateprofile->video_one;
Storage::disk($this->candidateprofile->disk)
->put($filename, Storage::disk('public')->get($filename), 'public');
$this->candidateprofile->update([
'upload_successful' => true,
]);
同样,将你的代码包装在try/catch
中将意味着该作业不会重试,因为从技术上讲它永远不会失败。
谢谢你。我删除了try / catch,现在可以了,
public $tries = 5;
因此应重试5次。此设置还可以吗?还是我应该保留它,这样它才能在后台不断尝试?@RyanSacks 5退休应该是罚款。TBF,我很想将其降低到3,但完全取决于您。:)