创建Amazon AWS S3预签名URL PHP [英] Creating amazon aws s3 pre signed url PHP
问题描述
根据此链接 http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html ,我可以轻松地创建一个预签名链接,只需将寿命添加到getObjectUrl
According to this link http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html, I can easily create a presigned link just adding the life span to getObjectUrl
$signedUrl = $client->getObjectUrl($bucket, 'data.txt', '+10 minutes');
// > https://my-bucket.s3.amazonaws.com/data.txt?AWSAccessKeyId=[...]&Expires=[...]&Signature=[...]
但是,我得到一个简单的网址,没有awsaccesskeyid和expires参数,
But I get a plain url, you know, without the awsaccesskeyid and expires parameters,
这是我的代码:
$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
怎么了?
推荐答案
好吧,如果有人像我那样遇到任何麻烦,这就是答案,我进入了amazon php开发论坛并获得了专业人士的帮助
Well, if anyone else has any trouble with this like I did, here is the answer, I went into the amazon php development forums and got help from the profesionals.
似乎您可能在SDK的版本2和版本3之间切换,或者查看了错误的文档.确保您准备好要使用的产品,并正在查看正确的文档.他们是不同的.
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 -作曲家要求:{"aws/aws-sdk-php":〜3.0"} -用户指南: http://docs.aws.amazon.com/aws- sdk-php/v3/guide/index.html -API文档: http://docs.aws.amazon.com/aws- sdk-php/v3/api/index.html -预签名的URL文档: http://docs .aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html
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 -作曲家要求:{"aws/aws-sdk-php":〜2.8"} -用户指南: http://docs.aws.amazon.com/aws- sdk-php/v2/guide/index.html -API文档: http://docs.aws.amazon.com/aws- sdk-php/v2/api/index.html -预签名的URL文档: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-s3.html#creating-a-pre-signed-url
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
有关您必须做什么的小型分步指南:
Mini step-by-step guide of what you have to do:
1.安装作曲家,最好使用sudo:
1.Install composer, preferably using sudo:
sudo curl -sS https://getcomposer.org/installer | sudo php
2.转到您的项目文件夹并创建一个composer.json文件,其中包含您想要/需要的版本,您可以在此处找到发行版:
2.Go to your project folder and create a composer.json file, with the version you want/need, you can find releases here: https://github.com/aws/aws-sdk-php/releases, commands for each version seem to be very version specific, be careful, this was my main problem.
{
"require": {
"aws/aws-sdk-php": "~3.0"
}
}
3.然后转到终端中的项目文件夹,并通过composer安装sdk并随后进行更新,例如:(如果更改版本,则必须再次更新.)
3.Then go to your project folder in the terminal, and install sdk via composer and update afterward like: (if you change version you have to update again.)
sudo php composer.phar install
sudo php composer.phar update
4.然后,一切准备就绪,您可以遵循正确的版本文档,在我的情况下,版本为"aws/aws-sdk-php":〜3.0",而对于预签名的url,有效的方法是:
4.Then everything is ready for you to follow proper version documentation, in my case for version "aws/aws-sdk-php": "~3.0" and for presigned url, what worked was:
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;
我希望这可以帮助任何遇到与我相同的问题的人.
I hope this helps anyone facing the same problems as I did.
这篇关于创建Amazon AWS S3预签名URL PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!