【问题标题】:How to queue upload to s3 using Laravel?如何使用 Laravel 排队上传到 s3?
【发布时间】:2023-03-20 21:58:01
【问题描述】:

我正在调度一个作业来排队我的视频文件,这些文件存储在 s3 上。 一切正常,除非我上传一个 20mb 的视频文件,当我查看我的存储桶时,它说文件是 120b。所以这让我觉得我将路径和文件名作为字符串而不是文件对象上传。 出于某种原因,当我尝试使用 Storage::get()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);
}

工作/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());
        }
    }

【问题讨论】:

  • 只是为了确认一下,您的 Job 是否打算将文件从您的 public 磁盘复制到 S3?

标签: laravel amazon-s3


【解决方案1】:

File Storage docs

put() 的第二个参数应该是文件的内容而不是文件的路径。此外,除非您更新了config/filesystem.php 中的public 磁盘,否则视频不会存储在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 左右,但这完全取决于你。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
  • 2022-08-05
  • 1970-01-01
  • 2018-02-16
  • 2015-05-22
  • 2022-06-29
  • 2017-02-10
相关资源
最近更新 更多