Reu*_*ble 5 java amazon-s3 amazon-web-services aws-lambda
我没有使用Java获得以下逻辑来使用AWS Lambda:
1)当在S3存储桶中创建新对象时,触发lambda函数(用java编写)
2)在此lambda函数中,列出所有DynamoDB表.
3)如果没有,则创建一个表.
4)将S3对象的详细信息作为项目写入DynamoDB.
我只得到第1项工作.当它到达第2项时,我在下面遇到与权限相关的错误.
任何帮助或建议?
我使用的权限是"Basic with DynamoDB",它具有以下权限:
START请求ID:e9ab5aba-307B-11e5-9663-3188c327cf5e作品尺寸:1024,日期时间:1970-01-01T00:00:00.000Zs3Key:HappyFace.jpgAWS凭据配置文件的文件没有指定路径中找到:/home/sbx_user1052/.aws/凭据:java.lang.IllegalArgumentException异常java.lang.IllegalArgumentException异常:AWS凭据配置文件的文件没有指定路径中找到:/home/sbx_user1052/.aws/credentials在com.amazonaws.auth.profile.internal.ProfilesConfigFileLoader.loadProfiles(ProfilesConfigFileLoader.的java:45)在com.amazonaws.auth.profile.ProfilesConfigFile.loadProfiles(ProfilesConfigFile.java:176)在com.amazonaws.auth.profile.ProfilesConfigFile(ProfilesConfigFile.java:112)在com.amazonaws.auth.profile. ProfilesConfigFile.(ProfilesConfigFile.java:92)位于com.amazonaws.auth.profile.ProfileCredentialsProvider.getCredentials(ProfileCredentialsProvider.java:123)位于com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1763)的com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.listTables(AmazonDynamoDBClient.java:1208)com.amazonaws .services.dynamodbv2.document.internal.ListTablesCollection.firstPage(ListTablesCollection.java:46)在com.amazonaws.services.dynamodbv2.document.internal.PageIterator.next(PageIterator.java:45)在com.amazonaws.services.dynamodbv2 .document.internal.IteratorSupport.nextResource(IteratorSupport.java:79)在com.amazonaws.services.dynamodbv2.document.internal.IteratorSupport.hasNext(IteratorSupport.java:47)在com.TriggerDynamoDB.handleRequest(TriggerDynamoDB.java:68 )at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.在java.lang.reflect.Method.invoke(Method.java:497)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中的reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
END RequestId:e9ab5aba-307b-11e5-9663-3188c327cf5e REPORT RequestId:e9ab5aba-307b-11e5-9663-3188c327cf5e持续时间:3294.97 ms结算时长:3300 ms内存大小:512 MB最大使用内存:51 MB
代码如下:
public class TriggerDynamoDB implements RequestHandler<S3Event, String> {
public String handleRequest(S3Event s3event, Context context) {
LambdaLogger logger = context.getLogger();
try {
S3EventNotificationRecord record = s3event.getRecords().get(0);
// Object key may have spaces or unicode non-ASCII characters.
String srcKey = record.getS3().getObject().getKey().replace('+', ' ');
srcKey = URLDecoder.decode(srcKey, "UTF-8");
long fileSize = record.getS3().getObject().getSizeAsLong();
DateTime datetime = record.getEventTime();
logger.log("fileSize: " + fileSize + ", DateTime:" + datetime);
logger.log("s3Key : " + srcKey);
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(new ProfileCredentialsProvider()));
//Table table = dynamoDB.getTable("dimensionFile");
TableCollection<ListTablesResult> tables = dynamoDB.listTables();
Iterator<Table> iterator = tables.iterator();
while (iterator.hasNext()) { // this is where the error was thrown
Table table = iterator.next();
System.out.println(table.getTableName());
}
return "Ok";
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ray 13
该ProfileCredentialsProvider你传递给AmazonDynamoDBClient构造尝试从加载证书~/.aws/credentials文件.运行Lambda函数的主机上不存在此文件,因此例外.此提供程序主要用于在您的开发计算机上运行的集成测试以及使用您的个人IAM用户凭据.
在Lambda上,您需要EnvironmentVariableCredentialsProvider.此提供程序将从Lambda在调用代码之前设置的环境变量中加载与Lambda函数关联的IAM角色的临时凭证.
如果对AmazonDynamoDBClient使用无参数构造函数,则默认使用DefaultAWSCredentialsProviderChain来提取凭据.此提供程序检查环境变量,Java系统属性~/.aws/credentials和EC2实例元数据服务(按此顺序),并且无论您是在本地还是在Lambda上运行,都应该获取凭据.
| 归档时间: |
|
| 查看次数: |
4774 次 |
| 最近记录: |