Example usage for com.amazonaws.services.s3 AmazonS3Client setRegion

List of usage examples for com.amazonaws.services.s3 AmazonS3Client setRegion

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client setRegion.

Prototype

@Override
@Deprecated
public synchronized void setRegion(com.amazonaws.regions.Region region) 

Source Link

Usage

From source file:com.streamsets.datacollector.bundles.SupportBundleManager.java

License:Apache License

/**
 * Instead of providing support bundle directly to user, upload it to StreamSets backend services.
 *//*from   w ww.j  av  a 2  s  .co  m*/
public void uploadNewBundleFromInstances(List<BundleContentGenerator> generators, BundleType bundleType)
        throws IOException {
    // Generate bundle
    SupportBundle bundle = generateNewBundleFromInstances(generators, bundleType);

    boolean enabled = configuration.get(Constants.UPLOAD_ENABLED, Constants.DEFAULT_UPLOAD_ENABLED);
    String accessKey = configuration.get(Constants.UPLOAD_ACCESS, Constants.DEFAULT_UPLOAD_ACCESS);
    String secretKey = configuration.get(Constants.UPLOAD_SECRET, Constants.DEFAULT_UPLOAD_SECRET);
    String bucket = configuration.get(Constants.UPLOAD_BUCKET, Constants.DEFAULT_UPLOAD_BUCKET);
    int bufferSize = configuration.get(Constants.UPLOAD_BUFFER_SIZE, Constants.DEFAULT_UPLOAD_BUFFER_SIZE);

    if (!enabled) {
        throw new IOException("Uploading support bundles was disabled by administrator.");
    }

    AWSCredentialsProvider credentialsProvider = new StaticCredentialsProvider(
            new BasicAWSCredentials(accessKey, secretKey));
    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider, new ClientConfiguration());
    s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
    s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));

    // Object Metadata
    ObjectMetadata s3Metadata = new ObjectMetadata();
    for (Map.Entry<Object, Object> entry : getMetadata(bundleType).entrySet()) {
        s3Metadata.addUserMetadata((String) entry.getKey(), (String) entry.getValue());
    }

    List<PartETag> partETags;
    InitiateMultipartUploadResult initResponse = null;
    try {
        // Uploading part by part
        LOG.info("Initiating multi-part support bundle upload");
        partETags = new ArrayList<>();
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket,
                bundle.getBundleKey());
        initRequest.setObjectMetadata(s3Metadata);
        initResponse = s3Client.initiateMultipartUpload(initRequest);
    } catch (AmazonClientException e) {
        LOG.error("Support bundle upload failed: ", e);
        throw new IOException("Support bundle upload failed", e);
    }

    try {
        byte[] buffer = new byte[bufferSize];
        int partId = 1;
        int size = -1;
        while ((size = readFully(bundle.getInputStream(), buffer)) != -1) {
            LOG.debug("Uploading part {} of size {}", partId, size);
            UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucket)
                    .withKey(bundle.getBundleKey()).withUploadId(initResponse.getUploadId())
                    .withPartNumber(partId++).withInputStream(new ByteArrayInputStream(buffer))
                    .withPartSize(size);

            partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
        }

        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucket,
                bundle.getBundleKey(), initResponse.getUploadId(), partETags);

        s3Client.completeMultipartUpload(compRequest);
        LOG.info("Support bundle upload finished");
    } catch (Exception e) {
        LOG.error("Support bundle upload failed", e);
        s3Client.abortMultipartUpload(
                new AbortMultipartUploadRequest(bucket, bundle.getBundleKey(), initResponse.getUploadId()));

        throw new IOException("Can't upload support bundle", e);
    } finally {
        // Close the client
        s3Client.shutdown();
    }
}

From source file:com.upplication.s3fs.S3FileSystemProvider.java

License:Open Source License

