Example usage for com.amazonaws.services.s3.model S3Object getBucketName

List of usage examples for com.amazonaws.services.s3.model S3Object getBucketName

Introduction

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

Prototype

public String getBucketName() 

Source Link

Document

Gets the name of the bucket in which this object is contained.

Usage

From source file:org.cloudifysource.esc.driver.provisioning.privateEc2.PrivateEC2CloudifyDriver.java

License:Open Source License

private Instance createEC2Instance(final PrivateEc2Template cfnTemplate, final ProvisioningContextImpl ctx,
        final boolean management, final String machineName, final long duration, final TimeUnit unit)
        throws CloudProvisioningException, TimeoutException {

    final InstanceProperties properties = cfnTemplate.getEC2Instance().getProperties();

    final String availabilityZone = properties.getAvailabilityZone() == null ? null
            : properties.getAvailabilityZone().getValue();
    final Placement placement = availabilityZone == null ? null : new Placement(availabilityZone);

    final String imageId = properties.getImageId() == null ? null : properties.getImageId().getValue();
    final String instanceType = properties.getInstanceType() == null ? null
            : properties.getInstanceType().getValue();
    final String keyName = properties.getKeyName() == null ? null : properties.getKeyName().getValue();
    final String privateIpAddress = properties.getPrivateIpAddress() == null ? null
            : properties.getPrivateIpAddress().getValue();
    final List<String> securityGroupIds = properties.getSecurityGroupIdsAsString();
    final List<String> securityGroups = properties.getSecurityGroupsAsString();

    S3Object s3Object = null;
    try {/*w  w w  .ja  va  2s . c o m*/

        String userData = null;
        if (properties.getUserData() != null) {
            // Generate ENV script for the provisioned machine
            final StringBuilder sb = new StringBuilder();
            final String script = management ? this.generateManagementCloudifyEnv(ctx)
                    : this.generateCloudifyEnv(ctx);

            s3Object = this.uploadCloudDir(ctx, script, management);
            final String cloudFileS3 = this.amazonS3Uploader.generatePresignedURL(s3Object);

            ComputeTemplate template = this.getManagerComputeTemplate();
            String cloudFileDir = (String) template.getRemoteDirectory();
            // Remove '/' from the path if it's the last char.
            if (cloudFileDir.length() > 1 && cloudFileDir.endsWith("/")) {
                cloudFileDir = cloudFileDir.substring(0, cloudFileDir.length() - 1);
            }
            final String endOfLine = " >> /tmp/cloud.txt\n";
            sb.append("#!/bin/bash\n");
            sb.append("export TMP_DIRECTORY=/tmp").append(endOfLine);
            sb.append("export S3_ARCHIVE_FILE='" + cloudFileS3 + "'").append(endOfLine);
            sb.append("wget -q -O $TMP_DIRECTORY/cloudArchive.tar.gz $S3_ARCHIVE_FILE").append(endOfLine);
            sb.append("mkdir -p " + cloudFileDir).append(endOfLine);
            sb.append("tar zxvf $TMP_DIRECTORY/cloudArchive.tar.gz -C " + cloudFileDir).append(endOfLine);
            sb.append("rm -f $TMP_DIRECTORY/cloudArchive.tar.gz").append(endOfLine);
            sb.append("echo ").append(cloudFileDir).append("/").append(CLOUDIFY_ENV_SCRIPT).append(endOfLine);
            sb.append("chmod 755 ").append(cloudFileDir).append("/").append(CLOUDIFY_ENV_SCRIPT)
                    .append(endOfLine);
            sb.append("source ").append(cloudFileDir).append("/").append(CLOUDIFY_ENV_SCRIPT).append(endOfLine);

            sb.append(properties.getUserData().getValue());
            userData = sb.toString();
            logger.fine("Instanciate ec2 with user data:\n" + userData);
            userData = StringUtils.newStringUtf8(Base64.encodeBase64(userData.getBytes()));
        }

        List<BlockDeviceMapping> blockDeviceMappings = null;
        AWSEC2Volume volumeConfig = null;
        if (properties.getVolumes() != null) {
            blockDeviceMappings = new ArrayList<BlockDeviceMapping>(properties.getVolumes().size());
            for (final VolumeMapping volMapping : properties.getVolumes()) {
                volumeConfig = cfnTemplate.getEC2Volume(volMapping.getVolumeId().getValue());
                blockDeviceMappings
                        .add(this.createBlockDeviceMapping(volMapping.getDevice().getValue(), volumeConfig));
            }
        }

        final RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
        runInstancesRequest.withPlacement(placement);
        runInstancesRequest.withImageId(imageId);
        runInstancesRequest.withInstanceType(instanceType);
        runInstancesRequest.withKeyName(keyName);
        runInstancesRequest.withPrivateIpAddress(privateIpAddress);
        runInstancesRequest.withSecurityGroupIds(securityGroupIds);
        runInstancesRequest.withSecurityGroups(securityGroups);
        runInstancesRequest.withMinCount(1);
        runInstancesRequest.withMaxCount(1);
        runInstancesRequest.withBlockDeviceMappings(blockDeviceMappings);
        runInstancesRequest.withUserData(userData);

        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("EC2::Instance request=" + runInstancesRequest);
        }

        final RunInstancesResult runInstances = this.ec2.runInstances(runInstancesRequest);
        if (runInstances.getReservation().getInstances().size() != 1) {
            throw new CloudProvisioningException(
                    "Request runInstace fails (request=" + runInstancesRequest + ").");
        }

        Instance ec2Instance = runInstances.getReservation().getInstances().get(0);
        ec2Instance = this.waitRunningInstance(ec2Instance, duration, unit);
        this.tagEC2Instance(ec2Instance, machineName, cfnTemplate.getEC2Instance());
        this.tagEC2Volumes(ec2Instance.getInstanceId(), cfnTemplate);
        this.waitRunningAgent(ec2Instance.getPublicIpAddress(), duration, unit);

        return ec2Instance;
    } finally {
        if (s3Object != null) {
            this.amazonS3Uploader.deleteS3Object(s3Object.getBucketName(), s3Object.getKey());
        }
    }
}

