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.athena.dolly.web.aws.s3.S3Service.java

License:Open Source License

/**
 * Retrieve object summaries in specific bucket
 * @param bucketName//from  ww w  .  j a v a 2s  . c  o  m
 * @return
 */
public List<S3Dto> listBucket(String bucketName, String prefix) {
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName)
            .withPrefix(prefix).withDelimiter(null);
    ObjectListing objectListing = s3.listObjects(listObjectsRequest);

    List<S3Dto> list = new ArrayList<S3Dto>();

    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        logger.info(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        list.add(makeDto(bucketName, objectSummary));
    }

    return list;
}

From source file:com.athena.dolly.web.aws.s3.S3Service.java

License:Open Source License

private S3Dto makeDto(String bucketName, S3ObjectSummary objectSummary) {
    S3Dto dto = new S3Dto();

    // Default value setting
    dto.setBucketName(bucketName);/*from w w w.ja  v  a  2 s  .c  o m*/
    dto.setLastModified(date2String(objectSummary.getLastModified(), "yyyy/MM/dd a KK:mm"));
    dto.setSize((objectSummary.getSize() / 1024) + "K");
    dto.setDataType(checkDataType(objectSummary.getKey()));

    // Caculate position

    String current = "";
    String dataType = "file";
    String parent = "";

    String key = objectSummary.getKey();

    dto.setUrl(presignedUrl(bucketName, key).toString());
    // 1. lastIndexOf("/") == -1 is root directory's file
    int pos = key.lastIndexOf("/");
    if (pos == -1) { // root file

    } else { // This is directory or file. Apply filter
        current = key.substring(0, pos);
        key = key.substring(pos + 1);

        if (key.equals("")) {
            key = "..";
            dataType = "folder";
        }
        if (parent.length() != 0)
            parent = current.substring(0, current.lastIndexOf("/"));
    }

    dto.setKey(key);
    dto.setDataType(dataType);
    dto.setParent(parent);

    // 2. lastIndexOf("/") == 
    return dto;
}

From source file:com.atlassian.localstack.sample.S3Sample.java

License:Open Source License

public static void runTest(AWSCredentials credentials) throws IOException {

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);/*from  ww  w  .ja  v  a  2  s .c  o m*/
    s3.setEndpoint(LocalstackTestRunner.getEndpointS3());

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    /*
     * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
     * so once a bucket name has been taken by any user, you can't create
     * another bucket with that same name.
     *
     * You can optionally specify a location for your bucket if you want to
     * keep your data closer to your applications or users.
     */
    System.out.println("Creating bucket " + bucketName + "\n");
    s3.createBucket(bucketName);

    /*
     * List the buckets in your account
     */
    System.out.println("Listing buckets");
    for (Bucket bucket : s3.listBuckets()) {
        System.out.println(" - " + bucket.getName());
    }
    System.out.println();

    /*
     * Upload an object to your bucket - You can easily upload a file to
     * S3, or upload directly an InputStream if you know the length of
     * the data in the stream. You can also specify your own metadata
     * when uploading to S3, which allows you set a variety of options
     * like content-type and content-encoding, plus additional metadata
     * specific to your applications.
     */
    System.out.println("Uploading a new object to S3 from a file\n");
    s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

    /*
     * Download an object - When you download an object, you get all of
     * the object's metadata and a stream from which to read the contents.
     * It's important to read the contents of the stream as quickly as
     * possibly since the data is streamed directly from Amazon S3 and your
     * network connection will remain open until you read all the data or
     * close the input stream.
     *
     * GetObjectRequest also supports several other options, including
     * conditional downloading of objects based on modification times,
     * ETags, and selectively downloading a range of an object.
     */
    System.out.println("Downloading an object");
    S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
    System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
    displayTextInputStream(object.getObjectContent());

    /*
     * List objects in your bucket by prefix - There are many options for
     * listing the objects in your bucket.  Keep in mind that buckets with
     * many objects might truncate their results when listing their objects,
     * so be sure to check if the returned object listing is truncated, and
     * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
     * additional results.
     */
    System.out.println("Listing objects");
    ObjectListing objectListing = s3
            .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
    }
    System.out.println();

    /*
     * Delete an object - Unless versioning has been turned on for your bucket,
     * there is no way to undelete an object, so use caution when deleting objects.
     */
    System.out.println("Deleting an object\n");
    s3.deleteObject(bucketName, key);

    /*
     * Delete a bucket - A bucket must be completely empty before it can be
     * deleted, so remember to delete any objects from your buckets before
     * you try to delete them.
     */
    System.out.println("Deleting bucket " + bucketName + "\n");
    s3.deleteBucket(bucketName);
}

