Example usage for com.amazonaws.services.s3.model PutObjectRequest setStorageClass

List of usage examples for com.amazonaws.services.s3.model PutObjectRequest setStorageClass

Introduction

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

Prototype

public void setStorageClass(String storageClass) 

Source Link

Document

Sets the optional Amazon S3 storage class to use when storing the new object.

Usage

From source file:org.entando.entando.plugins.jps3awsclient.aps.system.services.storage.AmazonS3StorageManager.java

License:Open Source License

public void store(IStorageObject obj, boolean reducedRedundancy, CannedAccessControlList acl)
        throws ApsSystemException {
    try {/* www. j  ava 2 s. c o m*/
        AmazonS3Client client = this.getS3Client();
        String bucketName = obj.getBucketName().toLowerCase();
        this.checkForAndCreateBucket(bucketName, client);
        ObjectMetadata omd = new ObjectMetadata();
        omd.setContentType(obj.getContentType());
        omd.setContentLength(obj.getContentLength());
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, obj.getStoragePath(),
                obj.getInputStream(), omd);
        // Check if reduced redundancy is enabled
        if (reducedRedundancy) {
            putObjectRequest.setStorageClass(StorageClass.ReducedRedundancy);
        }
        if (null != obj.getUserMetadata()) {
            ObjectMetadata objectMetadata = new ObjectMetadata();
            putObjectRequest.setMetadata(objectMetadata);
            Iterator<String> iter = obj.getUserMetadata().keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                objectMetadata.addUserMetadata(key, obj.getUserMetadata().get(key));
            }
        }
        client.putObject(putObjectRequest);
        // If we have an ACL set access permissions for the the data on S3
        if (acl != null) {
            client.setObjectAcl(bucketName, obj.getStoragePath(), acl);
        }
    } catch (Throwable t) {
        _logger.error("Error storing object", t);
        throw new ApsSystemException("Error storing object", t);
    }
}

From source file:org.mobicents.servlet.restcomm.amazonS3.S3AccessTool.java

License:Open Source License

public URI uploadFile(final String fileToUpload) {
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, securityKey);
    AmazonS3 s3client = new AmazonS3Client(credentials);
    try {//from   w w w  . j ava 2s  .com
        StringBuffer bucket = new StringBuffer();
        bucket.append(bucketName);
        if (folder != null && !folder.isEmpty())
            bucket.append("/").append(folder);
        URI fileUri = URI.create(fileToUpload);
        logger.info("File to upload to S3: " + fileUri.toString());
        File file = new File(fileUri);
        //            while (!file.exists()){}
        //            logger.info("File exist: "+file.exists());
        //First generate the Presigned URL, buy some time for the file to be written on the disk
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (daysToRetainPublicUrl > 0) {
            cal.add(Calendar.DATE, daysToRetainPublicUrl);
        } else {
            //By default the Public URL will be valid for 180 days
            cal.add(Calendar.DATE, 180);
        }
        date = cal.getTime();
        GeneratePresignedUrlRequest generatePresignedUrlRequestGET = new GeneratePresignedUrlRequest(
                bucket.toString(), file.getName());
        generatePresignedUrlRequestGET.setMethod(HttpMethod.GET);
        generatePresignedUrlRequestGET.setExpiration(date);

        URL downloadUrl = s3client.generatePresignedUrl(generatePresignedUrlRequestGET);

        //Second upload the file to S3
        //            while (!file.exists()){}
        while (!FileUtils.waitFor(file, 30)) {
        }
        if (file.exists()) {
            PutObjectRequest putRequest = new PutObjectRequest(bucket.toString(), file.getName(), file);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(new MimetypesFileTypeMap().getContentType(file));
            putRequest.setMetadata(metadata);
            if (reducedRedundancy)
                putRequest.setStorageClass(StorageClass.ReducedRedundancy);
            s3client.putObject(putRequest);

            if (removeOriginalFile) {
                removeLocalFile(file);
            }
            return downloadUrl.toURI();
        } else {
            logger.error("Timeout waiting for the recording file: " + file.getAbsolutePath());
            return null;
        }
    } catch (AmazonServiceException ase) {
        logger.error("Caught an AmazonServiceException");
        logger.error("Error Message:    " + ase.getMessage());
        logger.error("HTTP Status Code: " + ase.getStatusCode());
        logger.error("AWS Error Code:   " + ase.getErrorCode());
        logger.error("Error Type:       " + ase.getErrorType());
        logger.error("Request ID:       " + ase.getRequestId());
        return null;
    } catch (AmazonClientException ace) {
        logger.error("Caught an AmazonClientException, which ");
        logger.error("Error Message: " + ace.getMessage());
        return null;
    } catch (URISyntaxException e) {
        logger.error("URISyntaxException: " + e.getMessage());
        return null;
    }
}

