php - 如何在我的网站上显示来 self 的 Amazon S3 的图像?

标签 php amazon-web-services amazon-s3

我在 Amazon S3 上有我的照片。这些图片是私有(private)的而不是公开的,所以我无法通过直接链接显示它们 s3.amazonaws/bucket_name/key_name/image_name.jpg

我知道 Amazon S3 上的图像名称。我如何使用 PHP 在我的网站上显示 Amazon S3 图像 flower.png 和 house.png 等?

最佳答案

对于那些使用 AWS SDK v3 需要此行为的用户,如果您使用“+10 分钟”作为第三个参数直接调用 getObjectUrl,它将始终返回纯 URL。那是因为方法变了。要获取预签名链接,请执行以下操作:

//Get an instance of S3 Client. This is one one to do it:
$s3Client = new S3Client([
    'version'     => 'latest',
    'region'      => 'us-west-2', //Region of the bucket
    'credentials' => array(
        'key' => 'YOUR-ACCOUNT-KEY',
        'secret'  => 'YOUR-ACCOUNT-SECRET',
    )
]);

//Get a command to GetObject
$cmd = $s3Client->getCommand('GetObject', [
    'Bucket' => 'YOUR-BUCKET-NAME',
    'Key'    => 'YOUR-FILE-KEY'
]);

//The period of availability
$request = $s3Client->createPresignedRequest($cmd, '+10 minutes');

//Get the pre-signed URL
$signedUrl = (string) $request->getUri();

引用:https://docs.aws.amazon.com/aws-sdk-php/v3/guide/service/s3-presigned-url.html

关于php - 如何在我的网站上显示来 self 的 Amazon S3 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27768596/

相关文章:

php - 无法添加外键 Laravel

php - ResourceBundle 返回 NULL 而不会引发任何错误

amazon-web-services - 没有持续警报的AWS EC2自动扩展?

python - 如何将 X-Ray 与 SNS 一起使用以获得一个大链而不是许多小链?

python - Cloudfront 与 S3 签名 URL 和 Boto3

php - Node.js 或 PHP 中的模式识别算法?

php - 从 mysql 表中删除一行

amazon-web-services - 为什么 CloudFormation 不创建我的数据库?

ruby-on-rails - 回形针重新处理返回 true 但不更改文件夹结构

amazon-s3 - 尝试运行 aws cli 时 S3 访问被拒绝