2

I am trying to upload a file with AWS SDK to demonstrate upload break due to any reason. But I see it is uploading from the beginning always. The code there is pretty straight-forward, but the problem comes with saving persistent obj status so that we can re-use in resuming upload.

Here is my code:

public class Upload2 {

private static final File RESUME_UPLOAD_INFO = new File(System.getProperty("user.home"), "resumeUploadFile");
private static final boolean UPLOAD_HALF_AND_KILL = !RESUME_UPLOAD_INFO.exists();

private static Upload upload;

public static void main(String[] args) throws Exception
{
    log("Execution started");
    String bucket = "itpc123412";
    String key = "s3.txt";
    String file_path = "C:\\Users\\rkompelli\\Desktop\\abc3.txt";
    File f = new File(file_path);

    TransferManager tm = AWSConfiguration.getConnectionTransfer(); 
    System.out.println(RESUME_UPLOAD_INFO.exists());
    if (UPLOAD_HALF_AND_KILL)
    {
        PutObjectRequest por = new PutObjectRequest(bucket, key,f);
        log("Starting from scratch");
        upload = tm.upload(por, LISTENER);         
    }
    else
    {
        log("Starting again");
        upload = tm.resumeUpload(PersistableUpload.deserializeFrom(new FileInputStream(RESUME_UPLOAD_INFO)));
        upload.addProgressListener(LISTENER);
    }

    log("Total bytes to transfer: " + upload.getProgress().getTotalBytesToTransfer());
    log("Bytes transferred: " + upload.getProgress().getBytesTransferred());

    upload.waitForCompletion();
    log("Done");

    tm.shutdownNow();
    if (upload.isDone())
        RESUME_UPLOAD_INFO.delete();

    System.exit(0);
}

private static void log(String msg)
{
    System.out.println(DateFormat.getTimeInstance().format(new Date()) + ": " + msg);
}

private static long LAST_LOGGED_BYTES;
private static final S3ProgressListener LISTENER = new S3SyncProgressListener()
{
    @Override
    public void progressChanged(ProgressEvent pe)
    {
        long bytes = upload.getProgress().getBytesTransferred();
        if (pe.getEventType().isByteCountEvent())
        {
            if (LAST_LOGGED_BYTES == 0 || bytes - LAST_LOGGED_BYTES > 5)
            {
                LAST_LOGGED_BYTES = bytes;
                log("uploaded: " + NumberFormat.getNumberInstance().format(bytes));
            }
        }

        if (bytes > upload.getProgress().getTotalBytesToTransfer()/2 && UPLOAD_HALF_AND_KILL) 
        { 
            log("IF 2 in get progress"); 
            log("Exiting");
            System.exit(0); 
        }

    }

    @Override
    public void onPersistableTransfer(final PersistableTransfer pt)
    {
        // TODO should not be blocked
        try
        {
            log("Saving upload state");
            pt.serialize(new FileOutputStream(RESUME_UPLOAD_INFO));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }           
};      
}

I noticed that onPersistableTransfer() event is not executing which has code to serialize the obj.

Raj
  • 21
  • 3

0 Answers0