我使用S3 PHP类学习亚马逊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类。
我使用的非设计的Amazon S3的PHP类 。
如果你希望公众访问桶,它就是这么简单
HTTP:// [YourBucketName] .s3.amazonaws.com / [YourFileName]
只要你正确地设置权限。
如果你担心下载虐待,你会希望通过身份验证的URL(这我猜你从你的代码sammple想)。 在这种情况下,我建议你使用Amazon SDK: 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网址:
http(s)://<bucket>.s3.amazonaws.com/<object>
http(s)://s3.amazonaws.com/<bucket>/<object>
我看到,人们已经作出了回应,但我想补充一些更多的上下文对于那些谁可能有一个安全的水桶(需要访问)。 请注意,您不必生成的网址,如果你直接跟我们的S3存储,那么你可以使用“的file_get_contents”等,但它会非常慢,你不能同时使用多卷曲请求(速度)。 但是,如果你有一个新的PHP版本中,您可以使用并行线程。
安装:安装亚马逊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,然后使用多重卷曲工具,让他们在同一时间(推荐)。
一个简单的例子:
$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#_getObjectUrl http://undesigned.org.za/2007/10/22/amazon -S3-PHP-类/文档
供参考:
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;
}
如果您使用aws-sdk-php v3
和您的文件是私人的。
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => 'my-bucket',
'Key' => 'testKey'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();