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

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

Introduction

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

Prototype

@SdkInternalApi
AmazonS3Client(AmazonS3ClientParams s3ClientParams) 

Source Link

Document

Constructs a new client to invoke service methods on S3 using the specified parameters.

Usage

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 {//from  w w  w  . j  a  va2  s . c  o m
        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;/*from w w w. j a v  a  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:net.geoprism.data.aws.AmazonEndpoint.java

License:Open Source License

@Override
public void copyFiles(File directory, List<String> keys, boolean preserveDirectories) {
    try {//from  w  w  w . j av a  2s. c om
        List<File> files = new LinkedList<File>();

        AmazonS3 client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

        for (String key : keys) {
            GetObjectRequest request = new GetObjectRequest("geodashboarddata", key);

            S3Object object = client.getObject(request);

            InputStream istream = object.getObjectContent();

            try {
                String targetPath = this.getTargetPath(preserveDirectories, key);

                File file = new File(directory, targetPath);

                FileUtils.copyInputStreamToFile(istream, file);

                files.add(file);
            } finally {
                // Process the objectData stream.
                istream.close();
            }
        }
    } catch (IOException e) {
        throw new ProgrammingErrorException(e);
    }
}

From source file:net.geoprism.data.aws.AmazonEndpoint.java

License:Open Source License

private List<String> listFiles(String prefix) {
    List<String> files = new LinkedList<String>();

    try {/*from  ww w .ja  va2 s. c  om*/
        AmazonS3 s3Client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

        ListObjectsRequest request = new ListObjectsRequest();
        request = request.withBucketName("geodashboarddata");
        request = request.withPrefix(prefix);

        ObjectListing listing;

        do {
            listing = s3Client.listObjects(request);

            List<S3ObjectSummary> summaries = listing.getObjectSummaries();

            for (S3ObjectSummary summary : summaries) {
                String key = summary.getKey();

                if (key.endsWith(".xml.gz")) {
                    files.add(key);
                }
            }

            request.setMarker(listing.getNextMarker());
        } while (listing != null && listing.isTruncated());
    } catch (Exception e) {
        logger.error("Unable to retrieve files", e);
    }

    return files;
}

From source file:net.henryhu.roxlab2.NotePadProvider.java

License:Apache License

@Override
public boolean onCreate() {
    s3 = new AmazonS3Client(
            new PropertiesCredentials(NotePadProvider.class.getResourceAsStream("AwsCredentials.properties")));

    return true;/*from   w  w  w.j  a va  2  s  .co  m*/
}

From source file:net.oletalk.hellospringboot.dao.S3Dao.java

public void uploadFile(String bucketName, String key, File file) throws S3Exception {
    AmazonS3 s3client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    try {//from www  .ja  v a  2 s  .c  o m
        LOG.info("Uploading file to S3");
        s3client.putObject(new PutObjectRequest(bucketName, key, file));
    } catch (AmazonServiceException ase) {
        LOG.error("Problem uploading file to S3: " + ase.getMessage() + " (status code " + ase.getStatusCode()
                + ")");
        throw new S3Exception("Problem uploading file to S3");
    } catch (AmazonClientException ace) {
        LOG.error("Internal error uploading file to S3: " + ace.getMessage());
        throw new S3Exception("Problem uploading file to S3");
    }
    LOG.info("Upload complete");

}

From source file:net.oletalk.hellospringboot.dao.S3Dao.java

public void getObjectData(String bucketName, String key, OutputStream out) throws S3Exception {
    AmazonS3 s3client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    LOG.info("fetching requested object " + key);
    try {/*from  w  w  w  . j  a v a 2 s . c om*/
        S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key));
        LOG.info("fetched, serving up");
        try {
            IOUtils.copy(object.getObjectContent(), out);
        } catch (IOException ioe) {
            LOG.error("Problem writing message: " + ioe.getMessage());
        }

    } catch (AmazonS3Exception e) {
        LOG.error("Problem fetching from S3: ", e);
        String errorMsg = "Error fetching document";
        try {
            out.write(errorMsg.getBytes(Charset.defaultCharset()));
            throw new S3Exception("Error fetching document");
        } catch (IOException ioe) {
            LOG.error("Problem writing message: " + ioe.getMessage());
        }
    }
}

From source file:ninja.eivind.hotsreplayuploader.providers.hotslogs.HotsLogsAmazonS3ClientFactoryBean.java

License:Apache License

@Override
public AmazonS3 getObject() throws Exception {
    return new AmazonS3Client(credentials);
}

From source file:ohnosequences.ivy.S3Repository.java

License:Apache License

public S3Repository(String accessKey, String secretKey, boolean overwrite, Region region,
        CannedAccessControlList acl, boolean serverSideEncryption) {
    AWSCredentialsProvider provider = new InstanceProfileCredentialsProvider();
    try {//from  www.  jav  a 2s  . co m
        provider.getCredentials();
    } catch (AmazonClientException e1) {
        provider = new StaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey));
    }

    s3Client = new AmazonS3Client(provider);
    this.overwrite = overwrite;
    this.region = region;
    this.acl = acl;
    this.serverSideEncryption = serverSideEncryption;
}

From source file:ohnosequences.ivy.S3Repository.java

License:Apache License

public S3Repository(AWSCredentialsProvider provider, boolean overwrite, Region region,
        CannedAccessControlList acl, boolean serverSideEncryption) {
    s3Client = new AmazonS3Client(provider);
    this.overwrite = overwrite;
    this.region = region;
    this.acl = acl;
    this.serverSideEncryption = serverSideEncryption;
}