简体   繁体   English

使用 Laravel 从 Amazon S3 下载文件

[英]Download file from Amazon S3 with Laravel

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() ?如何通过Response::download()正确使用 S3 的结果?

Thanks!谢谢!

EDIT: I've found I can use $s3->getObjectUrl($bucket, $file, $expiration) to generate an access URL.编辑:我发现我可以使用$s3->getObjectUrl($bucket, $file, $expiration)来生成访问 URL。 This could work, but it still doesn't solve the problem above completely.这可以工作,但它仍然不能完全解决上述问题。

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?不过,仍然不认为它是理想的吗?

The S3Client::getObject() method allows you to specify headers that S3 should use when it sends the response. S3Client::getObject()方法允许您指定 S3 在发送响应时应使用的标头。 The getObjectUrl() method uses the GetObject operation to generate the URL, and can accept any valid GetObject parameters in its last argument. getObjectUrl()方法使用 GetObject 操作生成 URL,并且可以在其最后一个参数中接受任何有效的 GetObject 参数。 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:您应该能够通过执行以下操作,使用预先签名的 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 Server article on the AWS Developer Guide如果您想从服务器流式传输 S3 对象,则应查看 AWS 开发人员指南上的从 Web 服务器流式传输 Amazon S3 对象一

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.因此,您可以将 SaveAs 选项与 getObject 方法一起使用。 You can also specify the version id if you are using versioning on your bucket and want to make use of it.如果您在存储桶上使用版本控制并希望使用它,您还可以指定版本 ID。

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

The answer is somewhat outdated with the new SDK.新 SDK 的答案有些过时。 The following works with v3 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}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM