简体   繁体   中英

How to convert S3object to Observable

I'm trying to implement a method with RxJava and Vert.x framework.

The method has Observable<String> as its return type, but when fetching some data from S3 bucket within the method bogy, it returns S3Object .

Now, I have to covert S3Object to Observable<string> . (I store JSON like abc.json in S3 bucket).

 public Observable<String> getCustomerFroms3(String orderId) {

    AWSCredentials credentials = new BasicAWSCredentials (
           "*************MIIVCCCA",
            "az4aBUp58x&&&&&&&&&&&&&&&&&&&3gUTw"
    );
    AmazonS3 s3client = AmazonS3ClientBuilder
            .standard()
            .withCredentials(new AWSStaticCredentialsProvider (credentials))
            .withRegion(Regions.AP_SOUTH_1)
            .build();

    String bucketName = "database8";
    S3Object s3Object = s3client.getObject(bucketName,orderId);
    System.out.println(s3Object);

}

How I could convert S3Object to Observable ?

A straightforward approach would be to use the Observable factory method to create simple instance out of the S3Object content:

public Observable<String> getCustomerFroms3(String orderId) {
    return Observable.create(
          s -> {
              try {
                  AWSCredentials credentials = new BasicAWSCredentials (
                        "*************MIIVCCCA",
                        "az4aBUp58x&&&&&&&&&&&&&&&&&&&3gUTw"
                  );
                  AmazonS3 s3client = AmazonS3ClientBuilder
                        .standard()
                        .withCredentials(new AWSStaticCredentialsProvider (credentials))
                        .withRegion(Regions.AP_SOUTH_1)
                        .build();

                  S3Object s3Object = s3client.getObject("database8", orderId);
                  s.onNext(IOUtils.toString(s3Object.getObjectContent()));
                  s.onComplete();
              } catch (Exception e) {
                  s.onError(e);
              }
          }
    );
}

Note that this is only to demonstrate the approach to create the Observable instance and should not be considered as an optimal option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM