Rôm*_*ias 23 php amazon-s3 amazon-web-services laravel-5.2
我正在寻找一个很好的解决方案来获取亚马逊s3的签名网址.
我有一个版本使用它,但没有使用laravel:
private function getUrl ()
{
$distribution = $_SERVER["AWS_CDN_URL"];
$cf = Amazon::getCFClient();
$url = $cf->getSignedUrl(array(
'url' => $distribution . self::AWS_PATH.rawurlencode($this->fileName),
'expires' => time() + (session_cache_expire() * 60)));
return $url;
}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是使用laravel的最佳方式,因为它有一个完整的文件系统可以工作......
但如果没有其他办法,我该如何获得客户?调试我在Filesystem对象中找到了它的一个实例,但它受到保护......
bri*_*n_d 45
在Laravel,
$s3 = \Storage::disk('s3');
$client = $s3->getDriver()->getAdapter()->getClient();
$expiry = "+10 minutes";
$command = $client->getCommand('GetObject', [
'Bucket' => \Config::get('filesystems.disks.s3.bucket'),
'Key' => "file/in/s3/bucket"
]);
$request = $client->createPresignedRequest($command, $expiry);
return (string) $request->getUri();
Run Code Online (Sandbox Code Playgroud)
确保您拥有适用于flysystem composer包的AWS(版本会有所不同):
"league/flysystem-aws-s3-v3": "1.0.9"
Run Code Online (Sandbox Code Playgroud)
Ken*_*ael 28
对于Laravel 5.5及更高版本,您现在可以使用临时URL/s3预签名URL.
use \Storage;
// Make sure you have s3 as your disk driver
$url = Storage::disk('s3')->temporaryUrl(
'file1.jpg', Carbon::now()->addMinutes(5)
);
Run Code Online (Sandbox Code Playgroud)
这仅适用于s3存储驱动程序AFAIK.
https://laravel.com/docs/5.5/filesystem#retrieving-files
经过大量的错误,我终于找到了使用以下代码访问s3存储桶的私有内容的解决方案:-
use Storage;
use Config;
$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = Config::get('filesystems.disks.s3.bucket');
$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => '344772707_360.mp4' // file name in s3 bucket which you want to access
]);
$request = $client->createPresignedRequest($command, '+20 minutes');
// Get the actual presigned-url
echo $presignedUrl = (string)$request->getUri();
Run Code Online (Sandbox Code Playgroud)
上面的解释答案 (@brian_d) 没问题,但生成预签名 url 需要太多时间。我浪费了将近 4-5 天来克服这个问题。终于为我工作了。感谢@Kenth。
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
$disk = Storage::disk('s3');
$url = $disk->getAwsTemporaryUrl($disk->getDriver()->getAdapter(), $value, Carbon::now()->addMinutes(5), []);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12775 次 |
最近记录: |