Reputation: 141
i am rephrasing my question again because got devoted for not posting clearly . So i have a folder where i will be getting many files in minutes . I need to keep on watching that folder and as soon as file arrives i have to upload into S3 . The size of the files would be max 10 KB but frequency will be high .
Here is my code that watches the folder and start S3 upload . But my issue is how can i pass file from file watch class to S3 uploader .
Here is my code to watch Folder
public class MonitorDirectory {
public static void main(String[] args) throws IOException, InterruptedException {
Path faxFolder = Paths.get("C:\\Users\\u6034690\\Desktop\\ONEFILE\\");
WatchService watchService = FileSystems.getDefault().newWatchService();
faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
boolean valid = true;
do {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
WatchEvent.Kind kind = event.kind();
if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
String fileName = event.context().toString();
UploadObject onj=new UploadObject();//How to pass file path here
onj.uploadIntoS3();
System.out.println("File Created:" + fileName);
}
}
valid = watchKey.reset();
} while (valid);
}
}
Here is my class where i upload file into S3 which works fine now
public class UploadObject {
public void uploadIntoS3() throws IOException, InterruptedException {
String bucketName = "a205381-auditxml/S3UPLOADER";
try {
AmazonS3 s3client = new AmazonS3Client();
s3client.putObject(bucketName, "hello.txt",
new File("C:\\Users\\u6034690\\Desktop\\ONEFILE\\hello.txt"));
System.out.println("Uploaded onject");
} catch (AmazonServiceException e) {
e.printStackTrace();
} catch (SdkClientException e) {
e.printStackTrace();
}
}
}
So how to pass file path from MonitorDirectory
class to onj.uploadIntoS3();
Also one last check is my code going to sustain huge amount of file upload frequency .
Upvotes: 1
Views: 507
Reputation: 1585
Get path like below inside for loop
final Path createdFile = (Path) event.context();
final Path expectedAbsolutePath = watchedDirectory.resolve(createdFile);
String fileName = event.context().toString();
System.out.println("Full path: " + expectedAbsolutePath);
UploadObject obj=new UploadObject();//How to pass file path here
and then pass in S3 uploader
obj.uploadIntoS3(expectedAbsolutePath.toFile());
Upvotes: 1