据我所知,没有办法检查从 S3 下载的当前状态。话虽如此,S3 确实有足够的可用带宽,所以我不会太担心他们的服务器超载 :) 就在上周,亚马逊宣布S3 现在平均每秒处理 650,000 个对象。
如果您想在 PHP 中实现类似 @Pushpesh 的解决方案,一种解决方案是使用 Amazon SDK for PHP 并执行以下操作:
<?php
#Generate presigned S3 URL to download S3 object from
# Include AWS SDK for PHP and create S3
require_once("./aws-sdk/sdk.class.php");
$s3 = new AmazonS3();
# Let S3 know which file we want to be downloading
$s3_bucket_name = "yours3bucketname";
$s3_object_path = "folder1/object1.zip";
$s3_url_lifetime = "10 minutes";
$filename = "download.zip";
#Check whether the user has already downloaded a file in last two hours
$user_can_download = true;
if($user_can_download) {
$s3_url = $s3->get_object_url($s3_bucket_name, $s3_object_path, $s3_url_lifetime, array('response' => array('content-type' => 'application/force-download', 'content-disposition' => 'attachment; filename={$filename}')));
header("Location: {$s3_url}");
}
else {
echo "Sorry, you need to wait a bit longer before you can download a file again...";
}
?>
这使用 get_object_url 函数,该函数生成预签名 URL,允许您让其他人下载您在 S3 中设置为私有的文件,而无需公开这些文件。
如您所见,此生成的链接只会在 10 分钟内有效,并且是唯一的链接。因此,您可以安全地让人们从此链接下载,而不必担心人们传播该链接:该链接将过期。人们获得新的有效链接的唯一方法是通过您的下载脚本,如果尝试启动下载的 IP/用户已经超过其使用限制,它将拒绝生成新链接。但是,在 S3 中将这些文件设置为私有很重要:如果您将它们公开,这不会有多大好处。您可能还想查看生成这些预签名 URL 的 S3 API的文档。