From source file:org.openflamingo.fs.s3.S3DirectoryInfo.java

License:Apache License

/**
 *  ??/*from w w w .  j  a va  2s. c  o m*/
 *
 * @param object Amazon S3 Object
 */
public S3DirectoryInfo(S3Object object) {
    this.fullyQualifiedPath = object.getBucketName() + "/" + object.getKey();
    this.path = FileUtils.getPath(this.fullyQualifiedPath);
    this.length = object.getObjectMetadata().getContentLength();
    this.modificationTime = object.getObjectMetadata().getLastModified().getTime();
    this.accesTime = object.getObjectMetadata().getLastModified().getTime();
}

From source file:org.openflamingo.fs.s3.S3ObjectInfo.java

License:Apache License

/**
 *  ??//from ww w  .  ja va  2 s  .c om
 *
 * @param object Amazon S3 Object
 */
public S3ObjectInfo(S3Object object, String filename) {
    this.fullyQualifiedPath = "/" + object.getBucketName() + "/" + object.getKey();
    this.filename = filename;
    this.path = FileUtils.getPath(this.fullyQualifiedPath);
    this.length = object.getObjectMetadata().getContentLength();
    this.modificationTime = object.getObjectMetadata().getLastModified().getTime();
    this.accesTime = object.getObjectMetadata().getLastModified().getTime();
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  ./*from  w ww. j  a  v  a  2 s .co m*/
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getObject(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    Map<String, String> map = new HashMap<String, String>();

    if (!object.getKey().endsWith("/")) {
        String qualifiedPath = "/" + bucketName + "/" + object.getKey();
        map.put("bucketName", object.getBucketName());
        map.put("name", FileUtils.getFilename(qualifiedPath));
        map.put("path", qualifiedPath);
    } else {
        map.put("bucketName", object.getBucketName());
        map.put("name", object.getKey());
        map.put("name", "/" + bucketName + "/" + object.getKey());
    }

    setValue("redirectionLocation", object.getRedirectLocation(), map);
    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return map;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  .//from  ww  w  .  j  a  v  a2 s.  co m
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getDirectory(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    List<FileInfo> filesList = new ArrayList<FileInfo>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(object.getBucketName())
            .withPrefix(objectKey).withDelimiter("/");

    ObjectListing objectListing = null;

    do {
        objectListing = client.listObjects(listObjectsRequest);
        List<String> commonPrefixes = objectListing.getCommonPrefixes();
        List<S3ObjectSummary> summary = objectListing.getObjectSummaries();
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());

    Map<String, String> map = new HashMap<String, String>();

    map.put("bucketName", object.getBucketName());
    map.put("name", object.getKey());
    map.put("redirectionLocation", object.getRedirectLocation());

    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return null;
}

From source file:org.zalando.stups.fullstop.controller.S3Controller.java

License:Apache License

@RequestMapping(method = RequestMethod.GET, value = "/download")
public void downloadFiles(@RequestParam(value = "bucket") final String bucket,
        @RequestParam(value = "location") final String location, @RequestParam(value = "page") final int page) {

    try {//  w  w w  . ja  v a  2  s . c  o m
        log.info("Creating fullstop directory here: {}", fullstopLoggingDir);

        boolean mkdirs = new File(fullstopLoggingDir).mkdirs();
    } catch (SecurityException e) {
        // do nothing
    }

    AmazonS3Client amazonS3Client = new AmazonS3Client();
    amazonS3Client.setRegion(Region.getRegion(Regions
            .fromName((String) cloudTrailProcessingLibraryProperties.getAsProperties().get(S3_REGION_KEY))));

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket) //
            .withPrefix(location) //
            .withMaxKeys(page);

    ObjectListing objectListing = amazonS3Client.listObjects(listObjectsRequest);

    final List<S3ObjectSummary> s3ObjectSummaries = objectListing.getObjectSummaries();

    while (objectListing.isTruncated()) {

        objectListing = amazonS3Client.listNextBatchOfObjects(objectListing);
        s3ObjectSummaries.addAll(objectListing.getObjectSummaries());

    }

    for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
        String bucketName = s3ObjectSummary.getBucketName();
        String key = s3ObjectSummary.getKey();

        S3Object object = amazonS3Client.getObject(new GetObjectRequest(bucketName, key));
        InputStream inputStream = object.getObjectContent();

        File file = new File(fullstopLoggingDir,
                object.getBucketName() + object.getObjectMetadata().getETag() + JSON_GZ);

        copyInputStreamToFile(inputStream, file);
        log.info("File saved here: {}", file.getAbsolutePath());

    }
}

