Chapters
40 Chapters
|
4:39:58
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
38.
Private Downloads & Signed URLs
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4 but works great in Symfony 5!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.87.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.9.0
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.22
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.0.3
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.4
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.2.3
"symfony/console": "^4.0", // v4.2.3
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.2.3
"symfony/framework-bundle": "^4.0", // v4.2.3
"symfony/property-access": "4.2.*", // v4.2.3
"symfony/property-info": "4.2.*", // v4.2.3
"symfony/security-bundle": "^4.0", // v4.2.3
"symfony/serializer": "4.2.*", // v4.2.3
"symfony/twig-bundle": "^4.0", // v4.2.3
"symfony/validator": "^4.0", // v4.2.3
"symfony/web-server-bundle": "^4.0", // v4.2.3
"symfony/yaml": "^4.0", // v4.2.3
"twig/extensions": "^1.5" // v1.5.4
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.1.0
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.2.3
"symfony/dotenv": "^4.0", // v4.2.3
"symfony/maker-bundle": "^1.0", // v1.11.3
"symfony/monolog-bundle": "^3.0", // v3.3.1
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.2.3
"symfony/stopwatch": "4.2.*", // v4.2.3
"symfony/var-dumper": "^3.3|^4.0", // v4.2.3
"symfony/web-profiler-bundle": "4.2.*" // v4.2.3
}
}
8 Comments
Hello,
How would we go about integrating cloudfront into the symfony application?
There aren't any good tutorials on the web. This was the main thing I was looking for in this course.
Hey Laura M.!
Fair question :). CloudFront can be used for two separate things... and how you integrate it is a bit different for those 2 different things.
1) You can upload files to S3, but then you want CloudFront to actually serve these to your users. In this case, you set up a cloud front distribution that "pulls" from your S3 bucket. Getting the permissions/config correct *can* be a pain, from experience :/. But once you have it set up, you will have a new cloudfront URL that you can use instead of your S3 domain. Then, you can just replace your S3 URL in your code that we created in this tutorial with the new CloudFront URL. Basically, on the Symfony side, you're just rendering a different hostname (the CloudFront host name) to the assets.
2) You can make CloudFront serve your static JavaScript/CSS assets. This is similar, except that you tell CloudFront to "pull" from your "origin". That's a fancy way of telling cloudfront that when someone asks for /css/main.css on CloudFront, to actually fetch it from YourDomain.com/css/main.css. Once you have this setup... you (once again) just need to make sure that your static assets all point to the cloud front domain in your application. How you do this depends a bit on if you're using Webpack Encore, but probably includes setting the "assets base_urls" setting to your new cloud front distribution domain. https://symfony.com/doc/cur...
So, it is kind of a tricky topic... but actually has nothing to do with uploading... it's ALL about CloudFront configuration. You can also create signed URLs from CloudFront. You would configure a distribution like we did in part (1) above then get signed URLs from it. The config & authentication is kind of a pain - CloudFront is never friendly :/.
If you have any specific questions, you're welcome to ask - we've implemented this a few times in the past.
Cheers!
Hello,
I managed to get it working. After I set up my distribution, I just replaced my s3 bucket host parameter from my symfony parameters.yml with my Cloudfront domain name and it worked.
Next step is to make signed URLs, I will dig a little before trying.
Thanks a lot for your feedback.
Hello Ryan,
Great tutorial as always. I have just one question here, what if the filename has some non ASCII characters?? For example, a file with my lastname "Gonçalves.pdf". How would you suggest we could serve the file with its original filename?
I used mb_convert_encoding but the result is not great.
Thanks
Hey Ricardo manuel de faria silva gonçalves!
Hmm... interesting question! I wonder at what point you start seeing issues? Storing the ç in the database should be fine (as long as you have utf8 encoding on your database). And that character should also be totally fine being used in a URL. At what point are you seeing the character having issues?
Cheers!
Everything works fine up until I use makeDisposition. I get an error like "the filename fallback must only contain ASCII characters.".
Storing in the db is not an issue, everything worked correctly until this moment, and now that I think about it, it was working fine before using S3 (cannot be 100% because I stumbled upon this by accident, but I could swear the downloader worked correctly before).
Ah, indeed! I learned something! Apparently the original HTTP spec says that you're only allowed to use ASCII characters in your string, which is why Symfony enforces that. But apparently many browsers *do* allow non-ASCII characters... but maybe not all of them :/ - https://stackoverflow.com/q...
So, the answer is that if you DO need non-ASCII characters, you'll need to build the Content-Disposition header manually, but then it also may not work in all browsers :/. If you want to remove the characters, https://github.com/doctrine... might do it, but the character encoding stuff is not my string suit.
Cheers!
Thanks weaverryan .
In the past, I can't remember a case where the exact filename was a requirement, translating ñ to n or ç to c is actually normal. In any case, if needed, I'll set up the headers manually and well, let's hope the end user has the correct browser.
Cheers!
"Houston: no signs of life"
Start the conversation!