Example usage for com.amazonaws.services.s3.model BucketPolicy getPolicyText

List of usage examples for com.amazonaws.services.s3.model BucketPolicy getPolicyText

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model BucketPolicy getPolicyText.

Prototype

public String getPolicyText() 

Source Link

Document

Gets the raw policy JSON text as returned by Amazon S3.

Usage

From source file:aws.example.s3.GetBucketPolicy.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetBucketPolicy <bucket>\n\n" + "Where:\n"
            + "    bucket - the bucket to get the policy from.\n\n" + "Example:\n"
            + "    GetBucketPolicy testbucket\n\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);//from  w w w .  jav a 2  s .  c om
    }

    String bucket_name = args[0];
    String policy_text = null;

    System.out.format("Getting policy for bucket: \"%s\"\n\n", bucket_name);

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    try {
        BucketPolicy bucket_policy = s3.getBucketPolicy(bucket_name);
        policy_text = bucket_policy.getPolicyText();
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }

    if (policy_text == null) {
        System.out.println("The specified bucket has no bucket policy.");
    } else {
        System.out.println("Returned policy:");
        System.out.println("----");
        System.out.println(policy_text);
        System.out.println("----\n");
    }

    System.out.println("Done!");
}

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 {/* w  ww  . j a  v  a 2  s. co 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 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;//  w  w  w. ja  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;
}