从S3存储桶访问时是否存在java.io.FileNotFoundException?

时间:2018-05-01 07:56:09

标签: java amazon-web-services aws-lambda s3-bucket

我正在AWS中做一个小POC。我尝试从S3存储桶读取csv文件并显示在CloudWatch日志文件中。一切都很顺利,但访问文件时得到java.io.FileNotFoundException

编码

public class LambdaFunctionHandler implements RequestHandler<S3Event, Report> {

Region AWS_REGION = Region.getRegion(Regions.US_EAST_1);

public Report handleRequest(S3Event s3event, Context context) {
    long startTime = System.currentTimeMillis();
    Report statusReport = new Report();
    LambdaLogger logger = context.getLogger();

    logger.log("Lambda Function Started");
    logger.log("I am inside lambda function");
    Helper helper = new Helper();

    try {
        logger.log("I am inside lambda function2");
        S3EventNotificationRecord record = s3event.getRecords().get(0);
        logger.log("I am inside lambda function3");
        String srcBucket = record.getS3().getBucket().getName();
        logger.log("I am inside lambda function4");
        // Object key may have spaces or unicode non-ASCII characters.
        String srcKey = record.getS3().getObject().getKey().replace('+', ' ');
        logger.log("I am inside lambda function5");
        srcKey = URLDecoder.decode(srcKey, "UTF-8");
        logger.log("I am inside lambda function6");

        AmazonS3 s3Client = new AmazonS3Client();
        logger.log("I am inside lambda function7");
        S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
        logger.log("I am inside lambda function8");
        statusReport.setFileSize(s3Object.getObjectMetadata().getContentLength());
        logger.log("I am inside lambda function9");

        logger.log("S3 Event Received: " + srcBucket + "/" + srcKey);
        logger.log("I am inside lambda function10");

        File file = new File(srcBucket+"/"+srcKey);
        try {
            Scanner readData =new Scanner(file);
            while(readData.hasNext()) {
                String data = readData.next();
                logger.log("I am inside lambda function data ;;got it");
                //System.out.println("data"+data);
            }
            readData.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }

CloudWatch日志文件输出

enter image description here

CloudWatch日志错误

我在lambda函数中

我在lambda function2

里面

我在lambda function3

里面

我在lambda function4

里面

我在lambda function5

里面

我在lambda function6里面

我在lambda function7

里面

我在lambda function8

里面

我在lambda function9

里面

收到S3事件:readfilefromcsvfile / def_nhtsa_crash_test.csv

我在lambda function10

里面

java.io.FileNotFoundException:

readfilefromcsvfile / def_nhtsa_crash_test.csv(没有这样的文件或目录) java.io.FileNotFoundException:readfilefromcsvfile / def_nhtsa_crash_test.csv(没有这样的文件或目录) 在java.io.FileInputStream.open0(本机方法)

如果我们查看日志输出,它会告诉我没有这样的文件,但我把它保存在S3存储桶中。谁能告诉我如何在java中访问S3文件?或访问S3存储文件时应该给出的路径名是什么?

1 个答案:

答案 0 :(得分:2)

此处无需创建File实例。您可以简单地处理S3 InputStream。

S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
....
Scanner scanner = new Scanner(s3Object.getObjectContent());
while (scanner.hasNext()) {
    System.out.println(scanner.next());
}