Example usage for com.amazonaws.services.s3.model S3ObjectSummary getKey

List of usage examples for com.amazonaws.services.s3.model S3ObjectSummary getKey

Introduction

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

Prototype

public String getKey() 

Source Link

Document

Gets the key under which this object is stored in Amazon S3.

Usage

From source file:com.digitaslbi.helios.mock.utils.ConnectionHelper.java

private static Map convertResultToFile(ListObjectsRequest listObjectsRequest, String path, String parentPath) {
    Map<String, File> filesMap = new HashMap<String, File>();
    File aux;//from  w w w . ja  va  2s.c om

    ObjectListing objListing = s3Client.listObjects(listObjectsRequest);

    // creates the parent elemment
    aux = new File();
    aux.setIsParent(true);
    aux.setFullPath(parentPath);
    aux.setPath(parentPath);

    // adapts the path in order to use as map key
    if (aux.getPath() != null) {
        aux.setPath(clearPathDelimiter(aux.getPath()));

        if (aux.getPath().lastIndexOf(MocksConstants.AWS_PARENT_DELIMITER.getValue()) > 0) {
            aux.setPath(aux.getPath()
                    .substring(aux.getPath().lastIndexOf(MocksConstants.AWS_PARENT_DELIMITER.getValue()) + 1));
        }
    }
    filesMap.put(aux.getPath(), aux);

    for (S3ObjectSummary objSummary : objListing.getObjectSummaries()) {
        aux = new File();
        aux.setFullPath(objSummary.getKey());
        aux.setParent(objListing.getPrefix());

        // if size is 0 is considered a folder
        aux.setIsFile((objSummary.getSize() == 0) ? false : true);

        if (aux.getParent() != null) {
            if (!aux.getFullPath().equals(aux.getParent())) {
                aux.setPath(aux.getFullPath().replace(aux.getParent(), ""));
            }
        } else {
            aux.setPath(aux.getFullPath());
        }

        if (aux.getPath() != null) {
            filesMap.put(clearPathDelimiter(aux.getPath()), aux);
        }
    }

    for (String folderNames : objListing.getCommonPrefixes()) {
        aux = new File();
        aux.setFullPath(folderNames);
        aux.setParent(objListing.getPrefix());
        aux.setIsFile(false);

        if (aux.getParent() != null) {
            aux.setPath(aux.getFullPath().replace(aux.getParent(), ""));
        } else {
            aux.setPath(aux.getFullPath());
        }

        if (aux.getPath() != null) {
            filesMap.put(clearPathDelimiter(aux.getPath()), aux);
        }
    }

    return filesMap;
}

From source file:com.digitaslbi.helios.mock.utils.ConnectionHelper.java

