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:n3phele.storage.s3.CloudStorageImpl.java

License:Open Source License

public boolean deleteFile(Repository repo, String filename) {
    boolean result = false;
    Credential credential = repo.getCredential().decrypt();

    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    try {/*ww  w.j a  va 2  s.  c  o  m*/
        s3.deleteObject(repo.getRoot(), filename);
        result = true;
    } 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 result;
}

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

License:Open Source License

public boolean deleteFolder(Repository repo, String filename) {
    boolean result = false;
    Credential credential = repo.getCredential().decrypt();
    int retry = 3;

    setPermissions(repo, filename, false);
    if (!filename.endsWith("/")) {
        filename += "/";
    }//  w ww.ja v  a  2 s .c  o  m

    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    while (retry-- > 0) {
        try {
            ObjectListing objects = s3.listObjects(repo.getRoot(), filename);
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                log.info("Delete " + repo.getRoot() + ":" + objectSummary.getKey());
                s3.deleteObject(repo.getRoot(), objectSummary.getKey());
            }
            if (objects.isTruncated()) {
                retry++;
                log.info("Doing next portion");
                continue;
            }
            result = true;
            break;
        } 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 result;
}

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

License:Open Source License

public boolean setPermissions(Repository repo, String filename, boolean isPublic) {
    String bucket = repo.getRoot();
    Credential credential = repo.getCredential().decrypt();
    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    String key = new S3ObjectResource(bucket, filename).getId();
    boolean inserted = false;

    s3.setEndpoint(repo.getTarget().toString());
    try {//from  w  w w .j  ava 2 s  .c  o m
        List<Statement> statements = new ArrayList<Statement>();
        Policy policy = null;
        BucketPolicy bp = s3.getBucketPolicy(repo.getRoot());
        if (bp != null && bp.getPolicyText() != null) {
            log.info("Policy text " + bp.getPolicyText());
            policy = PolicyHelper.parse(bp.getPolicyText());
            log.info("Policy object is " + (policy == null ? null : policy.toJson()));

            if (policy != null) {
                if (policy.getStatements() != null) {
                    for (Statement statement : policy.getStatements()) {
                        if (statement.getId().equals("n3phele")) {
                            List<com.amazonaws.auth.policy.Resource> resources = statement.getResources();
                            List<com.amazonaws.auth.policy.Resource> update = new ArrayList<com.amazonaws.auth.policy.Resource>();
                            if (resources != null) {
                                for (com.amazonaws.auth.policy.Resource resource : resources) {
                                    String resourceName = resource.getId();
                                    if (resourceName.endsWith("*")) {
                                        resourceName = resourceName.substring(0, resourceName.length() - 1);
                                    }
                                    if (!(resourceName + "/").startsWith(key + "/")) {
                                        update.add(resource);
                                    } else {
                                        log.info("Removing " + resource.getId());
                                    }
                                }
                            }
                            if (isPublic && !inserted)
                                update.add(new S3ObjectResource(repo.getRoot(), filename + "*"));
                            if (update.size() > 0) {
                                statement.setResources(update);
                                statements.add(statement);
                            }
                            inserted = true;
                        } else {
                            statements.add(statement);
                        }
                    }
                }
                if (!inserted && isPublic) {
                    Statement statement = new Statement(Effect.Allow);
                    statement.setId("n3phele");
                    statement.setPrincipals(Arrays.asList(new Principal("*")));
                    statement.setActions(Arrays.asList((Action) S3Actions.GetObject));
                    statement.setResources(Arrays
                            .asList((com.amazonaws.auth.policy.Resource) new S3ObjectResource(repo.getRoot(),
                                    filename + "*")));
                    statements.add(statement);
                }
            }
        }
        if (policy == null && isPublic) {
            policy = new Policy("n3phele-" + repo.getRoot());
            Statement statement = new Statement(Effect.Allow);
            statement.setId("n3phele");
            statement.setPrincipals(Arrays.asList(new Principal("*")));
            statement.setActions(Arrays.asList((Action) S3Actions.GetObject));
            statement.setResources(Arrays.asList(
                    (com.amazonaws.auth.policy.Resource) new S3ObjectResource(repo.getRoot(), filename + "*")));
            statements.add(statement);
        }
        if (policy != null) {
            if (statements.size() != 0) {
                policy.setStatements(statements);
                s3.setBucketPolicy(repo.getRoot(), policy.toJson());
                log.info("Set policy " + policy.toJson());
            } else {
                s3.deleteBucketPolicy(repo.getRoot());
            }
        }
        return true;

    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "Service Error processing " + repo, e);
    } catch (AmazonClientException e) {
        log.log(Level.SEVERE, "Client Error processing " + repo, e);
    } catch (IllegalArgumentException e) {
        log.log(Level.SEVERE, "parse error ", e);
        log.log(Level.SEVERE, "cause", e.getCause());
    }
    return false;
}

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

License:Open Source License

