Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Basset work on Vapor #58

Merged
merged 4 commits into from Jul 5, 2023
Merged

Make Basset work on Vapor #58

merged 4 commits into from Jul 5, 2023

Conversation

promatik
Copy link
Member

@promatik promatik commented Jul 3, 2023

Update on #57.

Kept @malek77z changes 馃憣

malek77z and others added 4 commits July 3, 2023 20:02
Ployfil fix for Laravel Vapor.
Removed the double '//' in the s3 url and writes the files to public so they can be accessed from the client.
added env variables so can tweak on vapor server
@@ -5,11 +5,11 @@
'dev_mode' => env('BASSET_DEV_MODE', env('APP_ENV') === 'local'),

// disk and path where to store bassets
'disk' => 'public',
'disk' => env('BASSET_DISK', 'public'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's document these new .ENV variables please. And ideally also create a section in the README called "How to use on Laravel Vapor"

@tabacitu
Copy link
Member

tabacitu commented Jul 5, 2023

Tested and everything still works fine outside Vapor. @malek77z let us know if this makes it work fine for you. And if you could tell us the steps you followed (after this PR) to make it work on Vapor... that would help out so many other people.

I'll merge this PR right now and release a new tag later today, so you'll be able to get it with composer update. Until then you can use composer require backpack/basset:"dev-malek77z-main as 1.9.99". But we'll be fast tagging this, I promise 馃榾

Thanks a lot for the PR 馃檹

Cheers!

@tabacitu tabacitu merged commit 5531142 into main Jul 5, 2023
7 checks passed
@tabacitu tabacitu deleted the malek77z-main branch July 5, 2023 08:35
@malek77z
Copy link
Contributor

malek77z commented Jul 5, 2023

@tabacitu great, i've just installed and confirmed works as expected

Happy to help with the Vapor install as well, having basset actually makes it so much easier and faster to deploy as the number of assets in backpack5 made the deploy time 30 min or so (although removed things like tinymce etc to speed up) and now the deploy is 3 min :-)

Specifically for basset, these are the steps I take

  1. get s3 working locally with s3.. I find this a good step just to confirm it will work in principle.
    Change this in the env
BASSET_DISK=s3
BASSET_CACHE_MAP=false

confirm al the AWS credentials are put in the env correctly

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=
AWS_BUCKET=

Note, there is a lot of options with the permissions for the user, s3 and so on to make sure you have access, but also can write, and make things public..

This is most of them:

For the bucket do the following allowing write etc Also, if using Vapor make sure all the perms are correct for the user

a. Bucket itself Block all public access Off
b. Access control list (ACL) Grantee: Bucket owner (your AWS account) Objects: List, Write Bucket ACL: Read, Write
c. Cross-origin resource sharing (CORS) (dev bucket)

[ { "AllowedHeaders": [ "" ], "AllowedMethods": [ "GET", "PUT" ], "AllowedOrigins": [ "" ], "ExposeHeaders": [] } ]

d. User perms... I.e. a User will be part of a Group, which will have a Policy with these perms (bucket ##BUCKETNAME##)

 { "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:GetObject", "s3:GetObjectAcl", "s3:GetObjectVersion", "s3:DeleteObject", "s3:DeleteObjectVersion", "s3:ReplicateObject" ], "Resource": "arn:aws:s3:::##BUCKETNAME##/*" }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::##BUCKETNAME##" } ] }

With that all done, you should get the s3 to work locally and know that everything is set up mostly correctly.

  1. Get working on Vapor

First, the great thing is Vapor does all the s3 bucket setup for you.
In the vapor.yml file be sure to include this in the config

storage: yourbucketname

I personally am deploying to vapor from github actions, and found it important not to set basset to s3 till after deploy as the deploy script would try to check the s3 connection variables that vapor will set itself. This is really simple though, and simply update the .env in Vapor itself to the following:

BASSET_DISK=s3
BASSET_CACHE_MAP=false

(same as local)

then your golden. You should see the files writing to the s3 bucket that Vapor created for you.
Note that if you change the env variables after deploy in the cli or in the webapp, you will have to redeploy to get them changes... this can be done though the webapp and only takes a min.

@malek77z
Copy link
Contributor

malek77z commented Jul 5, 2023