protected S3FileSystem createFileSystem0(URI uri, Object accessKey, Object secretKey, Object sessionToken) {
    AmazonS3Client client;
    ClientConfiguration config = createClientConfig(props);

    if (accessKey == null && secretKey == null) {
        client = new AmazonS3Client(new com.amazonaws.services.s3.AmazonS3Client(config));
    } else {/*from   www . ja v a2 s.  c om*/

        AWSCredentials credentials = (sessionToken == null
                ? new BasicAWSCredentials(accessKey.toString(), secretKey.toString())
                : new BasicSessionCredentials(accessKey.toString(), secretKey.toString(),
                        sessionToken.toString()));
        client = new AmazonS3Client(new com.amazonaws.services.s3.AmazonS3Client(credentials, config));
    }

    // note: path style access is going to be deprecated
    // https://aws.amazon.com/blogs/aws/amazon-s3-path-deprecation-plan-the-rest-of-the-story/
    boolean usePathStyle = "true".equals(props.getProperty("s_3_path_style_access"))
            || "true".equals(props.getProperty("s3_path_style_access"));
    if (usePathStyle) {
        S3ClientOptions options = S3ClientOptions.builder().setPathStyleAccess(usePathStyle).build();
        client.client.setS3ClientOptions(options);
    }

    if (uri.getHost() != null) {
        client.setEndpoint(uri.getHost());
    } else if (props.getProperty("endpoint") != null) {
        client.setEndpoint(props.getProperty("endpoint"));
    } else if (props.getProperty("region") != null) {
        client.setRegion(props.getProperty("region"));
    }

    S3FileSystem result = new S3FileSystem(this, client, uri.getHost());
    return result;
}

From source file:com.yahoo.athenz.zts.store.CloudStore.java

License:Apache License

public AmazonS3 getS3Client() {

    if (!awsEnabled) {
        throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR, "AWS Support not enabled");
    }/* w w  w.j a  v a2 s  . c o m*/

    if (credentials == null) {
        throw new ResourceException(ResourceException.INTERNAL_SERVER_ERROR,
                "AWS Role credentials are not available");
    }

    AmazonS3Client s3 = new AmazonS3Client(credentials);
    if (awsRegion != null) {
        s3.setRegion(Region.getRegion(Regions.fromName(awsRegion)));
    }
    return s3;
}

From source file:org.apache.gobblin.aws.AWSSdkClient.java

License:Apache License

/***
 * Initialize the AWS SDK Client//  w w  w. j  a  va 2 s.com
 *
 * @param awsClusterSecurityManager The {@link AWSClusterSecurityManager} to fetch AWS credentials
 * @param region The Amazon AWS {@link Region}
 */
public AWSSdkClient(final AWSClusterSecurityManager awsClusterSecurityManager, final Region region) {
    this.amazonEC2Supplier = Suppliers.memoize(new Supplier<AmazonEC2>() {
        @Override
        public AmazonEC2 get() {
            AmazonEC2Client amazonEC2 = new AmazonEC2Client(awsClusterSecurityManager.getCredentialsProvider());
            amazonEC2.setRegion(region);
            return amazonEC2;
        }
    });
    this.amazonS3Supplier = Suppliers.memoize(new Supplier<AmazonS3>() {
        @Override
        public AmazonS3 get() {
            AmazonS3Client amazonS3 = new AmazonS3Client(awsClusterSecurityManager.getCredentialsProvider());
            amazonS3.setRegion(region);
            return amazonS3;
        }
    });
    this.amazonAutoScalingSupplier = Suppliers.memoize(new Supplier<AmazonAutoScaling>() {
        @Override
        public AmazonAutoScaling get() {
            AmazonAutoScalingClient amazonAutoScaling = new AmazonAutoScalingClient(
                    awsClusterSecurityManager.getCredentialsProvider());
            amazonAutoScaling.setRegion(region);
            return amazonAutoScaling;
        }
    });
}

From source file:org.apache.oodt.cas.filemgr.datatransfer.S3DataTransfererFactory.java

License:Apache License

@Override
public S3DataTransferer createDataTransfer() {
    String bucketName = System.getProperty(BUCKET_NAME_PROPERTY);
    String region = System.getProperty(REGION_PROPERTY);
    String accessKey = System.getProperty(ACCESS_KEY_PROPERTY);
    String secretKey = System.getProperty(SECRET_KEY_PROPERTY);
    boolean encrypt = Boolean.getBoolean(ENCRYPT_PROPERTY);

    AmazonS3Client s3 = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
    s3.setRegion(Region.getRegion(Regions.valueOf(region)));

    return new S3DataTransferer(s3, bucketName, encrypt);
}

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

