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

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

Introduction

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

Prototype

@Override
@Deprecated
public synchronized void setEndpoint(String endpoint) 

Source Link

Usage

From source file:com.kpbird.aws.Main.java

public void createS3() {

    try {//from   w ww .ja  v  a2 s.c  om
        AmazonS3Client s3 = new AmazonS3Client(credentials);
        s3.setEndpoint(endPoint);
        s3.setRegion(region);
        log.Info("Creating Bucket :" + BucketName);

        s3.createBucket(BucketName);
        log.Info("Policy :" + BucketPolicy);
        s3.setBucketPolicy(BucketName, BucketPolicy);

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:com.mesosphere.dcos.cassandra.executor.backup.S3StorageDriver.java

License:Apache License

private AmazonS3Client getAmazonS3Client(BackupRestoreContext ctx) throws URISyntaxException {
    final String accessKey = ctx.getAccountId();
    final String secretKey = ctx.getSecretKey();
    String endpoint = getEndpoint(ctx);
    LOGGER.info("endpoint: {}", endpoint);

    final BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
    final AmazonS3Client amazonS3Client = new AmazonS3Client(basicAWSCredentials);
    amazonS3Client.setEndpoint(endpoint);

    if (ctx.usesEmc()) {
        final S3ClientOptions options = new S3ClientOptions();
        options.setPathStyleAccess(true);
        amazonS3Client.setS3ClientOptions(options);
    }//  w  w w. ja va2 s  .co  m

    return amazonS3Client;
}

From source file:com.netflix.exhibitor.core.s3.S3ClientImpl.java

License:Apache License

private AmazonS3Client createClient(AWSCredentialsProvider awsCredentialProvider,
        BasicAWSCredentials basicAWSCredentials, S3ClientConfig clientConfig) {
    AmazonS3Client localClient;
    if (awsCredentialProvider != null) {
        if (clientConfig != null) {
            localClient = new AmazonS3Client(awsCredentialProvider, clientConfig.getAWSClientConfig());
        } else {//from  www. j  a  va  2  s. c  om
            localClient = new AmazonS3Client(awsCredentialProvider);
        }
    } else if (basicAWSCredentials != null) {
        if (clientConfig != null) {
            localClient = new AmazonS3Client(basicAWSCredentials, clientConfig.getAWSClientConfig());
        } else {
            localClient = new AmazonS3Client(basicAWSCredentials);
        }
    } else {
        if (clientConfig != null) {
            localClient = new AmazonS3Client(clientConfig.getAWSClientConfig());
        } else {
            localClient = new AmazonS3Client();
        }
    }

    if (s3Region != null) {
        String fixedRegion = s3Region.equals("us-east-1") ? "" : ("-" + s3Region);
        String endpoint = ENDPOINT_SPEC.replace("$REGION$", fixedRegion);
        localClient.setEndpoint(endpoint);
        log.info("Setting S3 endpoint to: " + endpoint);
    }

    return localClient;
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileProvider.java

License:Apache License

/**
 * Create FileSystem event hook//  w  w  w. j a  v  a  2  s  .c  o  m
 * 
 * @param rootName
 * @param fileSystemOptions
 * @return
 * @throws FileSystemException 
 */
@Override
protected FileSystem doCreateFileSystem(FileName rootName, FileSystemOptions fileSystemOptions)
        throws FileSystemException {
    SS3FileSystem fileSystem = null;
    GenericFileName genRootName = (GenericFileName) rootName;

    AWSCredentials storageCreds;
    AmazonS3Client client;

    FileSystemOptions currFSO;
    UserAuthenticator ua;

    if (fileSystemOptions == null) {
        currFSO = getDefaultFileSystemOptions();
        ua = SS3FileSystemConfigBuilder.getInstance().getUserAuthenticator(currFSO);
    } else {
        currFSO = fileSystemOptions;
        ua = DefaultFileSystemConfigBuilder.getInstance().getUserAuthenticator(currFSO);
    }

    UserAuthenticationData authData = null;
    try {
        authData = ua.requestAuthentication(AUTHENTICATOR_TYPES);

        String currAcct = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(genRootName.getUserName())));

        String currKey = UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
                UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(genRootName.getPassword())));

        storageCreds = new BasicAWSCredentials(currAcct, currKey);

        client = new AmazonS3Client(storageCreds);

        if (StringUtils.isNoneBlank(endpoint)) {
            client.setEndpoint(endpoint);
        }

        if (region != null) {
            client.setRegion(region);
        }

        fileSystem = new SS3FileSystem(genRootName, client, fileSystemOptions);
    } finally {
        UserAuthenticatorUtils.cleanup(authData);
    }

    return fileSystem;
}

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.  j a  va2s  .  co m

        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:hu.mta.sztaki.lpds.cloud.entice.imageoptimizer.iaashandler.amazontarget.Storage.java

