将文件从AWS S3下载到客户端系统

时间:2015-11-02 18:48:23

标签: php amazon-s3 amazon-ec2 download aws-sdk

我正在尝试为我的用户提供下载文件选项。我正在AWS EC2AWS PHP SDK V2.8一起工作。我可以在我的网站上显示图像。我根据问题force-download-with-php-on-amazon-s3尝试但没有成功。这个问题的大部分答案都很古老。我使用下面的代码上传

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";
}

以下是尝试的内容。

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");

我试过

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

enter image description here

但没有成功。任何帮助欣赏。

1 个答案:

答案 0 :(得分:1)

如果对象具有公共读取权限,您应该能够链接到它。 您还可以使用bucket policy设置对存储桶中所有对象的读取权限。

您还可以重定向到对象的S3 presigned URL

$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);