From source file:org.mule.module.s3.simpleapi.SimpleAmazonS3AmazonDevKitImpl.java

License:Open Source License

@Override
public String createObject(@NotNull S3ObjectId objectId, @NotNull S3ObjectContent content, String contentType,
        String contentDisposition, CannedAccessControlList acl, StorageClass storageClass,
        Map<String, String> userMetadata, String encryption) {
    Validate.notNull(content);/*from   w ww  .j a v a  2s .c o m*/
    PutObjectRequest request = content.createPutObjectRequest();

    if (request.getMetadata() != null) {
        request.getMetadata().setContentType(contentType);
        if (StringUtils.isNotBlank(contentDisposition)) {
            request.getMetadata().setContentDisposition(contentDisposition);
        }
        if (encryption != null) {
            request.getMetadata().setServerSideEncryption(encryption);
        }
    }
    request.getMetadata().setUserMetadata(userMetadata);
    request.setBucketName(objectId.getBucketName());
    request.setKey(objectId.getKey());
    request.setCannedAcl(acl);
    if (storageClass != null) {
        request.setStorageClass(storageClass);
    }
    return s3.putObject(request).getVersionId();
}

From source file:org.restcomm.connect.commons.amazonS3.S3AccessTool.java

License:Open Source License

public URI uploadFile(final String fileToUpload) {
    if (s3client == null) {
        s3client = getS3client();/*from   w w w. j av a2  s  .c  o m*/
    }
    if (logger.isInfoEnabled()) {
        logger.info("S3 Region: " + bucketRegion.toString());
    }
    try {
        if (testing && (!testingUrl.isEmpty() || !testingUrl.equals(""))) {
            //                s3client.setEndpoint(testingUrl);
            //                s3client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
            FileUtils.touch(new File(URI.create(fileToUpload)));
        }
        StringBuffer bucket = new StringBuffer();
        bucket.append(bucketName);
        if (folder != null && !folder.isEmpty())
            bucket.append("/").append(folder);
        URI fileUri = URI.create(fileToUpload);
        if (logger.isInfoEnabled()) {
            logger.info("File to upload to S3: " + fileUri.toString());
        }
        File file = new File(fileUri);

        while (!FileUtils.waitFor(file, 30)) {
        }
        if (file.exists()) {
            PutObjectRequest putRequest = new PutObjectRequest(bucket.toString(), file.getName(), file);
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(new MimetypesFileTypeMap().getContentType(file));
            putRequest.setMetadata(metadata);
            if (reducedRedundancy)
                putRequest.setStorageClass(StorageClass.ReducedRedundancy);
            s3client.putObject(putRequest);

            if (removeOriginalFile) {
                removeLocalFile(file);
            }
            URI recordingS3Uri = s3client.getUrl(bucket.toString(), file.getName()).toURI();
            return recordingS3Uri;
            //                return downloadUrl.toURI();
        } else {
            logger.error("Timeout waiting for the recording file: " + file.getAbsolutePath());
            return null;
        }
    } catch (AmazonServiceException ase) {
        logger.error("Caught an AmazonServiceException");
        logger.error("Error Message:    " + ase.getMessage());
        logger.error("HTTP Status Code: " + ase.getStatusCode());
        logger.error("AWS Error Code:   " + ase.getErrorCode());
        logger.error("Error Type:       " + ase.getErrorType());
        logger.error("Request ID:       " + ase.getRequestId());
        return null;
    } catch (AmazonClientException ace) {
        logger.error("Caught an AmazonClientException ");
        logger.error("Error Message: " + ace.getMessage());
        return null;
    } catch (URISyntaxException e) {
        logger.error("URISyntaxException: " + e.getMessage());
        return null;
    } catch (IOException e) {
        logger.error("Problem while trying to touch recording file for testing", e);
        return null;
    }
}

From source file:raymond.mockftpserver.S3BucketFileSystem.java

License:Apache License

@Override
public void add(FileSystemEntry entry) {
    ObjectMetadata metaData = new ObjectMetadata();
    PutObjectRequest request;
    if (isDirectory(entry)) {
        metaData.setContentLength(0);//from  w  ww  .j a va 2 s  .c o  m
        InputStream is = new ByteArrayInputStream(new byte[0]);
        request = new PutObjectRequest(bucket, entry.getPath() + FOLDER_SUFFIX, is, metaData);
    } else {
        metaData.setContentLength(entry.getSize());
        request = new PutObjectRequest(bucket, entry.getPath(), ((FileEntry) entry).createInputStream(),
                metaData);
    }
    request.setStorageClass(StorageClass.ReducedRedundancy);
    s3.putObject(request);
}