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的结果和响应::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:
EDIT2:
$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?
仍然不认为这是理想的吗?
3 个解决方案
#1
18
The S3Client::getObject()
method allows you to specify headers that S3 should use when it sends the response. The getObjectUrl()
method uses 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-to-user直接下载:
$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 PHP Developer Blog.
如果您想要从服务器上流一个S3对象,那么您应该查看AWS PHP开发人员博客上Web服务器文章中的Amazon S3对象流。
#2
2
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方法。如果您在bucket上使用版本控制并希望使用版本id,也可以指定版本id。
$result = $this->client->getObject(array(
'Bucket'=> $bucket_name,
'Key' => $file_name,
'SaveAs' => $to_file,
'VersionId' => $version_id));
#3
0
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}");
#1
18
The S3Client::getObject()
method allows you to specify headers that S3 should use when it sends the response. The getObjectUrl()
method uses 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-to-user直接下载:
$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 PHP Developer Blog.
如果您想要从服务器上流一个S3对象,那么您应该查看AWS PHP开发人员博客上Web服务器文章中的Amazon S3对象流。
#2
2
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方法。如果您在bucket上使用版本控制并希望使用版本id,也可以指定版本id。
$result = $this->client->getObject(array(
'Bucket'=> $bucket_name,
'Key' => $file_name,
'SaveAs' => $to_file,
'VersionId' => $version_id));
#3
0
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}");