Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)
edited by

When I try to provide a download file option to my users, I'm not getting any success. Below is the code that I'm using for uploading:

try {

    $result = $s3->putObject(array(

       'Bucket' => $bucketName,

       'ACL' => 'authenticated-read',

       'Key' => "s3112.png",

       'ServerSideEncryption' => 'AES256',

        'SourceFile' => $filepath,

       'ContentType' => mime_content_type($filepath),

        'debug' => [

          'logfn' => function ($msg) {

               echo $msg . "\n";

           },

            'stream_size' => 0,

           'scrub_auth' => true,

           'http' => true,

       ],

   ));

} catch (S3Exception $e) {

   echo $e->getMessage() . "\n";

}

And I tried, 

"<a href="https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.pn" download>" 

but still no success. 

Any help would be appreciated.

1 Answer

0 votes
by (12.4k points)
edited by

For your case you can redirect it to an S3 preassigned URL of the object, like:

$cmd = $s3Client->getCommand('GetObject', [

    'Bucket' => 'my-bucket',

    'Key'    => 'testKey'

]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

// Get the actual presigned-url

$presignedUrl = (string) $request->getUri();

header('Location: ' . $presignedUrl);

To know more about AWS s3 read here.

Do checkout aws certification training to master AWS.

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
+4 votes
1 answer

31k questions

32.4k answers

501 comments

693 users

Browse Categories

...