Example usage for com.amazonaws.services.s3.model ObjectMetadata ObjectMetadata

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata ObjectMetadata

Introduction

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

Prototype

public ObjectMetadata() 

Source Link

Usage

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

License:Open Source License

/**
 * Stores a given item on S3//from ww w  .  j  a va2  s.co  m
 * @param obj the data to be stored
 * @param reducedRedundancy whether or not to use reduced redundancy storage
 * @param acl a canned access control list indicating what permissions to store this object with (can be null to leave it set to default)
 */
public void store(AwscrudStorageObject obj, boolean reducedRedundancy, CannedAccessControlList acl) {
    // Make sure the bucket exists before we try to use it
    checkForAndCreateBucket(obj.getBucketName());

    ObjectMetadata omd = new ObjectMetadata();
    omd.setContentType(obj.getMimeType());
    omd.setContentLength(obj.getData().length);

    ByteArrayInputStream is = new ByteArrayInputStream(obj.getData());
    PutObjectRequest request = new PutObjectRequest(obj.getBucketName(), obj.getStoragePath(), is, omd);

    // Check if reduced redundancy is enabled
    if (reducedRedundancy) {
        request.setStorageClass(StorageClass.ReducedRedundancy);
    }

    s3client.putObject(request);

    // If we have an ACL set access permissions for the the data on S3
    if (acl != null) {
        s3client.setObjectAcl(obj.getBucketName(), obj.getStoragePath(), acl);
    }

}

From source file:com.BoomPi.ImageResizeHandlerFromS3.java

License:Apache License

private String putResizedImageToS3(ImageProcess jpegImageProcess, String s3ObjectKey, int width, int height)
        throws IOException {
    ByteArrayOutputStream os = jpegImageProcess.resize(width, height).getOutPutStream();
    try (InputStream is = new ByteArrayInputStream(os.toByteArray());) {

        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(os.size());
        meta.setContentType(jpegImageProcess.getImageMime());

        String dstKey = String.join("_", "resized", s3ObjectKey);

        AmazonS3 s3Client = new AmazonS3Client();

        System.out.println("Writing to: " + dstBucket + "/" + dstKey);
        s3Client.putObject(dstBucket, dstKey, is, meta);
    }/*from  w w  w  .j  a  v  a2 s.co  m*/
    return "Ok";
}

From source file:com.casadocodigo.ecommerce.infra.AmazonFileSaver.java

public String write(String baseFolder, Part multipartFile) throws IOException {

    String fileName = extractFilename(multipartFile.getHeader(CONTENT_DISPOSITION));

    String path = baseFolder + File.separator + fileName;

    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

    ObjectMetadata metaData = new ObjectMetadata();
    byte[] bytes = IOUtils.toByteArray(multipartFile.getInputStream());
    metaData.setContentLength(bytes.length);
    /*ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, byteArrayInputStream, metadata);
    client.putObject(putObjectRequest);*/

    s3client.putObject(new PutObjectRequest(BUCKET_NAME, path, multipartFile.getInputStream(), metaData)
            .withCannedAcl(CannedAccessControlList.PublicRead));

    /*s3client.putObject(BUCKET_NAME,fileName, 
        multipartFile.getInputStream(), metaData);*/
    return END_POINT + File.separator + BUCKET_NAME + File.separator + path;

}

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

License:Apache License

@Override
public CirrusFolderData createDirectory(final String path) throws ServiceRequestFailedException {
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(0);//from ww  w.jav a  2  s  . c o  m
    final InputStream emptyContent = new ByteArrayInputStream(new byte[0]);

    final String key = path + SEPARATOR;
    final PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, key, emptyContent, metadata);

    // Send request to S3 to create folder
    this.amazonS3Client.putObject(putObjectRequest);

    return new CirrusFolderData(key);
}

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

License:Apache License

@Override
public CirrusFileData transferFile(final String filePath, final long fileSize, final InputStream inputStream)
        throws ServiceRequestFailedException {
    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(fileSize);
    this.amazonS3Client.putObject(BUCKET_NAME, filePath, inputStream, metadata);
    return new CirrusFileData(SEPARATOR + filePath, fileSize);
}

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

