Example usage for com.amazonaws.services.s3 AmazonS3ClientBuilder standard

List of usage examples for com.amazonaws.services.s3 AmazonS3ClientBuilder standard

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3ClientBuilder standard.

Prototype

public static AmazonS3ClientBuilder standard() 

Source Link

Usage

From source file:ubicrypt.core.provider.s3.S3Provider.java

License:Open Source License

@Override
public Observable<ProviderStatus> init(long userId) {
    return Observable.<ProviderStatus>create(subscriber -> {
        if (conf == null) {
            subscriber.onError(new RuntimeException("conf not specified"));
            return;
        }// w  w w . j  a  v  a 2s .  c o m
        try {
            AmazonS3ClientBuilder clientb = AmazonS3ClientBuilder.standard()
                    .withCredentials(new AWSCredentialsProvider() {
                        @Override
                        public AWSCredentials getCredentials() {
                            return new AWSCredentials() {
                                @Override
                                public String getAWSAccessKeyId() {
                                    return conf.getAccessKeyId();
                                }

                                @Override
                                public String getAWSSecretKey() {
                                    return conf.getSecrectKey();
                                }
                            };
                        }

                        @Override
                        public void refresh() {
                        }
                    });
            if (conf.getRegion() != null) {
                clientb.withRegion(conf.getRegion());
            }
            client = clientb.build();
            prefix = Long.toString(userId, MAX_RADIX) + "/";
            try {
                client.headBucket(new HeadBucketRequest(conf.getBucket()));
            } catch (AmazonS3Exception e) {
                switch (e.getErrorCode()) {
                case "404 Not Found":
                    client.createBucket(conf.getBucket());
                    break;
                default:
                    subscriber.onError(e);
                    return;
                }
            }
            maxKey.set(client.listObjects(conf.getBucket(), prefix).getObjectSummaries().stream().map(obj -> {
                try {
                    return Long.valueOf(substringAfter(obj.getKey(), prefix), MAX_RADIX);
                } catch (Exception e) {
                    return 0L;
                }
            }).max(Long::compareTo).orElse(0L));

            initialized.set(true);
            subscriber.onNext(ProviderStatus.initialized);
            subscriber.onCompleted();
        } catch (Exception e) {
            subscriber.onError(e);
        }
    }).subscribeOn(Schedulers.io());
}