public boolean checkExists(Repository repo, String filename) {
    boolean result = false;
    Credential credential = repo.getCredential().decrypt();

    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    try {//w w  w .  j  ava2 s . com
        ObjectMetadata metadata = s3.getObjectMetadata(repo.getRoot(), filename);
        log.info("Exists " + metadata.getContentType());
        return true;
    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "Service Error processing " + repo + " filename " + filename, e);
    } catch (AmazonClientException e) {
        log.log(Level.SEVERE, "Client Error processing " + repo + " filename " + filename, e);
    }
    return result;
}

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

License:Open Source License

public List<FileNode> getFileList(Repository repo, String prefix, int max) {
    List<FileNode> result = new ArrayList<FileNode>();
    Credential credential = repo.getCredential().decrypt();

    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    Policy p = null;/*ww w  . j a va  2  s  . c  o m*/
    try {
        BucketPolicy bp = s3.getBucketPolicy(repo.getRoot());
        log.info("Policy text " + bp.getPolicyText());
        if (bp != null && bp.getPolicyText() != null) {
            p = PolicyHelper.parse(bp.getPolicyText());
            log.info("Policy object is " + (p == null ? null : p.toJson()));
        }
    } catch (Exception e) {
        log.log(Level.WARNING, "Policy not supported", e);
    }

    try {
        ObjectListing s3Objects = s3
                .listObjects(new ListObjectsRequest(repo.getRoot(), prefix, null, "/", max));
        if (s3Objects.getCommonPrefixes() != null) {
            for (String dirName : s3Objects.getCommonPrefixes()) {
                String name = dirName;
                if (dirName.endsWith("/")) {
                    name = dirName.substring(0, dirName.length() - 1);
                }
                boolean isPublic = isPublicFolder(p, repo.getRoot(), name);
                name = name.substring(name.lastIndexOf("/") + 1);
                FileNode folder = FileNode.newFolder(name, prefix, repo, isPublic);
                log.info("Folder:" + folder);
                result.add(folder);
            }
        }
        if (s3Objects.getObjectSummaries() != null) {
            for (S3ObjectSummary fileSummary : s3Objects.getObjectSummaries()) {
                String key = fileSummary.getKey();
                if (key != null && !key.equals(prefix)) {
                    String name = key.substring(key.lastIndexOf("/") + 1);
                    UriBuilder builder = UriBuilder.fromUri(repo.getTarget()).path(repo.getRoot());
                    if (prefix != null && !prefix.isEmpty()) {
                        builder = builder.path(prefix);
                    }
                    FileNode file = FileNode.newFile(name, prefix, repo, fileSummary.getLastModified(),
                            fileSummary.getSize(), builder.path(name).toString());
                    log.info("File:" + file);
                    result.add(file);
                }
            }
        }

    } 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 result;
}

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.Utils.java

License:Apache License

/**
 * Create AmazonS3Client from properties.
 * //from  ww w .j  av  a2s  .  c om
 * @param prop properties to configure @link {@link AmazonS3Client}
 * @return {@link AmazonS3Client}
 */
public static AmazonS3Client openService(final Properties prop) {
    String accessKey = prop.getProperty(S3Constants.ACCESS_KEY);
    String secretKey = prop.getProperty(S3Constants.SECRET_KEY);
    AmazonS3Client s3service = null;
    if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey)) {
        LOG.info("Configuring Amazon Client from environment");
        s3service = new AmazonS3Client(getClientConfiguration(prop));
    } else {
        LOG.info("Configuring Amazon Client from property file.");
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        s3service = new AmazonS3Client(credentials, getClientConfiguration(prop));
    }
    String region = prop.getProperty(S3Constants.S3_REGION);
    String endpoint = null;
    String propEndPoint = prop.getProperty(S3Constants.S3_END_POINT);
    if ((propEndPoint != null) & !"".equals(propEndPoint)) {
        endpoint = propEndPoint;
    } else {
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region s3Region = Regions.getCurrentRegion();
            if (s3Region != null) {
                region = s3Region.getName();
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION
                        + "] not configured and cannot be derived from environment");
            }
        }
        if (DEFAULT_AWS_BUCKET_REGION.equals(region)) {
            endpoint = S3 + DOT + AWSDOTCOM;
        } else if (Region.EU_Ireland.toString().equals(region)) {
            endpoint = "s3-eu-west-1" + DOT + AWSDOTCOM;
        } else {
            endpoint = S3 + DASH + region + DOT + AWSDOTCOM;
        }
    }
    /*
     * setting endpoint to remove latency of redirection. If endpoint is
     * not set, invocation first goes us standard region, which
     * redirects it to correct location.
     */
    s3service.setEndpoint(endpoint);
    LOG.info("S3 service endpoint [{}] ", endpoint);
    return s3service;
}

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());
        }//  w  w w . j a va 2  s  . co m
        configClientMap.put(s3config, client);
    }
    return client;
}

From source file:org.elasticsearch.cloud.aws.AwsS3Service.java

License:Apache License

private synchronized AmazonS3 getClient(String endpoint, String account, String key) {
    Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
    AmazonS3Client client = clients.get(clientDescriptor);
    if (client != null) {
        return client;
    }//from  w ww. j  a v a  2 s .c o  m

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    String protocol = componentSettings.get("protocol", "http").toLowerCase();
    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new ElasticsearchIllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }

    String proxyHost = componentSettings.get("proxy_host");
    if (proxyHost != null) {
        String portString = componentSettings.get("proxy_port", "80");
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new ElasticsearchIllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }
    client = new AmazonS3Client(credentials, clientConfiguration);

    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    clients.put(clientDescriptor, client);
    return client;
}

From source file:org.elasticsearch.cloud.aws.InternalAwsS3Service.java

License:Apache License

private synchronized AmazonS3 getClient(String endpoint, String protocol, String account, String key,
        Integer maxRetries) {//  w  ww  .ja  va 2s . co m
    Tuple<String, String> clientDescriptor = new Tuple<String, String>(endpoint, account);
    AmazonS3Client client = clients.get(clientDescriptor);
    if (client != null) {
        return client;
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    // the response metadata cache is only there for diagnostics purposes,
    // but can force objects from every response to the old generation.
    clientConfiguration.setResponseMetadataCacheSize(0);
    if (protocol == null) {
        protocol = settings.get("cloud.aws.protocol", "https").toLowerCase();
        protocol = settings.get("cloud.aws.s3.protocol", protocol).toLowerCase();
    }

    if ("http".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTP);
    } else if ("https".equals(protocol)) {
        clientConfiguration.setProtocol(Protocol.HTTPS);
    } else {
        throw new IllegalArgumentException(
                "No protocol supported [" + protocol + "], can either be [http] or [https]");
    }

    String proxyHost = settings.get("cloud.aws.proxy_host");
    proxyHost = settings.get("cloud.aws.s3.proxy_host", proxyHost);
    if (proxyHost != null) {
        String portString = settings.get("cloud.aws.proxy_port", "80");
        portString = settings.get("cloud.aws.s3.proxy_port", portString);
        Integer proxyPort;
        try {
            proxyPort = Integer.parseInt(portString, 10);
        } catch (NumberFormatException ex) {
            throw new IllegalArgumentException(
                    "The configured proxy port value [" + portString + "] is invalid", ex);
        }
        clientConfiguration.withProxyHost(proxyHost).setProxyPort(proxyPort);
    }

    if (maxRetries != null) {
        // If not explicitly set, default to 3 with exponential backoff policy
        clientConfiguration.setMaxErrorRetry(maxRetries);
    }

    // #155: we might have 3rd party users using older S3 API version
    String awsSigner = settings.get("cloud.aws.s3.signer", settings.get("cloud.aws.signer"));
    if (awsSigner != null) {
        logger.debug("using AWS API signer [{}]", awsSigner);
        try {
            AwsSigner.configureSigner(awsSigner, clientConfiguration);
        } catch (IllegalArgumentException e) {
            logger.warn("wrong signer set for [cloud.aws.s3.signer] or [cloud.aws.signer]: [{}]", awsSigner);
        }
    }

    AWSCredentialsProvider credentials;

    if (account == null && key == null) {
        credentials = new AWSCredentialsProviderChain(new EnvironmentVariableCredentialsProvider(),
                new SystemPropertiesCredentialsProvider(), new InstanceProfileCredentialsProvider());
    } else {
        credentials = new AWSCredentialsProviderChain(
                new StaticCredentialsProvider(new BasicAWSCredentials(account, key)));
    }
    client = new AmazonS3Client(credentials, clientConfiguration);

    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }
    clients.put(clientDescriptor, client);
    return client;
}

From source file:org.elasticsearch.repositories.s3.InternalAwsS3Service.java

License:Apache License

@Override
public synchronized AmazonS3 client(Settings repositorySettings) {
    String clientName = CLIENT_NAME.get(repositorySettings);
    AmazonS3Client client = clientsCache.get(clientName);
    if (client != null) {
        return client;
    }/*from  w  ww .j  a  v a  2s.c o m*/

    S3ClientSettings clientSettings = clientsSettings.get(clientName);
    if (clientSettings == null) {
        throw new IllegalArgumentException(
                "Unknown s3 client name [" + clientName + "]. Existing client configs: "
                        + Strings.collectionToDelimitedString(clientsSettings.keySet(), ","));
    }

    logger.debug("creating S3 client with client_name [{}], endpoint [{}]", clientName,
            clientSettings.endpoint);

    AWSCredentialsProvider credentials = buildCredentials(logger, deprecationLogger, clientSettings,
            repositorySettings);
    ClientConfiguration configuration = buildConfiguration(clientSettings, repositorySettings);

    client = new AmazonS3Client(credentials, configuration);

    if (Strings.hasText(clientSettings.endpoint)) {
        client.setEndpoint(clientSettings.endpoint);
    }

    clientsCache.put(clientName, client);
    return client;
}