【发布时间】:2020-05-20 15:59:50
【问题描述】:
我不确定我是否错过了这里的步骤。
我有一个 s3 存储桶,我需要能够从我编写的在我的 EC2 上运行的 AWS 开发工具包 PHP 脚本进行访问。我创建了一个 IAM 角色以允许访问。
IAM Allow_S3_Access_to_EC2
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::myinbox"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::myinbox/*"
},
{
"Sid": "VisualEditor2",
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "*"
},
{
"Sid": "VisualEditor3",
"Effect": "Allow",
"Action": "s3:DeleteObject",
"Resource": "arn:aws:s3:::myinbox/*"
}
]
}
我对 IAM 角色的信任关系是
信任关系
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
然后,我将该 IAM 角色附加到我的 EC2 实例。从我所读到的内容来看,这就是我所要做的,但我认为我还需要做更多。
在我的存储桶策略中,我有以下内容允许从我的 SES 访问以创建电子邮件对象。
S3 存储桶政策
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowSESPuts",
"Effect": "Allow",
"Principal": {
"Service": "ses.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::myinbox/*",
"Condition": {
"StringEquals": {
"aws:Referer": "************"
}
}
},
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::************:role/Allow_S3_Access_to_EC2"
},
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::myinbox",
"arn:aws:s3:::myinbox/*"
]
}
]
}
对于我的 EC2 甚至我附加的 IAM 角色,我的存储桶策略中没有任何内容。我是否还需要在我的存储桶策略中添加一些内容?这就是我困惑的地方。
我遇到的是当创建一个新对象并尝试从我的 AWS 开发工具包 PHP 访问该对象时,我得到一个“403”禁止。如果我在 S3 控制台中公开该对象,我就可以正常访问它。因此,即使我已经为我的 EC2 设置了访问我的 S3 的权限,除非我将对象公开,否则我无法访问它。
我什至尝试通过终端对实际服务器上的对象使用wget,除非我将对象公开
当我在我的角色上运行 IAM 策略模拟器时,我得到了
这是我的 PHP
PHP 脚本
require '../aws-ses/aws-autoloader.php';
use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;
$bucketName = 'myinbox';
try {
// Instantiate the client.
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-west-2',
'credentials' => array('key'=>'*********************',
'secret'=>'*******************************************')
]);
} catch (Exception $e) {
// We use a die, so if this fails. It stops here. Typically this is a REST call so this would
// return a json object.
die("Error: " . $e->getMessage());
}
// Use the high-level iterators (returns ALL of your objects).
$objects = $s3->getIterator('ListObjects', array('Bucket' => $bucketName));
【问题讨论】:
标签: amazon-web-services amazon-s3 amazon-ec2 amazon-iam