1

I am trying to provide download file option to my users. I am working on AWS EC2 with AWS PHP SDK V2.8. I am able to display images on my website. I try according to question force-download-with-php-on-amazon-s3 but no success. Most of the answer of this question are pretty old. I am using below code 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";
}

Here is what is tried.

header('Content-disposition: attachment; filename=s3112.png');
header('Content-type: image/png');
readfile("https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png");

//header("Location: https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png");
//// Location redirection to a MP3 that lets the browser decide what to do.
//header("Location:  https://s3-ap-southeast-1.amazonaws.com/mytest.sample/s3112.png");

I tried with

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

enter image description here

but no success. Any help appreciate.

Community
  • 1
  • 1
urfusion
  • 5,528
  • 5
  • 50
  • 87

1 Answers1

1

If the object has public-read access, you should just be able to link to it. You can also set read access to all objects in a bucket using a bucket policy.

You could also redirect to an S3 presigned URL of the object:

$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);
Francis Eytan Dortort
  • 1,407
  • 14
  • 20
  • as you check in my question I am using `AES256` for encrypt my files. So if there any way download them. – urfusion Nov 03 '15 at 07:33
  • S3 server-side encryption is for data "at rest" and should not affect how an object is retrieved. "Decryption of the encrypted data requires no effort on your part. When you GET an encrypted object, we fetch and decrypt the key, and then use it to decrypt your data." (source https://aws.amazon.com/blogs/aws/new-amazon-s3-server-side-encryption/) – Francis Eytan Dortort Nov 03 '15 at 08:47