From source file:com.att.aro.core.cloud.aws.AwsRepository.java

License:Apache License

public List<S3ObjectSummary> getlist() {
    List<S3ObjectSummary> objects = null;
    if (s3Client != null) {
        ObjectListing objectListing = null;
        try {//from ww  w .  j  a  v a2s.  c o  m
            objectListing = s3Client.listObjects(bucketName);
            objects = objectListing.getObjectSummaries();
            for (S3ObjectSummary objectSummary : objects) {
                LOGGER.debug(
                        " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
            }
        } catch (Exception exc) {
            LOGGER.error("Error Message: " + exc.getMessage());
        }
        return objects;
    }
    return objects;
}

From source file:com.att.aro.ui.view.menu.tools.AWSDialog.java

License:Apache License

private JButton getRemoteListButton() {
    if (loadRemoteButton == null) {
        loadRemoteButton = new JButton();
        loadRemoteButton/*from   w w  w  .j  av  a  2s .  co  m*/
                .setText(ResourceBundleHelper.getMessageString(DialogItem.upload_trace_dialog_button_ok));
        loadRemoteButton.setText("Get Traces");
        loadRemoteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setCredentials();
                AwsRepository awsRepo = new AwsRepository(awsInfo, getProxySetting());
                if (true == awsRepo.isAuthenticated()) {
                    List<S3ObjectSummary> listRemote = awsRepo.getlist();
                    if (listRemote != null) {
                        for (S3ObjectSummary o : listRemote) {
                            if (o.getKey().endsWith(".zip")) {
                                model.addElement(o.getKey());
                            }
                        }
                        if (listRemote.size() > 1) {
                            if (mode == AWS.DOWNLOAD) {
                                list.setSelectedIndex(0);
                            }
                        }
                    } else {
                        errorDialog("Unable to connect AWS");
                    }
                } else {
                    errorDialog(errorMessage);
                }
            }
        });
    }
    return loadRemoteButton;
}

From source file:com.awscrud.aws.S3StorageManager.java

License:Open Source License

/**
 * Deletes the specified S3 object from the S3 storage service.  If a
 * storage path is passed in that has child S3 objects, it will recursively
 * delete the underlying objects./*  w w w . ja  v a  2s .co  m*/
 * @param s3Store the s3 object to be deleted
 */
public void delete(AwscrudStorageObject s3Store) {

    if (s3Store.getStoragePath() == null || s3Store.getStoragePath().equals("")) {
        logger.log(Level.WARNING, "Empty storage path passed to delete method");
        return; // We don't want to delete everything in a path
    }

    // Go through the store structure and delete child objects
    ObjectListing listing = s3client.listObjects(s3Store.getBucketName(), s3Store.getStoragePath());
    while (true) {
        List<S3ObjectSummary> objectList = listing.getObjectSummaries();
        for (S3ObjectSummary summary : objectList) {
            s3client.deleteObject(s3Store.getBucketName(), summary.getKey());
        }
        if (listing.isTruncated()) {
            listing = s3client.listNextBatchOfObjects(listing);
        } else {
            break;
        }
    }

}

From source file:com.cirrus.server.osgi.service.amazon.s3.AmazonS3StorageService.java

License:Apache License