License:Apache License

@Override
public void write(final FilePath filePath, final FileItem fileItem) {
    checkInitialization();/*from  w  w w.  j a v a  2s .c o m*/

    final ObjectMetadata metadata = new ObjectMetadata();
    metadata.addUserMetadata(USER_METADATA_FILENAME, fileItem.filename());
    metadata.addUserMetadata(USER_METADATA_LAST_UPDATED_TIME, formatter.print(fileItem.lastUpdatedTime()));
    metadata.setContentLength(fileItem.getBytes().length);
    final InputStream is = new ByteArrayInputStream(fileItem.getBytes());
    final String bucketName = bucketNameForFilePath(filePath);
    if (!amazonS3Client.doesBucketExist(bucketName)) {
        createBucket(bucketName);
    }
    final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filePath.filename(), is,
            metadata);
    amazonS3Client.putObject(putObjectRequest);
}

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

License:Apache License

public static boolean canReadWriteBucket(final ClientOptions clientOptions, final String bucketName) {

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

    try {/*  w w  w.  j av  a  2s.c  o m*/

        final AmazonS3 client = acquireClient(clientOptions);

        final String fileContent = "testing put and delete";
        final InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes());
        final String key = UUID.randomUUID().toString() + ".txt";

        final ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(fileContent.length());

        client.putObject(bucketName, key, inputStream, metadata);
        client.deleteObject(bucketName, key);

        return true;

    } catch (AmazonClientException e) {

        return false;

    }

}

From source file:com.cloudbees.demo.beesshop.web.ProductController.java

License:Apache License

/**
 * @param id    id of the product//from w  w  w  .j  a  va 2 s . c o m
 * @param photo to associate with the product
 * @return redirection to display product
 */
@RequestMapping(value = "/product/{id}/photo", method = RequestMethod.POST)
@Transactional
public String updatePhoto(@PathVariable long id, @RequestParam("photo") MultipartFile photo) {

    if (photo.getSize() == 0) {
        logger.info("Empty uploaded file");
    } else {
        try {
            String contentType = fileStorageService.findContentType(photo.getOriginalFilename());
            if (contentType == null) {
                logger.warn("Skip file with unsupported extension '{}'", photo.getName());
            } else {

                InputStream photoInputStream = photo.getInputStream();
                long photoSize = photo.getSize();

                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentLength(photoSize);
                objectMetadata.setContentType(contentType);
                objectMetadata
                        .setCacheControl("public, max-age=" + TimeUnit.SECONDS.convert(365, TimeUnit.DAYS));
                String photoUrl = fileStorageService.storeFile(photoInputStream, objectMetadata);

                Product product = productRepository.get(id);
                logger.info("Saved {}", photoUrl);
                product.setPhotoUrl(photoUrl);
                productRepository.update(product);
            }

        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    }
    return "redirect:/product/" + id;
}

From source file:com.cloudbees.plugins.binarydeployer.s3.S3Repository.java

License:Open Source License

private PutObjectRequest prepareUpload(VirtualFile file, String name) throws IOException {
    log.debug("Preparing upload for " + name + " to S3::" + bucketName);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(file.length());

    return new PutObjectRequest(bucketName, name, file.open(), metadata);
}

From source file:com.dateofrock.simpledbmapper.s3.S3Task.java

License:Apache License

@Override
public S3TaskResult call() throws Exception {
    S3TaskResult taskResult = new S3TaskResult(Operation.UPLOAD, this.simpleDBAttributeName, this.bucketName,
            this.key);
    try {/*from  w w  w  .  ja  va2  s.c om*/
        ObjectMetadata meta = null;
        if (this.contentType != null) {
            meta = new ObjectMetadata();
            meta.setContentType(this.contentType);
        }
        this.s3.putObject(this.bucketName, this.key, this.input, meta);
        taskResult.setSuccess(true);
    } catch (Exception e) {
        taskResult.setSuccess(false);
        taskResult.setS3Exception(e);
    } finally {
        IOUtils.closeQuietly(this.input);
    }
    return taskResult;
}