From source file:oulib.aws.s3.S3Util.java

/**
 * Pull out Tiff metadata from input S3 object and inject into the 
 * content of target S3 Object;<br>
 * Generate the new output S3 object that has the metadata from input object.
 * // ww w . ja  v a  2s . co  m
 * @param s3client : S3 client
 * @param obj1 : input object that provides metadata
 * @param obj2 : target object that receives metadata
 * 
 * @return PutObjectResult
 */
public static PutObjectResult copyS3ObjectTiffMetadata(AmazonS3 s3client, S3Object obj1, S3Object obj2) {

    PutObjectResult result = null;

    BufferedInputStream bufferedInputStrean = null;
    ByteArrayOutputStream byteArrayOutputStream = null;
    ByteArrayInputStream byteArrayInputStream = null;
    ByteArrayInputStream bis = null;
    S3ObjectInputStream content1 = null;
    S3ObjectInputStream content2 = null;
    String targetBucketName = obj2.getBucketName();
    String outputKey = obj2.getKey().split(".tif")[0] + "-copied.tif";

    ImageMetadata metadata1, metadata2;
    TiffImageMetadata tiffMetadata1, tiffMetadata2;
    TiffOutputSet output1, output2;

    try {
        content1 = obj1.getObjectContent();
        content2 = obj2.getObjectContent();

        byte[] bytes1 = IOUtils.toByteArray(content1);
        byte[] bytes2 = IOUtils.toByteArray(content2);

        metadata1 = Imaging.getMetadata(bytes1);
        metadata2 = Imaging.getMetadata(bytes2);

        tiffMetadata1 = (TiffImageMetadata) metadata1;
        tiffMetadata2 = (TiffImageMetadata) metadata2;

        output1 = tiffMetadata1.getOutputSet();
        output2 = tiffMetadata2.getOutputSet();

        TiffOutputDirectory rootDir = output2.getOrCreateRootDirectory();
        TiffOutputDirectory exifDir = output2.getOrCreateExifDirectory();
        TiffOutputDirectory gpsDir = output2.getOrCreateGPSDirectory();

        if (null != output1.getRootDirectory()) {
            List<TiffOutputField> fs = output1.getRootDirectory().getFields();
            for (TiffOutputField f1 : fs) {
                if (null == rootDir.findField(f1.tag)
                        // CANNOT create the output image with this tag included!
                        && !"PlanarConfiguration".equals(f1.tagInfo.name)) {
                    rootDir.add(f1);
                }
            }
        }

        if (null != output1.getExifDirectory()) {
            for (TiffOutputField f2 : output1.getExifDirectory().getFields()) {
                exifDir.removeField(f2.tagInfo);
                exifDir.add(f2);
            }
        }

        if (null != output1.getGPSDirectory()) {
            for (TiffOutputField f3 : output1.getGPSDirectory().getFields()) {
                gpsDir.removeField(f3.tagInfo);
                gpsDir.add(f3);
            }
        }

        byteArrayOutputStream = new ByteArrayOutputStream();
        TiffImageWriterLossy writerLossy = new TiffImageWriterLossy(output2.byteOrder);
        writerLossy.write(byteArrayOutputStream, output2);

        byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(byteArrayOutputStream.toByteArray().length);
        metadata.setContentType("image/tiff");
        metadata.setLastModified(new Date());

        result = s3client
                .putObject(new PutObjectRequest(targetBucketName, outputKey, byteArrayInputStream, metadata));

    } catch (ImageReadException | IOException | ImageWriteException ex) {
        Logger.getLogger(S3Util.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (null != content1) {
                content1.close();
            }
            if (null != content2) {
                content2.close();
            }
            if (null != bufferedInputStrean) {
                bufferedInputStrean.close();
            }
            if (null != byteArrayInputStream) {
                byteArrayInputStream.close();
            }
            if (null != byteArrayOutputStream) {
                byteArrayOutputStream.close();
            }
            if (null != bis) {
                bis.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(S3Util.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return result;
}