public static void deleteFolder(String bucketName, String folderName) {
    List<S3ObjectSummary> fileList = s3Client.listObjects(bucketName, folderName).getObjectSummaries();

    try {//from   ww  w  . j  a v a 2s  . c  om
        for (S3ObjectSummary file : fileList) {
            s3Client.deleteObject(bucketName, file.getKey());
        }

        s3Client.deleteObject(bucketName, folderName);

    } catch (AmazonServiceException ase) {
        log.error("Caught an AmazonServiceException.");
        log.error("Error Message:    " + ase.getMessage());
        log.error("HTTP Status Code: " + ase.getStatusCode());
        log.error("AWS Error Code:   " + ase.getErrorCode());
        log.error("Error Type:       " + ase.getErrorType());
        log.error("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        log.error("Caught an AmazonClientException.");
        log.error("Error Message: " + ace.getMessage());
    }
}

From source file:com.digitaslbi.helios.utils.S3Helper.java

public static Folder getFolder(String path) {
    ListObjectsRequest listObjectsRequest;
    Folder folder;//w w  w .  ja va 2s.  c  o  m
    File aux;

    if (path == null || path.equals("/")) {
        listObjectsRequest = getRootFolders();
    } else {
        listObjectsRequest = getContentByPreffix(path);
    }

    ObjectListing objListing = s3Client.listObjects(listObjectsRequest);

    folder = new Folder();
    folder.setFiles(new ArrayList<File>());

    folder.setPath(path);
    folder.setParent(obtainParentPath(path));

    for (S3ObjectSummary objSummary : objListing.getObjectSummaries()) {
        aux = new File();
        aux.setPath(objSummary.getKey());
        aux.setParent(objListing.getPrefix());

        // if size is 0 is considered a folder
        aux.setIsFile((objSummary.getSize() == 0) ? false : true);

        if (!aux.getPath().equals(path)) {
            folder.getFiles().add(aux);
        }
    }

    for (String folderNames : objListing.getCommonPrefixes()) {
        aux = new File();
        aux.setPath(folderNames);
        aux.setParent(objListing.getPrefix());
        aux.setIsFile(false);

        folder.getFiles().add(aux);
    }

    log.info("[S3Helper][getFolder] Path: " + path + " items found: " + folder.getFiles().size());

    return folder;
}

From source file:com.digitaslbi.helios.utils.S3Helper.java

public static void deleteFolder(String folderName) {
    connect();// w w  w . j a  va2 s  . c  o m
    List<S3ObjectSummary> fileList = s3Client
            .listObjects(S3Properties.getInstance().getBucketName(), folderName).getObjectSummaries();

    try {
        for (S3ObjectSummary file : fileList) {
            s3Client.deleteObject(S3Properties.getInstance().getBucketName(), file.getKey());
        }

        s3Client.deleteObject(S3Properties.getInstance().getBucketName(), folderName);

    } catch (AmazonServiceException ase) {
        log.error("[S3Helper][deleteFolder] Caught an AmazonServiceException.");
        log.error("Error Message:    " + ase.getMessage());
        log.error("HTTP Status Code: " + ase.getStatusCode());
        log.error("AWS Error Code:   " + ase.getErrorCode());
        log.error("Error Type:       " + ase.getErrorType());
        log.error("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        log.error("[S3Helper][deleteFolder] Caught an AmazonClientException.");
        log.error("Error Message: " + ace.getMessage());
    }
}

From source file:com.dongli.model.MyJSONData.java

License:Open Source License

public static JSONArray getAllObjects() throws MyRESTException {

    // return value
    JSONArray jsonArray = new JSONArray();

    try {//from w  w w.  j  ava  2  s . c  o m
        ObjectListing objects = MyAWSStorage.getInstance().s3client
                .listObjects(MyConfiguration.getInstance().bucket);

        do {
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                JSONObject jb = new JSONObject();
                jb.put("url", MyConfiguration.getInstance().urlPrefix + MyConfiguration.getInstance().rootPrefix
                        + "/" + objectSummary.getKey());
                jsonArray.put(jb);
                // System.out.println(objectSummary.getKey());
            }

            objects = MyAWSStorage.getInstance().s3client.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());
    } catch (JSONException e) {
        throw new MyRESTException("Failed to get all JSON object.");
    } catch (AmazonServiceException ase) {
        throw new MyRESTException("Failed to get all JSON object.");
    } catch (AmazonClientException ace) {
        throw new MyRESTException("Failed to get all JSON object.");
    }

    return jsonArray;
}

From source file:com.dss.sframework.tools.amazon.AmazonUtil.java

License:Open Source License

public static void deleteBucket() {
    String name = ConstantAmazon.BUCKET_NAME.toLowerCase(Locale.US);
    List<S3ObjectSummary> objData = sS3Client.listObjects(name).getObjectSummaries();
    if (objData.size() > 0) {
        DeleteObjectsRequest emptyBucket = new DeleteObjectsRequest(name);
        List<KeyVersion> keyList = new ArrayList<>();
        for (S3ObjectSummary summary : objData) {
            keyList.add(new KeyVersion(summary.getKey()));
        }// ww  w .  java 2  s.c  o m
        emptyBucket.withKeys(keyList);
        sS3Client.deleteObjects(emptyBucket);
    }
    sS3Client.deleteBucket(name);
}

From source file:com.dustindoloff.s3websitedeploy.Main.java

License:Apache License

private static boolean emptyBucket(final AmazonS3 s3Client, final String bucket) {
    final ListObjectsV2Request request = new ListObjectsV2Request();
    request.setBucketName(bucket);/*from  w ww  .jav  a  2s.  c o m*/

    String continuationToken = null;
    ListObjectsV2Result result;
    do {
        request.setContinuationToken(continuationToken);
        result = s3Client.listObjectsV2(bucket);
        for (final S3ObjectSummary summary : result.getObjectSummaries()) {
            s3Client.deleteObject(bucket, summary.getKey());
        }

        continuationToken = result.getNextContinuationToken();
    } while (result.isTruncated());

    return true;
}

From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w  ww  .jav  a2s .c o  m*/
 */
@Override
public DeleteObjectsResult deleteObjectsWithPrefix(AmazonS3 client, String bucketName, String prefix) {
    if (client == null) {
        return null;
    } else if (StringUtil.isEmpty(bucketName)) {
        return null;
    } else if (StringUtil.isEmpty(prefix)) {
        return null;
    }
    int pre_len = prefix.length();
    ObjectListing objectListing = this.getObjectList(client, bucketName, prefix);
    List<KeyVersion> keyList = new ArrayList<KeyVersion>();
    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        String key = objectSummary.getKey();
        int len = key.length();
        if (len < pre_len)
            continue;
        int i;
        for (i = 0; i < pre_len; i++)
            if (key.charAt(i) != prefix.charAt(i))
                break;
        if (i < pre_len)
            continue;
        KeyVersion keyVersion = new KeyVersion(key);
        keyList.add(keyVersion);
    }
    return this.deleteObjects2(client, bucketName, keyList);
}

From source file:com.emc.vipr.s3.sample._99_DeleteBucket.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // delete the demo bucket and all its content
    for (S3ObjectSummary summary : s3.listObjects(ViPRS3Factory.S3_BUCKET).getObjectSummaries()) {
        s3.deleteObject(ViPRS3Factory.S3_BUCKET, summary.getKey());
    }// w  w w .  j av  a 2  s  . c o m
    s3.deleteBucket(ViPRS3Factory.S3_BUCKET);

    // print bucket key/value and content for validation
    System.out.println(String.format("deleted bucket [%s]", ViPRS3Factory.S3_BUCKET));
}

From source file:com.epam.dlab.module.aws.S3FileList.java

License:Apache License

private boolean matchBillingRegexAndWasNotProcessed(S3ObjectSummary o) {
    return reportPattern.matcher(o.getKey()).matches()
            && !moduleData.wasProcessed(o.getKey(), o.getLastModified(), extractDatePrefix(reportPattern, o));
}