Warm tip: This article is reproduced from serverfault.com, please click

amazon s3-如何使用Laravel将上传到s3的队列?

(amazon s3 - How to queue upload to s3 using Laravel?)

发布于 2020-11-30 15:46:59

我正在分派作业以将我的视频文件排队,这些文件存储在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());
        }
    }
Questioner
Ryan Sacks
Viewed
11
Rwd 2020-12-01 00:20:01

文件存储文档

的第二个参数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中将意味着该作业不会重试,因为从技术上讲它永远不会失败。