License:Apache License

/**
 * @param endpoint S3 endpoint URL/*from   w w w  .  j  a  v  a2 s.  co m*/
 * @param accessKey Access key
 * @param secretKey Secret key
 * @param bucket Bucket name 
 * @param path Key name of the object to download (path + file name)
 * @param file Local file to download to 
 * @throws Exception On any error
 */
public static void download(String endpoint, String accessKey, String secretKey, String bucket, String path,
        File file) throws Exception {
    AmazonS3Client amazonS3Client = null;
    InputStream in = null;
    OutputStream out = null;
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxConnections(MAX_CONNECTIONS);
        clientConfiguration.setMaxErrorRetry(PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY);
        clientConfiguration.setConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT);
        amazonS3Client = new AmazonS3Client(awsCredentials, clientConfiguration);
        S3ClientOptions clientOptions = new S3ClientOptions().withPathStyleAccess(true);
        amazonS3Client.setS3ClientOptions(clientOptions);
        amazonS3Client.setEndpoint(endpoint);
        S3Object object = amazonS3Client.getObject(new GetObjectRequest(bucket, path));
        in = object.getObjectContent();
        byte[] buf = new byte[BUFFER_SIZE];
        out = new FileOutputStream(file);
        int count;
        while ((count = in.read(buf)) != -1)
            out.write(buf, 0, count);
        out.close();
        in.close();
    } catch (AmazonServiceException x) {
        Shrinker.myLogger.info("download error: " + x.getMessage());
        throw new Exception("download exception", x);
    } catch (AmazonClientException x) {
        Shrinker.myLogger.info("download error: " + x.getMessage());
        throw new Exception("download exception", x);
    } catch (IOException x) {
        Shrinker.myLogger.info("download error: " + x.getMessage());
        throw new Exception("download exception", x);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
            }
        }
        if (amazonS3Client != null) {
            try {
                amazonS3Client.shutdown();
            } catch (Exception e) {
            }
        }
    }
}

From source file:hu.mta.sztaki.lpds.cloud.entice.imageoptimizer.iaashandler.amazontarget.Storage.java

License:Apache License

/**
 * @param file Local file to upload/*w  w  w  .  j  a v  a2  s . c o  m*/
 * @param endpoint S3 endpoint URL
 * @param accessKey Access key
 * @param secretKey Secret key
 * @param bucket Bucket name 
 * @param path Key name (path + file name)
 * @throws Exception On any error
 */
