This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

vdomah
vdomah

Can't implement direct upload method of s3 on OctoberCMS.

Сonditions: the octoberCMS project is located on heroku server wich got upload file size limit == 4Mb Goal: to be able upload files bigger then 4Mb Methods: direct upload s3 feature wich is implemented like this https://stackoverflow.com/questions/42994214/laravel-5-4file-upload-to-into-aws-s3-directly-to-bucket#42994416

Steps:

  • need to be able to save files directly to s3 without creating temporary copy on the project's filesystem
  • after that need to create System\Models\File object using file url wich we got after performing first step

Code of my method trying to implement this. Thanks for help!

public function onUploadAvatar()
{
    $data = \Request::all();

    try {
        $imageName = 'uploads/' . $data['file']->getClientOriginalName();
        $image_url = \Storage::disk('s3')->put($imageName, file_get_contents($data['file']));

        $user = UserModel::find(post('user_id'));
        if (!$user)
            throw new Exception('No user found with id ' . post('user_id'));

        File::where('attachment_id', $user->id)
            ->where('attachment_type', 'RainLab\User\Models\User')
            ->where('field', 'avatar')
            ->delete();

        $file = new File;
        //$file->data = $data['avatar'];
        $file->fromFile($image_url);
        $file->attachment_id = $user->id;
        $file->attachment_type = 'RainLab\User\Models\User';
        $file->field = 'avatar';
        $file->save();

        $preview_path = $file->getThumb(77, 77, ['mode' => 'crop']);

        return response()->json([
            'result' => 1,
            'data' => ['file_id' => $file->id, 'preview_path' => (string)$preview_path],
        ], 200);
    }
    catch (Exception $ex) {
        return response()->json(['result' => 0, 'error' => $ex->getMessage()], 400);
    }
}

Last updated

JeffGoldblum
JeffGoldblum

I'm pretty sure that you'd have to change the JS of the fileupload formwidget itself in order to upload directly to S3, any server side processing of the uploaded files would involve running into the max_upload_filesize limit

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.