@Override
public List<ICirrusData> list(final String path) throws ServiceRequestFailedException {
    final List<ICirrusData> content = new ArrayList<>();
    final ListObjectsRequest listObjectsRequest = this.buildObjectRequest(path);

    final AtomicReference<ObjectListing> objectListing = new AtomicReference<>();
    do {/*from w w  w. j a v  a  2s.  com*/
        objectListing.set(this.amazonS3Client.listObjects(listObjectsRequest));
        for (final S3ObjectSummary objectSummary : objectListing.get().getObjectSummaries()) {
            final String key = objectSummary.getKey();

            if (path.equals(SEPARATOR)) {
                // root directory
                if (!key.contains(SEPARATOR)) {
                    content.add(new CirrusFileData(SEPARATOR + key, objectSummary.getSize()));
                } else {
                    if (key.indexOf(SEPARATOR) == key.length() - 1) {
                        content.add(new CirrusFolderData(key));
                    }
                }
            } else {
                final int beginIndex = key.indexOf(SEPARATOR);
                final String substring = key.substring(beginIndex + 1, key.length());
                if (!substring.isEmpty()) {
                    final ICirrusData cirrusData;
                    if (substring.endsWith(SEPARATOR)) {
                        cirrusData = new CirrusFolderData(substring);
                    } else {
                        cirrusData = new CirrusFileData(SEPARATOR + substring, objectSummary.getSize());
                    }

                    content.add(cirrusData);
                }
            }
        }
        listObjectsRequest.setMarker(objectListing.get().getNextMarker());
    } while (objectListing.get().isTruncated());

    return content;
}

From source file:com.clicktravel.infrastructure.persistence.aws.s3.S3FileStore.java

License:Apache License

@Override
public List<FilePath> list(final String directory, final String prefix) {

    try {//ww  w . j  a  v  a 2  s  . c  om

        final List<FilePath> filePathList = new ArrayList<FilePath>();
        final ObjectListing objectListing = amazonS3Client.listObjects(bucketNameForDirectory(directory),
                prefix);

        final List<S3ObjectSummary> s3objectSummaries = objectListing.getObjectSummaries();
        for (final S3ObjectSummary s3ObjectSummary : s3objectSummaries) {
            final FilePath filePath = new FilePath(directory, s3ObjectSummary.getKey());
            filePathList.add(filePath);
        }

        return filePathList;

    } catch (final AmazonS3Exception e) {
        throw new PersistenceResourceFailureException("An error occurred obtaining a listing of directory -> "
                + directory + " with prefix -> " + prefix, e);
    }
}

From source file:com.climate.oada.dao.impl.S3ResourceDAO.java

License:Open Source License

@Override
public List<FileResource> getFileUrls(Long userId, String type) {
    List<FileResource> retval = new ArrayList<FileResource>();
    long validfor = new Long(validHours).longValue() * HOURS_TO_MILLISECONDS;
    try {//from w  w  w. ja  va2s.c  o m
        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        String prefix = userId.toString() + S3_SEPARATOR + type;

        LOG.debug("Listing objects from bucket " + bucketName + " with prefix " + prefix);

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName)
                .withPrefix(prefix);
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                LOG.debug(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");

                Date expiration = new Date();
                long milliSeconds = expiration.getTime();
                milliSeconds += validfor;
                expiration.setTime(milliSeconds);

                GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                        bucketName, objectSummary.getKey());
                generatePresignedUrlRequest.setMethod(HttpMethod.GET);
                generatePresignedUrlRequest.setExpiration(expiration);

                FileResource res = new FileResource();
                res.setFileURL(s3client.generatePresignedUrl(generatePresignedUrlRequest));
                retval.add(res);
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonServiceException ase) {
        logAWSServiceException(ase);
    } catch (AmazonClientException ace) {
        logAWSClientException(ace);
    } catch (Exception e) {
        LOG.error("Unable to retrieve S3 file URLs " + e.getMessage());
    }
    return retval;
}

From source file:com.cloud.utils.S3Utils.java

License:Apache License

public static List<File> getDirectory(final ClientOptions clientOptions, final String bucketName,
        final String sourcePath, final File targetDirectory, final FileNamingStrategy namingStrategy) {

    assert clientOptions != null;
    assert isNotBlank(bucketName);
    assert isNotBlank(sourcePath);
    assert targetDirectory != null;

    final AmazonS3 connection = acquireClient(clientOptions);

    // List the objects in the source directory on S3
    final List<S3ObjectSummary> objectSummaries = listDirectory(bucketName, sourcePath, connection);
    final List<File> files = new ArrayList<File>();

    for (final S3ObjectSummary objectSummary : objectSummaries) {

        files.add(getFile(clientOptions, bucketName, objectSummary.getKey(), targetDirectory, namingStrategy));

    }//from w w  w.jav a 2s.  c o  m

    return unmodifiableList(files);

}