public static void upload(File file, String endpoint, String accessKey, String secretKey, String bucket,
        String path) throws Exception {
    AmazonS3Client amazonS3Client = null;
    try {
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxConnections(MAX_CONNECTIONS);
        clientConfiguration.setMaxErrorRetry(PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY);
        clientConfiguration.setConnectionTimeout(ClientConfiguration.DEFAULT_CONNECTION_TIMEOUT);
        amazonS3Client = new AmazonS3Client(awsCredentials, clientConfiguration);
        S3ClientOptions clientOptions = new S3ClientOptions().withPathStyleAccess(true);
        amazonS3Client.setS3ClientOptions(clientOptions);
        amazonS3Client.setEndpoint(endpoint);
        //         amazonS3Client.putObject(new PutObjectRequest(bucket, path, file)); // up to 5GB
        TransferManager tm = new TransferManager(amazonS3Client); // up to 5TB
        Upload upload = tm.upload(bucket, path, file);
        // while (!upload.isDone()) { upload.getProgress().getBytesTransferred(); Thread.sleep(1000); } // to get progress
        upload.waitForCompletion();
        tm.shutdownNow();
    } catch (AmazonServiceException x) {
        Shrinker.myLogger.info("upload error: " + x.getMessage());
        throw new Exception("upload exception", x);
    } catch (AmazonClientException x) {
        Shrinker.myLogger.info("upload error: " + x.getMessage());
        throw new Exception("upload exception", x);
    } finally {
        if (amazonS3Client != null) {
            try {
                amazonS3Client.shutdown();
            } catch (Exception e) {
            }
        }
    }
}

From source file:jp.buyee.glover.KinesisConnectorExecutor.java

License:Open Source License

/**
 * Helper method to create the Amazon S3 bucket.
 * //from  ww w .ja va2 s.c  o  m
 * @param s3Bucket
 *        The name of the bucket to create
 */
private void createS3Bucket(String s3Bucket) {
    AmazonS3Client client = new AmazonS3Client(config.AWS_CREDENTIALS_PROVIDER);
    client.setEndpoint(config.S3_ENDPOINT);
    LOG.info("Creating Amazon S3 bucket " + s3Bucket);
    S3Utils.createBucket(client, s3Bucket);
}

From source file:n3phele.storage.s3.CloudStorageImpl.java

License:Open Source License

public boolean createBucket(Repository repo) throws ForbiddenException {
    Credential credential = repo.getCredential().decrypt();
    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    try {/*from w ww  .j  a va 2 s.  co m*/
        try {
            s3.listObjects(new ListObjectsRequest(repo.getRoot(), null, null, null, 0));

            // it exists and the current account owns it
            return false;
        } catch (AmazonServiceException ase) {
            switch (ase.getStatusCode()) {
            case 403:
                /*
                 * A permissions error means the bucket exists, but is owned by
                 * another account.
                 */
                throw new ForbiddenException(
                        "Bucket " + repo.getRoot() + " has already been created by another user.");
            case 404:
                Bucket bucket = s3.createBucket(repo.getRoot());
                log.info("Bucket created " + bucket.getName());
                return true;
            default:
                throw ase;
            }
        }

    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "Service Error processing " + repo, e);

    } catch (AmazonClientException e) {
        log.log(Level.SEVERE, "Client Error processing " + repo, e);

    }
    return false;
}

From source file:n3phele.storage.s3.CloudStorageImpl.java

License:Open Source License

public FileNode getMetadata(Repository repo, String filename) {
    FileNode f = new FileNode();
    UriBuilder result = UriBuilder.fromUri(repo.getTarget());
    result.path(repo.getRoot()).path(filename);
    f.setName(result.build().toString());
    Credential credential = repo.getCredential().decrypt();

    log.info("Get info on " + repo.getRoot() + " " + filename);
    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    try {//from   www . j  a va2  s . co  m
        ObjectListing metadata = s3.listObjects(
                new ListObjectsRequest().withBucketName(repo.getRoot()).withPrefix(filename).withMaxKeys(1));
        List<S3ObjectSummary> metadataList = metadata.getObjectSummaries();
        if (metadataList == null || metadataList.size() != 1) {
            throw new NotFoundException(filename);
        }
        f.setModified(metadataList.get(0).getLastModified());
        f.setSize(metadataList.get(0).getSize());

        log.info(filename + " " + f.getModified() + " " + f.getSize());
    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "Service Error processing " + f + repo, e);
        throw new NotFoundException("Retrieve " + filename + " fails " + e.toString());
    } catch (AmazonClientException e) {
        log.log(Level.SEVERE, "Client Error processing " + f + repo, e);
        throw new NotFoundException("Retrieve " + filename + " fails " + e.toString());
    }
    return f;
}