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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10663238/
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-08-24 22:45:19  来源:igfitidea点击:

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

phpapiamazon-s3amazon-web-services

提问by Frank

I am learning Amazon S3 using S3 PHP Class. I have uploaded my all files to my S3 bucket, now I want to create links for each available file in my bucket.

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

Will the following function work for me?

以下功能对我有用吗?

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);

Or another function like get_object_url, but get_object_url()not in my S3 class.

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

I am using Undesigned's Amazon S3 PHP class.

我正在使用 Undesigned 的Amazon S3 PHP 类

采纳答案by Robbie

If you want the public to access the bucket, it is as simple as

如果你想让公众访问这个bucket,就这么简单

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

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

So long as you set permissions correctly.

只要您正确设置权限。

If you're worried about download abuse, you'll want an authenticated URL (which I guess you want from your code sammple). In which case, I suggest you use the Amazon SDK: http://aws.amazon.com/sdkforphp/as it contains examples of what you need.

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

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

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

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

回答by hari maliya

The following patterns are valid for constructing S3 URLs:

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

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

回答by Mike Q

I see that people have already responded to this but I wanted to add some more context for those who may have a secured bucket (requires access). Note, you don't have to generate the URLs if you talk directly to the S3 bucket, then you can use 'file_get_contents' etc. but it is much slower as you can't use multi curl requests (for speed). However you could use pthreads if you have a newer php release.

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

INSTALLATION: Install the S3 Class file for Amazon, there are easy ways to add it using composer or just downloading the S3.php file manually.

安装:为 Amazon 安装 S3 类文件,可以使用 Composer 或手动下载 S3.php 文件来添加它。

NOT SECURED: (see other posts on this matter, just basically use the URL)

不安全:(请参阅有关此问题的其他帖子,基本上只使用 URL)

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

SECURED HTTPS (when you have your bucket protected):

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

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

(1) Create a https:// url and use the multi curl tool to get them all at the same time (recommended).

(1) 创建一个 https:// url 并使用 multi curl 工具同时获取它们(推荐)。

A simplistic example:

一个简单的例子:

$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));    

more info:

更多信息:

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

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

FYI:

供参考:

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;
}

回答by Hossein

If you use aws-sdk-php v3and your file is private.

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

$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'my-bucket',
    'Key' => 'testKey'
]);

$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');

$presignedUrl = (string) $request->getUri();

回答by imprfekt

In addition to Hossein's answer, if you want to start download immediately upon visiting the link (simulate "Save as..." behaviour), you need to add ResponseContentDisposition parameter alongside Bucket and Key, like so:

除了侯赛因的回答之外,如果您想在访问链接后立即开始下载(模拟“另存为...”行为),您需要在 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();

Note: custom filename is optional, without specifying it, downloaded file will have its S3 name.

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