首页 > 解决方案 > 当文件继续写入目录时如何将目录中的文件上传到S3

问题描述

所以我将所有文件从一个目录上传到 S3 使用TransferManager ,我也可以上传。但是我在同一个目录文件中的问题也被写入了。那么我如何调用该方法写入 S3 。我必须以固定的时间间隔调用该方法吗?请建议调用该方法的最佳方法。

public void uploadDir(Path strFile,String strFileName){

        ArrayList<File> files = new ArrayList<File>();
        for (Path path : strFile) {
            files.add(new File(path.toString()));
        }

        TransferManager xfer_mgr = TransferManagerBuilder.standard().build();
        try {
            MultipleFileUpload xfer = xfer_mgr.uploadFileList(bucketName,strFileName, new File("."), files);
            //XferMgrProgress.showTransferProgress(xfer);
            //XferMgrProgress.waitForCompletion(xfer);
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }
    }

标签: javaamazon-web-servicesamazon-s3

解决方案


Couple of solutions, you could try any, based on your need.

Solution 1:- For scanrio like your, instead of time interval, you should be using fileAge.

FileAge:when the file was last modified, this common concept used by file Poller either local directory or remote directory.

So think like your files takes max. 20 seconds in writing, then only pull files older then 20s or more.

Solution 2:-

Other way is ask your clients, the program generating files to use some extension say .tmp, when file writing completed, ask them to convert it to actual file extension and modify your program to skip files with extension .tmp. e.g While writing abc.jpg to abc.jpg.tmp, when files writing completed, then rename it to abc.jpg.

Hope this helps.


推荐阅读