如何为 Amazon S3 存储桶的对象创建下载链接


How to create download link for an Amazon S3 bucket's object?

我正在使用 S3 PHP Class 学习 Amazon S3。我已将所有文件上传到 S3 存储桶,现在我想为存储桶中的每个可用文件创建链接。

以下功能对我有用吗?

public static function getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false)
{
}
    $s3 = new S3('access-key', 'secret-key');
    $s3->getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false);

或者另一个函数,如 get_object_url ,但get_object_url()不在我的 S3 类中。

我正在使用Undesigned的Amazon S3 PHP类。

如果您希望公众访问存储桶,它就像

http://[YourBucketName].s3.amazonaws.com/[YourFileName]

只要您正确设置权限即可。

如果您担心下载滥用,则需要一个经过身份验证的URL(我猜您希望从代码混乱中获取(。在这种情况下,我建议您使用 Amazon 开发工具包:http://aws.amazon.com/sdkforphp/因为它包含您需要的示例。

$s3->getObjectUrl($bucket, $filename, '5 minutes');

文档:http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.S3.S3Client.html#_getObjectUrl

以下模式适用于构造 S3 URL:

http(s)://<bucket>.s3.amazonaws.com/<object>
http(s)://s3.amazonaws.com/<bucket>/<object>

除了Hossein的回答之外,如果您想在访问链接后立即开始下载(模拟">另存为..."行为(,则需要在Bucket和Key旁边添加ResponseContentDisposition参数,如下所示:

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey',
    'ResponseContentDisposition' => 'attachment; filename="custom_file_name.mp3"'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();

注意:自定义文件名是可选的,无需指定,下载的文件将具有其 S3 名称。

如果您使用aws-sdk-php v3并且您的文件是私有的。

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();

我看到人们已经对此做出了回应,但我想为那些可能拥有安全存储桶(需要访问权限(的人添加更多上下文。请注意,如果您直接与 S3 存储桶通信,则不必生成 URL,然后您可以使用"file_get_contents"等,但它要慢得多,因为您不能使用多卷曲请求(为了速度(。但是,如果您有较新的 php 版本,则可以使用 pthreads。

安装:为亚马逊安装S3类文件,有一些简单的方法可以使用作曲家添加它或手动下载S3.php文件。

不安全:(见关于此事的其他帖子,只是基本上使用 URL(

http(s)://<bucket>.s3.amazonaws.com/<object>
http(s)://s3.amazonaws.com/<bucket>/<object>

安全 HTTPS(当您的存储桶受到保护时(:

https://amazon.com/file/you/wanted.xxx?ID:XXXXX?SIG:YYYYY  

(1(创建一个 https://网址,并使用多卷曲工具同时获取它们(推荐(。

一个简单的例子:

$url = /path/to_the/file_name/file.ext  
//note check amazon to confirm the path which will contain only "_" and no spaces.
$s3 = new S3($awsAccessKeyID, $awsSecretKey);
$curls[] = $s3->get_object_url($bucketName, $uri, '1 hour');
var_dump($results = multiCurlRequest($curls));    

更多信息:

http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.S3.S3Client.html#_getObjectUrlhttp://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation

仅供参考:

function multiCurlRequest($curlList = array(),$user = '', $pass = '',$timeout = self::MULTI_REQ_TIMEOUT_SECS, $retTxfr = 1) {
    if (empty($curlList) || count($curlList) == 0) return false;
    $master = curl_multi_init();
    $node_count = count($curlList);
    for ($i = 0; $i < $node_count; $i++) {
        $ch[$i] = curl_init($curlList[$i]);
        curl_setopt($ch[$i], CURLOPT_TIMEOUT, $timeout); // -- timeout after X seconds
        curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, $retTxfr);
        curl_setopt($ch[$i], CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch[$i], CURLOPT_USERPWD, "{$user}:{$pass}");
        curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($master, $ch[$i]);
    }
    // -- get all requests at once, finish when done or timeout met --
    do {  curl_multi_exec($master, $running);  }
    while ($running > 0);
    $results = array();
    // -- get results from requests --
    for ($i = 0; $i < $node_count; $i++) {
        $results[$i] = curl_multi_getcontent($ch[$i]);
        if ((int) curl_getinfo($ch[$i], CURLINFO_HTTP_CODE) > 399 || empty($results[$i])) {
            $this->set_request(  [ ['label' => '404', 'href' => $results[$i], '404' => '1' ] ] );
            unset($results[$i]);
        }
        curl_multi_remove_handle($master, $ch[$i]);
        curl_close($ch[$i]);
    }
    curl_multi_close($master);
    if (empty($results)) return false;
    //$results = array_values($results); // -- removed as we want the original positions
    return $results;
}