使用文件系统 Laravel 5.2 从 amazon s3 获取文件的签名 URL
Get file's signed URL from amazon s3 using Filesystem Laravel 5.2
我正在寻找一个好的解决方案来从 amazon s3 获得签名的 url。
我有一个版本可以使用它,但没有使用 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;
}
我不知道这是否是处理 laravel 的最佳方式,考虑到它有一个完整的文件系统可以工作...
但是如果没有别的办法,我要怎么获取客户端呢?调试我在 Filesystem 对象中找到了它的一个实例,但它是受保护的...
在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();
确保你也有 AWS for flysystem composer 包(版本会有所不同):
"league/flysystem-aws-s3-v3": "1.0.9"
对于 Laravel 5.5 及以上,
您现在可以使用临时 URLs/s3 预签名 URL。
use \Storage;
// Make sure you have s3 as your disk driver
$url = Storage::disk('s3')->temporaryUrl(
'file1.jpg', Carbon::now()->addMinutes(5)
);
这仅适用于 s3 存储驱动程序 AFAIK。
经过很多错误,我终于找到了使用以下代码访问 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();
上面的解释答案(@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), []);
这是将图像上传到 S3 并创建签名的完整代码 URL;
首先获取一个文件对象并创建对象名
$filename = $_FILES["image"]["name"];
$array = explode('.', $filename);
$fileExt = $array[1]; // to get file extension
$objectName = 'images/' . time() .$fileExt; // create object name
$document = fopen($_FILES["image"]["tmp_name"],'r');
// Code to upload document on s3
$s3 = \Storage::disk('s3');
$s3->put($objectName,$document,'public');
// If you want to get uploaded document Url you can use below code:
$image_name = \Storage::disk('s3')->url($name); // it will return stored document url
create/get 签名 URL 的代码,你也可以把它作为一个单独的函数:
$s3 = \Storage::disk('s3');
$client = $s3->getDriver()->getAdapter()->getClient();
$expiry = "+10 minutes";
$command = $client->getCommand('GetObject', [
'Bucket' => config('params.YOUR_AWS_BUCKET_NAME'), // bucket name
'Key' => $objectName
]);
$request = $client->createPresignedRequest($command, $expiry);
$url = (string) $request->getUri(); // it will return signed URL
$file_path = "profile/image.png";
$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = config('filesystems.disks.s3.bucket');
$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $file_path // file name in s3 bucket which you want to access
]);
$request = $client->createPresignedRequest($command, '+5 minutes');
return $presignedUrl = (string) $request->getUri(); // Get the actual presigned-url
对于 Laravel 9 显示受保护文件夹中的所有图像的示例:
$function = function ($image){
return Storage::disk('s3')->temporaryUrl(
$image, Carbon::now()->addMinutes(60)
);
};
$images = Storage::disk('s3')->allFiles('images/client');
return array_map($function,$images);
我正在寻找一个好的解决方案来从 amazon s3 获得签名的 url。
我有一个版本可以使用它,但没有使用 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;
}
我不知道这是否是处理 laravel 的最佳方式,考虑到它有一个完整的文件系统可以工作...
但是如果没有别的办法,我要怎么获取客户端呢?调试我在 Filesystem 对象中找到了它的一个实例,但它是受保护的...
在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();
确保你也有 AWS for flysystem composer 包(版本会有所不同):
"league/flysystem-aws-s3-v3": "1.0.9"
对于 Laravel 5.5 及以上, 您现在可以使用临时 URLs/s3 预签名 URL。
use \Storage;
// Make sure you have s3 as your disk driver
$url = Storage::disk('s3')->temporaryUrl(
'file1.jpg', Carbon::now()->addMinutes(5)
);
这仅适用于 s3 存储驱动程序 AFAIK。
经过很多错误,我终于找到了使用以下代码访问 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();
上面的解释答案(@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), []);
这是将图像上传到 S3 并创建签名的完整代码 URL;
首先获取一个文件对象并创建对象名
$filename = $_FILES["image"]["name"];
$array = explode('.', $filename);
$fileExt = $array[1]; // to get file extension
$objectName = 'images/' . time() .$fileExt; // create object name
$document = fopen($_FILES["image"]["tmp_name"],'r');
// Code to upload document on s3
$s3 = \Storage::disk('s3');
$s3->put($objectName,$document,'public');
// If you want to get uploaded document Url you can use below code:
$image_name = \Storage::disk('s3')->url($name); // it will return stored document url
create/get 签名 URL 的代码,你也可以把它作为一个单独的函数:
$s3 = \Storage::disk('s3');
$client = $s3->getDriver()->getAdapter()->getClient();
$expiry = "+10 minutes";
$command = $client->getCommand('GetObject', [
'Bucket' => config('params.YOUR_AWS_BUCKET_NAME'), // bucket name
'Key' => $objectName
]);
$request = $client->createPresignedRequest($command, $expiry);
$url = (string) $request->getUri(); // it will return signed URL
$file_path = "profile/image.png";
$client = Storage::disk('s3')->getDriver()->getAdapter()->getClient();
$bucket = config('filesystems.disks.s3.bucket');
$command = $client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $file_path // file name in s3 bucket which you want to access
]);
$request = $client->createPresignedRequest($command, '+5 minutes');
return $presignedUrl = (string) $request->getUri(); // Get the actual presigned-url
对于 Laravel 9 显示受保护文件夹中的所有图像的示例:
$function = function ($image){
return Storage::disk('s3')->temporaryUrl(
$image, Carbon::now()->addMinutes(60)
);
};
$images = Storage::disk('s3')->allFiles('images/client');
return array_map($function,$images);