使用 Laravel 从 Amazon S3 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18839216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:25:57  来源:igfitidea点击:

Download file from Amazon S3 with Laravel

phpamazon-s3laravel

提问by Prash

I'm a little sure as to how to launch a download of a file from Amazon S3 with Laravel 4. I'm using the AWS

我有点确定如何使用 Laravel 4 从 Amazon S3 启动文件下载。我正在使用 AWS

$result = $s3->getObject(array(
    'Bucket' => $bucket,
    'Key'    => 'data.txt',
));

// temp file
$file = tempnam('../uploads', 'download_');

file_put_contents($file, $result['Body']);

$response = Response::download($file, 'test-file.txt');

//unlink($file);

return $response;

The above works, but I'm stuck with saving the file locally. How can I use the result from S3 correctly with Response::download()?

以上工作,但我坚持在本地保存文件。如何正确使用 S3 的结果Response::download()

Thanks!

谢谢!

EDIT: I've found I can use $s3->getObjectUrl($bucket, $file, $expiration)to generate an access URL. This could work, but it still doesn't solve the problem above completely.

编辑:我发现我可以$s3->getObjectUrl($bucket, $file, $expiration)用来生成访问 URL。这可以工作,但仍然不能完全解决上述问题。

EDIT2:

编辑2:

$result = $s3->getObject(array(
    'Bucket' => $bucket,
    'Key'    => 'data.txt',
));

header('Content-type: ' . $result['ContentType']);
header('Content-Disposition: attachment; filename="' . $fileName . '"');
header('Content-length:' . $result['ContentLength']);

echo $result['Body'];

Still don't think it's ideal, though?

尽管如此,仍然不认为它是理想的吗?

回答by Jeremy Lindblom

The S3Client::getObject()methodallows you to specify headers that S3 should use when it sends the response. The getObjectUrl()methoduses the GetObject operation to generate the URL, and can accept any valid GetObject parameters in its last argument. You should be able to do a direct S3-to-user download with your desired headers using a pre-signed URL by doing something like this:

S3Client::getObject()方法允许您指定 S3 在发送响应时应使用的标头。该getObjectUrl()方法使用 GetObject 操作生成 URL,并且可以在其最后一个参数中接受任何有效的 GetObject 参数。您应该能够通过执行以下操作,使用预先签名的 URL 使用所需的标头直接从 S3 下载到用户:

$downloadUrl = $s3->getObjectUrl($bucket, 'data.txt', '+5 minutes', array(
    'ResponseContentDisposition' => 'attachment; filename="' . $fileName . '"',
));

If you want to stream an S3 object from your server, then you should check out the Streaming Amazon S3 Objects From a Web Serverarticle on the AWS Developer Guide

如果您想从服务器流式传输 S3 对象,则应查看AWS 开发人员指南上的从 Web 服务器流式传输 Amazon S3 对象一

回答by Rehmat

This question is not answered fully. Initially it was asked to how to save a file locally on the server itself from S3 to make use of it.

这个问题没有完全回答。最初,它被问及如何从 S3 将文件本地保存在服务器本身上以使用它。

So, you can use the SaveAs option with getObject method. You can also specify the version id if you are using versioning on your bucket and want to make use of it.

因此,您可以将 SaveAs 选项与 getObject 方法结合使用。如果您在存储桶上使用版本控制并希望使用它,您还可以指定版本 ID。

$result = $this->client->getObject(array(
'Bucket'=> $bucket_name,
'Key'   => $file_name,
'SaveAs'  => $to_file,
'VersionId' => $version_id));

回答by shanecp

The answer is somewhat outdated with the new SDK. The following works with v3 SDK.

新 SDK 的答案有些过时。以下适用于 v3 SDK。

$client->registerStreamWrapper();
$result = $client->headObject([
    'Bucket' => $bucket,
    'Key'    => $key
]);

$headers = $result->toArray();

header('Content-Type: ' . $headers['ContentType']);
header('Content-Disposition: attachment');

// Stop output buffering
if (ob_get_level()) {
    ob_end_flush();
}

flush();

// stream the output
readfile("s3://{$bucket}/{$key}");