Last active
January 19, 2022 08:26
-
-
Save treetop1500/79993d37b02a5b7b50ca3c30bf5c9fe5 to your computer and use it in GitHub Desktop.
Amazon S3 Force Download of Remote File with Symfony (AWS S3 PHP SDK V.3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
}, | |
"require": { | |
"aws/aws-sdk-php": "3.*" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is auto-generated during the composer install | |
parameters: | |
... | |
amazon_key: my_key | |
amazon_secret: my_secret | |
amazon_bucket: my_bucket | |
amazon_region: us-east-1 | |
amazon_version: latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
... | |
my.amazon.s3: | |
class: Aws\S3\S3Client | |
factory_class: Aws\S3\S3Client | |
factory_method: factory | |
arguments: | |
- | |
credentials: { key: "%amazon_key%", secret: "%amazon_secret%" } | |
region: "%amazon_region%" | |
version: "%amazon_version%" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
media_download: | |
path: /media/download/{file}{trailingSlash} | |
defaults: { _controller: UtilBundle:Download, trailingSlash : "/" } | |
requirements: { file: "([A-Za-z0-9-._s]+)", trailingSlash : "[/]{0,1}" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="{{ url('media_download',{'file':entity.path}) }}"> | |
Download | |
</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace UtilBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Response; | |
class DefaultController extends Controller | |
{ | |
public function downloadAction($file) | |
{ | |
$file_path = "uploads/media/$file"; // path to file once inside bucket | |
$s3 = $this->get('my.amazon.s3'); | |
$params = array( | |
'Bucket' => $this->getParameter("amazon_bucket"), | |
'Key' => $file_path, | |
'ResponseContentType' => 'application/octet-stream', | |
'ResponseContentDisposition' => 'attachment; filename="'.$file.'"', | |
'ResponseCacheControl' => 'No-cache', | |
); | |
$command = $s3->getCommand('GetObject',$params); | |
$preSignedRequest = $s3->createPresignedRequest($command, '+10 minutes'); | |
$body = $preSignedRequest->getUri(); | |
header("Location: ".$body); | |
die(); | |
return new Response(); // controllers require a response or Symfony will error | |
} | |
} |
Eventually the solution proposed here worked: https://stackoverflow.com/questions/56326183/service-not-found-even-though-it-exists-in-the-apps-container
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I tried and got the following error: