创建亚马逊 aws s3 预签名 url PHP
Creating amazon aws s3 pre signed url PHP
根据这个 link http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html,我可以轻松地创建一个预签名 link 只需将生命周期添加到 getObjectUrl
$signedUrl = $client->getObjectUrl($bucket, 'data.txt', '+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...]
但是我得到一个普通的 url,你知道,没有 awsaccesskeyid 和 expires 参数,
这是我的代码:
$bucket = 'imagenesfc';
$keyname = 'NASimagenes/codigoBarraBoleto/1001000098.png';
$filepath = 'NASimagenes/codigoBarraBoleto';
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-1'
));
$signedUrl = $s3->getObjectUrl($bucket, $keyname,'+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...]
echo $signedUrl."<br>";
编辑:我有 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 作为环境变量
我的回声看起来像:
https://s3-us-west-1.amazonaws.com/imagenesfc/NASimagenes/codigoBarraBoleto/1001000098.png
怎么了?
好吧,如果其他人像我一样有任何问题,这就是答案,我进入了亚马逊 php 开发论坛并得到了专业人士的帮助。
It seems you may be flip-flopping between Version 2 and Version 3 of the SDK or looking at the wrong document. Make sure you are getting the one you intend to use and are looking at the correct documentation. They are different.
V3
- Composer Requirement: {"aws/aws-sdk-php": "~3.0"}
- User Guide: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/index.html
- API Docs: http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html
- Pre-signed URL Docs: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html
V2
- Composer Requirement: {"aws/aws-sdk-php": "~2.8"}
- User Guide: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/index.html
- API Docs: http://docs.aws.amazon.com/aws-sdk-php/v2/api/index.html
- Pre-signed URL Docs: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#creating-a-pre-signed-url
关于您必须做什么的迷你分步指南:
1.Install 作曲家,最好使用 sudo:
sudo curl -sS https://getcomposer.org/installer | sudo php
2.Go 到你的项目文件夹并创建一个 composer.json 文件,你的版本是 want/need,你可以在这里找到发布:https://github.com/aws/aws-sdk-php/releases,每个版本的命令似乎是非常特定于版本的,要小心,这是我的主要问题。
{
"require": {
"aws/aws-sdk-php": "~3.0"
}
}
3.Then 进入终端中的项目文件夹,通过 composer 安装 sdk,然后像这样更新:(如果更改版本,则必须再次更新。)
sudo php composer.phar install
sudo php composer.phar update
4.Then 一切都准备就绪,您可以按照正确的版本文档进行操作,在我的情况下,版本 "aws/aws-sdk-php":“~3.0” 和预签名 url,有效的是:
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$sharedConfig = [
'region' => 'us-west-1',
'version' => 'latest'
]; //I have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables
$s3Client = new Aws\S3\S3Client($sharedConfig);
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $keyname
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();
echo $presignedUrl;
我希望这对遇到与我相同问题的人有所帮助。
如果您使用的是 Laravel,您可以轻松获得 S3
的临时 URL,如此处记录:https://laravel.com/docs/master/filesystem#file-urls
您的 Image
模型示例为:
class Image extends Model
{
...
public function getTemporaryUrlAttribute()
{
return Storage::disk('s3')->temporaryUrl(
$this->path,
now()->addSeconds(10)
);
}
}
那么在您的 HTML 中它将是:
<img width="100px" src="{{$image->temporaryUrl}}">
根据这个 link http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html,我可以轻松地创建一个预签名 link 只需将生命周期添加到 getObjectUrl
$signedUrl = $client->getObjectUrl($bucket, 'data.txt', '+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...]
但是我得到一个普通的 url,你知道,没有 awsaccesskeyid 和 expires 参数,
这是我的代码:
$bucket = 'imagenesfc';
$keyname = 'NASimagenes/codigoBarraBoleto/1001000098.png';
$filepath = 'NASimagenes/codigoBarraBoleto';
// Instantiate the client.
$s3 = S3Client::factory(array(
'version' => 'latest',
'region' => 'us-west-1'
));
$signedUrl = $s3->getObjectUrl($bucket, $keyname,'+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...]
echo $signedUrl."<br>";
编辑:我有 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 作为环境变量
我的回声看起来像:
https://s3-us-west-1.amazonaws.com/imagenesfc/NASimagenes/codigoBarraBoleto/1001000098.png
怎么了?
好吧,如果其他人像我一样有任何问题,这就是答案,我进入了亚马逊 php 开发论坛并得到了专业人士的帮助。
It seems you may be flip-flopping between Version 2 and Version 3 of the SDK or looking at the wrong document. Make sure you are getting the one you intend to use and are looking at the correct documentation. They are different.
V3 - Composer Requirement: {"aws/aws-sdk-php": "~3.0"} - User Guide: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/index.html - API Docs: http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html - Pre-signed URL Docs: http://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html
V2 - Composer Requirement: {"aws/aws-sdk-php": "~2.8"} - User Guide: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/index.html - API Docs: http://docs.aws.amazon.com/aws-sdk-php/v2/api/index.html - Pre-signed URL Docs: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#creating-a-pre-signed-url
关于您必须做什么的迷你分步指南:
1.Install 作曲家,最好使用 sudo:
sudo curl -sS https://getcomposer.org/installer | sudo php
2.Go 到你的项目文件夹并创建一个 composer.json 文件,你的版本是 want/need,你可以在这里找到发布:https://github.com/aws/aws-sdk-php/releases,每个版本的命令似乎是非常特定于版本的,要小心,这是我的主要问题。
{
"require": {
"aws/aws-sdk-php": "~3.0"
}
}
3.Then 进入终端中的项目文件夹,通过 composer 安装 sdk,然后像这样更新:(如果更改版本,则必须再次更新。)
sudo php composer.phar install
sudo php composer.phar update
4.Then 一切都准备就绪,您可以按照正确的版本文档进行操作,在我的情况下,版本 "aws/aws-sdk-php":“~3.0” 和预签名 url,有效的是:
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$sharedConfig = [
'region' => 'us-west-1',
'version' => 'latest'
]; //I have AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as environment variables
$s3Client = new Aws\S3\S3Client($sharedConfig);
$cmd = $s3Client->getCommand('GetObject', [
'Bucket' => $bucket,
'Key' => $keyname
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
$presignedUrl = (string) $request->getUri();
echo $presignedUrl;
我希望这对遇到与我相同问题的人有所帮助。
如果您使用的是 Laravel,您可以轻松获得 S3
的临时 URL,如此处记录:https://laravel.com/docs/master/filesystem#file-urls
您的 Image
模型示例为:
class Image extends Model
{
...
public function getTemporaryUrlAttribute()
{
return Storage::disk('s3')->temporaryUrl(
$this->path,
now()->addSeconds(10)
);
}
}
那么在您的 HTML 中它将是:
<img width="100px" src="{{$image->temporaryUrl}}">