Also, just to note adding to my work on Vapor, I have done work on the image uploader in backpack itself, not just to upload into a s3 bucket, which isnt too nuts, but the tricky part was changing the image uploader to also use the s3 for the tmp file as well.. Its very cool as uploading massive files is now possible. If thats useful to backpack please let me know who to share with

@tabacitu
Copy link
Member

tabacitu commented Jul 6, 2023

Happy to help with the Vapor install as well, having basset actually makes it so much easier and faster to deploy as the number of assets in backpack5 made the deploy time 30 min or so (although removed things like tinymce etc to speed up) and now the deploy is 3 min :-)

That is so awesome to hear! 馃帀 indeed that was one of the reasons we thought of the Basset solution in the first place, happy to hear it actually solves that problem 馃帀


Thanks A LOT for the steps @malek77z 馃檹 They really really helped us out. And I'm sure they'll help A LOT more people.


the tricky part was changing the image uploader to also use the s3 for the tmp file as well.. Its very cool as uploading massive files is now possible. If thats useful to backpack please let me know who to share with

pick-me-season-10-gif-by-rupauls-drag-race

That sounds interesting! Yes I want to hear more about that. I've opened a thread for us to talk specifically about that - doing all the changes needed for Backpack to support Vapor out-of-the-box. If you can share your experience there, that would be super-helpful - Laravel-Backpack/CRUD#5189

@maulsea
Copy link

maulsea commented Jul 22, 2023

@malek77z First of all, thanks for putting this together!

I ended up here after we deployed to Vapor to realize Basset wasn't working. I am trying to make this work on my local first but running into the below issue. I do not see anything uploaded to the bucket. I am guessing it would upload to AWS_BUCKET.

Currently, there is no way to separate the bucket names for the basset and the app. Is that correct?
I tried setting up another filesystem s3_public and gave it a public bucket as per the configuration recommended. But, that does not seem to have any impact.

CleanShot 2023-07-22 at 20 35 19

@promatik
Copy link
Member Author

Hi @maulsea!

I can see that the paths to basset files are very wrong, it starts with;
/var/www/html/vendor/backpack/...

It should never be a relative path to the system root, it should be relative to the project root.

I can also see that you have a 419 (Page expired) error on your requests, and those have nothing to do with basset, I'm wondering if your issue is related to Basset or some miss configuration on the server.

Is this a new project? Is it a migration from v5? If so, did it worked before v6 upgrade?
Sorry for the trouble 馃憣

@maulsea
Copy link

maulsea commented Jul 24, 2023

@promatik Yes, this is an upgrade from v5 to v6. Works fine locally after the upgrade. It doesn't work on vapor. I am just trying to make it work locally first (the way Basset is supposed to work on vapor). I am not sure why Basset is acting that way when I add two environment variables below. The same happens in a vapor environment as well.

BASSET_DISK=s3
BASSET_CACHE_MAP=false

Any tips are appreciated.

UPDATE:
@promatik Looks like the @basset(base_path('...') references are not working for me in the vapor environment. In other words, basset is not uploading those assets to s3. Is there a way to disable Basset for them? We are okay without optimization there.
@malek77z Do they work for you fine without any changes?

@promatik
Copy link
Member Author

@maulsea, can you check maybe with php artisan tinker what is the result of base_path('')?
Because Laravel highly depends on base_path() something else may not be working, but base_path I think it's very unlikely...

@maulsea
Copy link

maulsea commented Jul 26, 2023

@promatik The screenshot I have shared above is my local environment. base_path() value is /var/www/html. I am running Laravel on Docker locally. Not sure why http://localhost is getting appended there. that surely cannot work.

Just to clarify again, if I don't add Basset-specific variables (BASSET_DISK=s3 and BASSET_CACHE_MAP=false) to env (that I will have to for vapor), everything works fine locally. That's telling me I need to tweak something in Basset. I could be wrong though.

Let me know your thoughts.

@promatik
Copy link
Member Author

@maulsea by any chance can you share with us your public disk configuration?
It's located at /config/filesystems.php.

'disks' => [
    ...
    'public' => [
        // what are the configurations here?
    ],

@malek77z
Copy link
Contributor

malek77z commented Aug 7, 2023

@maulsea How are you getting on with this now?
Just to confirm that the config you have is on the vapor env file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants