EC2 和 Lambda 拒绝 AWS S3 访问
AWS S3 Access Denied from EC2 and Lambda
我正在尝试从 S3 存储桶中获取或列出文件。存储桶设置为无私有访问权限,未添加特定权限。
我正在尝试从配置了具有完全 S3 访问权限的角色的 EC2 进行访问,这在以前有效。
我也在尝试从 Lambda 访问,配置了一个具有完全 S3 访问权限的角色,这是新的,以前从未工作过。
根据 IAM 模拟器,这应该是允许的。
这是我的 Lambda (python) 的摘录:
进口json
导入boto3
从日期时间导入日期时间
def lambda_handler(事件,上下文):
bucket = 'mybucketname' # this the name itself, no url or arn or anything
# check if file exists
s3client = boto3.client('s3')
key = 'mypath/' + 'anotherbitofpath' + '/' + 'index.html'
print(f"key = {key}")
objs = s3client.list_objects_v2(
Bucket=bucket,
Prefix=key
)
print(f"objs = {objs}")
if any([w.key == path_s3 for w in objs]):
print("Exists!")
else:
print("Doesn't exist")
非常感谢
更新存储桶策略,使其将 Lambda 函数的 IAM 角色(执行角色)的 ARN 指定为有权访问操作 s3:GetObject 的委托人。您可以使用类似于以下的存储桶策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::YourAWSAccount:role/AccountARole"
},
"Action": [
"s3:GetObject",
"s3:GetObjectVersion"
],
"Resource": [
"arn:aws:s3:::YourBucketName/*"
]
}
]
}
我实现了这个确切的用例。我可以从 Lambda 函数访问 S3 对象。唯一的区别是我在 Java 中实现了我的代码。这种标记对象的方法在 Lambda 函数中非常有效。
private void tagExistingObject(S3Client s3, String bucketName, String key, String label, String LabelValue) {
try {
GetObjectTaggingRequest getObjectTaggingRequest = GetObjectTaggingRequest.builder()
.bucket(bucketName)
.key(key)
.build();
GetObjectTaggingResponse response = s3.getObjectTagging(getObjectTaggingRequest);
// Get the existing immutable list - cannot modify this list.
List<Tag> existingList = response.tagSet();
ArrayList<Tag> newTagList = new ArrayList(new ArrayList<>(existingList));
// Create a new tag.
Tag myTag = Tag.builder()
.key(label)
.value(LabelValue)
.build();
// push new tag to list.
newTagList.add(myTag);
Tagging tagging = Tagging.builder()
.tagSet(newTagList)
.build();
PutObjectTaggingRequest taggingRequest = PutObjectTaggingRequest.builder()
.key(key)
.bucket(bucketName)
.tagging(tagging)
.build();
s3.putObjectTagging(taggingRequest);
System.out.println(key + " was tagged with " + label);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
我使用的角色具有对 S3 的完全访问权限,并且从 Lambda 函数执行 S3 操作没有问题。
我正在尝试从 S3 存储桶中获取或列出文件。存储桶设置为无私有访问权限,未添加特定权限。
我正在尝试从配置了具有完全 S3 访问权限的角色的 EC2 进行访问,这在以前有效。
我也在尝试从 Lambda 访问,配置了一个具有完全 S3 访问权限的角色,这是新的,以前从未工作过。
根据 IAM 模拟器,这应该是允许的。
这是我的 Lambda (python) 的摘录:
进口json 导入boto3 从日期时间导入日期时间
def lambda_handler(事件,上下文):
bucket = 'mybucketname' # this the name itself, no url or arn or anything
# check if file exists
s3client = boto3.client('s3')
key = 'mypath/' + 'anotherbitofpath' + '/' + 'index.html'
print(f"key = {key}")
objs = s3client.list_objects_v2(
Bucket=bucket,
Prefix=key
)
print(f"objs = {objs}")
if any([w.key == path_s3 for w in objs]):
print("Exists!")
else:
print("Doesn't exist")
非常感谢
更新存储桶策略,使其将 Lambda 函数的 IAM 角色(执行角色)的 ARN 指定为有权访问操作 s3:GetObject 的委托人。您可以使用类似于以下的存储桶策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::YourAWSAccount:role/AccountARole"
},
"Action": [
"s3:GetObject",
"s3:GetObjectVersion"
],
"Resource": [
"arn:aws:s3:::YourBucketName/*"
]
}
]
}
我实现了这个确切的用例。我可以从 Lambda 函数访问 S3 对象。唯一的区别是我在 Java 中实现了我的代码。这种标记对象的方法在 Lambda 函数中非常有效。
private void tagExistingObject(S3Client s3, String bucketName, String key, String label, String LabelValue) {
try {
GetObjectTaggingRequest getObjectTaggingRequest = GetObjectTaggingRequest.builder()
.bucket(bucketName)
.key(key)
.build();
GetObjectTaggingResponse response = s3.getObjectTagging(getObjectTaggingRequest);
// Get the existing immutable list - cannot modify this list.
List<Tag> existingList = response.tagSet();
ArrayList<Tag> newTagList = new ArrayList(new ArrayList<>(existingList));
// Create a new tag.
Tag myTag = Tag.builder()
.key(label)
.value(LabelValue)
.build();
// push new tag to list.
newTagList.add(myTag);
Tagging tagging = Tagging.builder()
.tagSet(newTagList)
.build();
PutObjectTaggingRequest taggingRequest = PutObjectTaggingRequest.builder()
.key(key)
.bucket(bucketName)
.tagging(tagging)
.build();
s3.putObjectTagging(taggingRequest);
System.out.println(key + " was tagged with " + label);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
我使用的角色具有对 S3 的完全访问权限,并且从 Lambda 函数执行 S3 操作没有问题。