I am doing a small POC in AWS. I trying to read a csv file from S3 bucket and displaying in the CloudWatch log files . Everything is going fine but while accessing the file getting java.io.FileNotFoundException
Coding
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 Log file output
CloudWatch Log Errors
I am inside lambda function
I am inside lambda function2
I am inside lambda function3
I am inside lambda function4
I am inside lambda function5
I am inside lambda function6
I am inside lambda function7
I am inside lambda function8
I am inside lambda function9
S3 Event Received: readfilefromcsvfile/def_nhtsa_crash_test.csv
I am inside lambda function10
java.io.FileNotFoundException:
readfilefromcsvfile/def_nhtsa_crash_test.csv (No such file or directory) java.io.FileNotFoundException: readfilefromcsvfile/def_nhtsa_crash_test.csv (No such file or directory) at java.io.FileInputStream.open0(Native Method)
If we will look into the log output it is telling that no such file, but I have kept it in S3 bucket. Can anyone tell me how to access S3 file in java ? or What is the path name I should give while accessing the S3 bucket file ?
There is no need to create a File instance here. You can simply work on the S3 InputStream.
S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
....
Scanner scanner = new Scanner(s3Object.getObjectContent());
while (scanner.hasNext()) {
System.out.println(scanner.next());
}