protected AmazonS3Client getAmazonS3Client(S3Configuration s3config) {
    AmazonS3Client client = configClientMap.get(s3config);
    if (client == null) {
        client = new AmazonS3Client(getAWSCredentials(s3config));
        client.setRegion(s3config.getDefaultBucketRegion());

        if (s3config.getEndpointURI() != null) {
            client.setEndpoint(s3config.getEndpointURI());
        }/*from www  .  jav  a  2s.  com*/
        configClientMap.put(s3config, client);
    }
    return client;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 *  Region? Amazon S3 Client ?./* www. j  a va2s . c  om*/
 *
 * @param region    Amazon S3 Region
 * @param accessKey Amazon S3 Access Key
 * @param secretKey Amazon S3 Secret Key
 * @return Amazon S3 Client
 */
public static AmazonS3Client getAmazonS3Client(String region, String accessKey, String secretKey) {
    Region awsRegion = Region.getRegion(Regions.valueOf(region));
    AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3Client awsClient = new AmazonS3Client(awsCredentials);
    awsClient.setRegion(awsRegion);
    return awsClient;
}

From source file:org.xmlsh.aws.gradle.s3.AmazonS3PluginExtension.java

License:BSD License

private AmazonS3 initClient() {
    AwsPluginExtension aws = project.getExtensions().getByType(AwsPluginExtension.class);

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    if (maxErrorRetry > 0)
        clientConfiguration.setMaxErrorRetry(maxErrorRetry);

    AmazonS3Client client = aws.createClient(AmazonS3Client.class, profileName, clientConfiguration);
    if (region != null) {
        client.setRegion(RegionUtils.getRegion(region));
    }//from  w  w  w. j  ava  2s . c  o m
    return client;
}

From source file:org.zalando.stups.fullstop.controller.S3Controller.java

License:Apache License

@RequestMapping(method = RequestMethod.GET, value = "/download")
public void downloadFiles(@RequestParam(value = "bucket") final String bucket,
        @RequestParam(value = "location") final String location, @RequestParam(value = "page") final int page) {

    try {//from ww  w.  ja  va2  s.c  o  m
        log.info("Creating fullstop directory here: {}", fullstopLoggingDir);

        boolean mkdirs = new File(fullstopLoggingDir).mkdirs();
    } catch (SecurityException e) {
        // do nothing
    }

    AmazonS3Client amazonS3Client = new AmazonS3Client();
    amazonS3Client.setRegion(Region.getRegion(Regions
            .fromName((String) cloudTrailProcessingLibraryProperties.getAsProperties().get(S3_REGION_KEY))));

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) //
            .withPrefix(location) //
            .withMaxKeys(page);

    ObjectListing objectListing = amazonS3Client.listObjects(listObjectsRequest);

    final List<S3ObjectSummary> s3ObjectSummaries = objectListing.getObjectSummaries();

    while (objectListing.isTruncated()) {

        objectListing = amazonS3Client.listNextBatchOfObjects(objectListing);
        s3ObjectSummaries.addAll(objectListing.getObjectSummaries());

    }

    for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
        String bucketName = s3ObjectSummary.getBucketName();
        String key = s3ObjectSummary.getKey();

        S3Object object = amazonS3Client.getObject(new GetObjectRequest(bucketName, key));
        InputStream inputStream = object.getObjectContent();

        File file = new File(fullstopLoggingDir,
                object.getBucketName() + object.getObjectMetadata().getETag() + JSON_GZ);

        copyInputStreamToFile(inputStream, file);
        log.info("File saved here: {}", file.getAbsolutePath());

    }
}

From source file:ti.aws.S3TransferManagerProxy.java

License:Open Source License

@Kroll.method
public void upload(KrollDict params) {
    if (credentialsProvider == null) {
        Log.e(LCAT, "Missing credentials provider! Please use 'configure(args)' before.");
        return;//from w  ww. j  av a  2 s  . co  m
    }

    final String file = params.getString("file");
    final String bucket = params.getString("bucket");
    final String key = params.getString("key");

    final KrollFunction success = (KrollFunction) params.get("success");
    final KrollFunction error = (KrollFunction) params.get("error");

    Context appContext = TiApplication.getInstance();
    if (appContext == null)
        return;

    AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider);
    s3Client.setRegion(Region.getRegion(Regions.fromName("eu-central-1")));

    TransferUtility transferUtility = TransferUtility.builder().context(appContext)
            .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
            .s3Client(new AmazonS3Client(AWSMobileClient.getInstance().getCredentialsProvider())).build();

    File nativeFile = new File(Uri.parse(file).getPath());

    TransferObserver uploadObserver = transferUtility.upload(key, nativeFile);

    uploadObserver.setTransferListener(new TransferListener() {
        @Override
        public void onStateChanged(int id, TransferState state) {
            KrollObject krollObject = getKrollObject();
            if (krollObject == null) {
                return;
            }

            if (TransferState.COMPLETED == state) {
                KrollDict event = new KrollDict();
                event.put("body", key);

                success.callAsync(krollObject, event);
            }
        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
        }

        @Override
        public void onError(int id, Exception ex) {
            KrollObject krollObject = getKrollObject();
            if (krollObject == null) {
                return;
            }

            KrollDict event = new KrollDict();
            event.put("error", ex.getMessage());

            error.callAsync(krollObject, event);
        }
    });
}