Skip to content

Instantly share code, notes, and snippets.

@miroslavign
Created May 29, 2017 15:36
Show Gist options
  • Save miroslavign/e4169b889ae90a7a4e205db500b10321 to your computer and use it in GitHub Desktop.
Save miroslavign/e4169b889ae90a7a4e205db500b10321 to your computer and use it in GitHub Desktop.
private Observable<SentToS3Event> getAmazonObservable(String newFilename) {
Observable<SentToS3Event> amazonObservable = Observable.defer(new Func0<Observable<SentToS3Event>>() {
@Override
public Observable<SentToS3Event> call() {
return Observable.create((ObservableEmitter<SentToS3Event> subscriber) -> {
final PutObjectRequest putObjectRequest = new PutObjectRequest(AmazonS3FetchParams.MY_BUCKET_NAME, newFilename, new java.io.File(fileURI))
.withCannedAcl(CannedAccessControlList.PublicRead)
.withStorageClass(StorageClass.Standard);
putObjectRequest.setGeneralProgressListener(new S3ProgressListener() {
@Override
public void onPersistableTransfer(PersistableTransfer persistableTransfer) {
}
@Override
public void progressChanged(ProgressEvent progressEvent) {
if (progressEvent.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
Logger.d("TRANSFER finished: " + progressEvent.getBytesTransferred() + " bytes");
SentToS3Event sentEvent = new SentToS3Event(fileName, fileURI, position,
0, from, nrRetry, messageDbId, getAmazonImageUrl(fileName));
subscriber.onNext(sentEvent);
subscriber.onComplete();
} else if (progressEvent.getEventCode() == ProgressEvent.FAILED_EVENT_CODE) {
SentToS3Event sentEvent = new SentToS3Event(fileName, fileURI, position,
-1, from, nrRetry, messageDbId, getAmazonImageUrl(fileName));
subscriber.onNext(sentEvent);
subscriber.onComplete();
}
}
});
PutObjectResult objectResult = s3Client.putObject(putObjectRequest);
});
}
});
return amazonObservable;
}
@miroslavign
Copy link
Author

    private Observable<SentToS3Event> getAmazonObservable(final String newFilename) {
        return Observable.create(observableEmitter -> {
            final PutObjectRequest putObjectRequest = new PutObjectRequest(AmazonS3FetchParams.MY_BUCKET_NAME, newFilename, new java.io.File(fileURI))
                    .withCannedAcl(CannedAccessControlList.PublicRead)
                    .withStorageClass(StorageClass.Standard);

            putObjectRequest.setGeneralProgressListener(new S3ProgressListener() {
                @Override
                public void onPersistableTransfer(PersistableTransfer persistableTransfer) {

                }

                @Override
                public void progressChanged(ProgressEvent progressEvent) {
                    if (progressEvent.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                        Logger.d("TRANSFER finished: " + progressEvent.getBytesTransferred() + " bytes");
                        SentToS3Event sentEvent = new SentToS3Event(fileName, fileURI, position,
                                0, from, nrRetry, messageDbId, getAmazonImageUrl(fileName));
                        observableEmitter.onNext(sentEvent);
                        observableEmitter.onComplete();
                    } else if (progressEvent.getEventCode() == ProgressEvent.FAILED_EVENT_CODE) {
                        SentToS3Event sentEvent = new SentToS3Event(fileName, fileURI, position,
                                -1, from, nrRetry, messageDbId, getAmazonImageUrl(fileName));
                        observableEmitter.onNext(sentEvent);
                        observableEmitter.onComplete();
                    }
                }
            });
            PutObjectResult objectResult = s3Client.putObject(putObjectRequest);
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment