我正在尝试为我的用户提供下载文件选项。我正在AWS EC2
与AWS 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>
但没有成功。任何帮助欣赏。
答案 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);