php - 使用 lumen/php 从 Amazon S3 下载文件
问题描述
根据 AWS 文档中给出的资源,可以使用以下代码下载对象:
public function download($folder, $file)
{
$s3 = new S3Client([
'region' => env('AWS_REGION'),
'version'=>'latest',
'credentials' => [
'key' =>env('AWS_ACCESS_KEY_ID'),
'secret' =>env('AWS_SECRET_ACCESS_KEY')
],
]);
$result = $s3->getObject([
'Bucket' => env('AWS_BUCKET'),
'Key' => $folder.'/'.$file,
'ResponseContentType' => 'text/plain',
'ResponseContentLanguage' => 'en-US',
'ResponseContentDisposition' => 'attachment; filename='.$file,
'ResponseCacheControl' => 'No-cache',
'ResponseExpires' => gmdate(DATE_RFC2822, time() + 3600),
]);
header("Content-Type: {$result['ContentType']}");
echo $result['Body'];
}
根据文档,上面的代码应该显示一个pdf文件(在我的例子中),它打开一个pdf但是说
无法加载 PDF 文档。
该文件是 s3 没有任何问题,我知道它的代码。但是我可以对浏览器做些什么来阅读和自动下载呢?
有关详细信息,我点击此链接https://docs.aws.amazon.com/AmazonS3/latest/userguide/download-objects.html并使用 AWS SDKs
编辑
我改成'ResponseContentType' => 'plain/text'
了'ResponseContentType' => 'application/pdf'
,它打开了pdf,但现在我需要下载它
解决方案
这很可能是:
'ResponseContentType' => 'application/pdf',
否则它将作为文本传输并导致文件损坏。
推荐阅读
- r - r - 如何在同一面板中绘制 dygraphs
- mysql - 获取相同的数字
- java - 如何为嵌套的 json 创建 JSONObject
- regex - 如果 vlookup 在 Google 表格中的其他两个单元格上匹配,如何减去两个单元格的值
- excel - Excel 索引与两个不同的工作表匹配给了我#VALUE 错误
- maven - 无法从入门指南或使用 Maven 的示例源执行 Maven 目标以构建 Tapestry 5.4.x
- macos - Mac 上的 PgAdmin 4 在尝试恢复时看不到文件系统
- java - ElasticSearch 通过数组字段搜索作为排他搜索
- azure-devops - 在 CLI 任务中使用服务连接?
- python - 从自然语言字符串中提取日期