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:example.uploads3.UploadS3.java

License:Apache License

public static void main(String[] args) throws Exception {
    String uploadFileName = args[0];
    String bucketName = "haos3";
    String keyName = "test/byspark.txt";
    // Create a Java Spark Context.
    SparkConf conf = new SparkConf().setAppName("UploadS3");
    JavaSparkContext sc = new JavaSparkContext(conf);

    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {//from ww w .ja va2  s .  c  o m
        System.out.println("Uploading a new object to S3 from a file\n");
        File file = new File(uploadFileName);
        PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, file);

        // Request server-side encryption.
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setServerSideEncryption("AES256");
        putRequest.setMetadata(objectMetadata);

        s3client.putObject(putRequest);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                + "to Amazon S3, but was rejected with an error response" + " for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
                + "an internal error while trying to " + "communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:fi.yle.tools.aws.maven.SimpleStorageServiceWagon.java

License:Apache License

@Override
protected void putResource(File source, String destination, TransferProgress transferProgress)
        throws TransferFailedException, ResourceDoesNotExistException {
    String key = getKey(destination);

    mkdirs(key, 0);/*  w  ww  .  j  av  a 2s.c  om*/

    InputStream in = null;
    try {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(source.length());
        objectMetadata.setContentType(Mimetypes.getInstance().getMimetype(source));

        in = new TransferProgressFileInputStream(source, transferProgress);

        this.amazonS3.putObject(new PutObjectRequest(this.bucketName, key, in, objectMetadata)
                .withCannedAcl(CannedAccessControlList.BucketOwnerFullControl));
    } catch (AmazonServiceException e) {
        throw new TransferFailedException(String.format("Cannot write file to '%s'", destination), e);
    } catch (FileNotFoundException e) {
        throw new ResourceDoesNotExistException(String.format("Cannot read file from '%s'", source), e);
    } finally {
        IoUtils.closeQuietly(in);
    }
}

From source file:fi.yle.tools.aws.maven.SimpleStorageServiceWagon.java

License:Apache License

private PutObjectRequest createDirectoryPutObjectRequest(String key) {
    ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);

    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(0);// ww w.j a v a 2 s  . c o m

    return new PutObjectRequest(this.bucketName, key, inputStream, objectMetadata)
            .withCannedAcl(CannedAccessControlList.BucketOwnerFullControl);
}

From source file:fr.eurecom.hybris.kvs.drivers.AmazonKvs.java

License:Apache License

public void put(String key, byte[] value) throws IOException {
    try {//from   ww w.  j  a va2  s  .c  o m
        ByteArrayInputStream bais = new ByteArrayInputStream(value);

        ObjectMetadata om = new ObjectMetadata();
        om.setContentLength(value.length);

        PutObjectRequest request = new PutObjectRequest(this.rootContainer, key, bais, om);
        request.setStorageClass(StorageClass.ReducedRedundancy);

        Upload upload = this.tm.upload(request); // NB: asynchronous, returns immediately
        upload.waitForCompletion();
    } catch (AmazonClientException | InterruptedException e) {
        throw new IOException(e);
    }
}

From source file:fr.xebia.cocktail.CocktailManager.java

License:Apache License

/**
 * TODO use PUT instead of POST/*from   www.j  a  va 2  s .c om*/
 * 
 * @param id id of the cocktail
 * @param photo to associate with the cocktail
 * @return redirection to display cocktail
 */
@RequestMapping(value = "/cocktail/{id}/photo", method = RequestMethod.POST)
public String updatePhoto(@PathVariable String id, @RequestParam("photo") MultipartFile photo) {

    if (!photo.isEmpty()) {
        try {
            String contentType = fileStorageService.findContentType(photo.getOriginalFilename());
            if (contentType == null) {
                logger.warn("photo",
                        "Skip file with unsupported extension '" + photo.getOriginalFilename() + "'");
            } 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);

                Cocktail cocktail = cocktailRepository.get(id);
                logger.info("Saved {}", photoUrl);
                cocktail.setPhotoUrl(photoUrl);
                cocktailRepository.update(cocktail);
            }

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

From source file:fsi_admin.JAwsS3Conn.java

License:Open Source License

@SuppressWarnings("rawtypes")
private boolean subirArchivo(StringBuffer msj, AmazonS3 s3, String S3BUKT, String nombre, Vector archivos) {
    //System.out.println("AwsConn SubirArchivo:" + nombre + ":nombre");

    if (!archivos.isEmpty()) {
        FileItem actual = null;//from  w w  w. jav  a 2  s.  co m

        try {
            for (int i = 0; i < archivos.size(); i++) {
                InputStream inputStream = null;
                try {
                    actual = (FileItem) archivos.elementAt(i);
                    /////////////////////////////////////////////////////////
                    //Obtain the Content length of the Input stream for S3 header
                    InputStream is = actual.getInputStream();
                    byte[] contentBytes = IOUtils.toByteArray(is);

                    Long contentLength = Long.valueOf(contentBytes.length);

                    ObjectMetadata metadata = new ObjectMetadata();
                    metadata.setContentLength(contentLength);

                    //Reobtain the tmp uploaded file as input stream
                    inputStream = actual.getInputStream();

                    //Put the object in S3
                    //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-'));
                    //System.out.println("BUCKET: " + S3BUKT + " OBJETO: " + nombre.replace('_', '-'));
                    s3.putObject(new PutObjectRequest(S3BUKT, nombre, inputStream, metadata));
                } finally {
                    if (inputStream != null)
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                }
                ////////////////////////////////////////////////////////////
            }
            return true;
        } catch (AmazonServiceException ase) {
            ase.printStackTrace();
            msj.append("Error de AmazonServiceException al subir archivo a S3.<br>");
            msj.append("Mensaje: " + ase.getMessage() + "<br>");
            msj.append("Cdigo de Estatus HTTP: " + ase.getStatusCode() + "<br>");
            msj.append("Cdigo de Error AWS:   " + ase.getErrorCode() + "<br>");
            msj.append("Tipo de Error:       " + ase.getErrorType() + "<br>");
            msj.append("Request ID:       " + ase.getRequestId());
            return false;
        } catch (AmazonClientException ace) {
            ace.printStackTrace();
            msj.append("Error de AmazonClientException al subir archivo a S3.<br>");
            msj.append("Mensaje: " + ace.getMessage());
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            msj.append("Error de Entrada/Salida al subir archivo a S3: " + e.getMessage());
            return false;
        }

    } else {
        msj.append("Error al subir archivo a la nube: No se envi ningun archivo");
        return false;
    }
}

From source file:gateway.controller.util.GatewayUtil.java

License:Apache License

/**
 * Handles the uploaded file from the data/file endpoint. This will push the file to S3, and then modify the content
 * of the job to reference the new S3 location of the file.
 * // w  ww. j a va 2 s .c  o m
 * @param jobId
 *            The Id of the Job, used for generating a unique S3 bucket file name.
 * @param job
 *            The ingest job, containing the DataResource metadata
 * @param file
 *            The file to be uploaded
 * @return The modified job, with the location of the S3 file added to the metadata
 */
public IngestJob pushS3File(String jobId, IngestJob job, MultipartFile file)
        throws AmazonServiceException, AmazonClientException, IOException {
    // The content length must be specified.
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(file.getSize());
    // Send the file to S3. The key corresponds with the S3 file name.
    String fileKey = String.format("%s-%s", jobId, file.getOriginalFilename());
    s3Client.putObject(AMAZONS3_BUCKET_NAME, fileKey, file.getInputStream(), metadata);
    // Note the S3 file path in the Ingest Job.
    // Attach the file to the FileLocation object
    FileLocation fileLocation = new S3FileStore(AMAZONS3_BUCKET_NAME, fileKey, file.getSize(), AMAZONS3_DOMAIN);
    ((FileRepresentation) job.getData().getDataType()).setLocation(fileLocation);
    logger.log(String.format("S3 File for Job %s Persisted to %s:%s", jobId, AMAZONS3_BUCKET_NAME, fileKey),
            PiazzaLogger.INFO);
    return job;
}

From source file:generator.components.S3FileUtility.java

License:Apache License

/**
 * Upload file to s3 bucket//from   w  w  w .  j a  v a  2 s . co m
 * 
 * @param file
 *            the object
 */
public String writeFileToS3(File file, FileLocation fileLocation) throws FileNotFoundException {

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(file.length());
    String fileKey = String.format("%s-%s", uuidFactory.getUUID(), file.getName());

    // BasicAWSCredentials credentials = new BasicAWSCredentials(AMAZONS3_ACCESS_KEY, AMAZONS3_PRIVATE_KEY);
    s3Client = new AmazonS3Client();

    // Making the object public
    PutObjectRequest putObj = new PutObjectRequest(S3_OUTPUT_BUCKET, fileKey, file);
    putObj.setCannedAcl(CannedAccessControlList.PublicRead);
    s3Client.putObject(putObj);

    return fileKey;
}

From source file:gov.cdc.sdp.cbr.aphl.AphlS3Producer.java

License:Apache License

private ObjectMetadata determineMetadata(final Exchange exchange) {
    ObjectMetadata objectMetadata = new ObjectMetadata();

    Long contentLength = exchange.getIn().getHeader(S3Constants.CONTENT_LENGTH, Long.class);
    if (contentLength != null) {
        objectMetadata.setContentLength(contentLength);
    }// w  w  w . j a  va  2  s .  c o  m

    String contentType = exchange.getIn().getHeader(S3Constants.CONTENT_TYPE, String.class);
    if (contentType != null) {
        objectMetadata.setContentType(contentType);
    }

    String cacheControl = exchange.getIn().getHeader(S3Constants.CACHE_CONTROL, String.class);
    if (cacheControl != null) {
        objectMetadata.setCacheControl(cacheControl);
    }

    String contentDisposition = exchange.getIn().getHeader(S3Constants.CONTENT_DISPOSITION, String.class);
    if (contentDisposition != null) {
        objectMetadata.setContentDisposition(contentDisposition);
    }

    String contentEncoding = exchange.getIn().getHeader(S3Constants.CONTENT_ENCODING, String.class);
    if (contentEncoding != null) {
        objectMetadata.setContentEncoding(contentEncoding);
    }

    String contentMD5 = exchange.getIn().getHeader(S3Constants.CONTENT_MD5, String.class);
    if (contentMD5 != null) {
        objectMetadata.setContentMD5(contentMD5);
    }

    Date lastModified = exchange.getIn().getHeader(S3Constants.LAST_MODIFIED, Date.class);
    if (lastModified != null) {
        objectMetadata.setLastModified(lastModified);
    }

    Map<String, String> userMetadata = CastUtils
            .cast(exchange.getIn().getHeader(S3Constants.USER_METADATA, Map.class));
    if (userMetadata != null) {
        objectMetadata.setUserMetadata(userMetadata);
    }

    Map<String, String> s3Headers = CastUtils
            .cast(exchange.getIn().getHeader(S3Constants.S3_HEADERS, Map.class));
    if (s3Headers != null) {
        for (Map.Entry<String, String> entry : s3Headers.entrySet()) {
            objectMetadata.setHeader(entry.getKey(), entry.getValue());
        }
    }

    String encryption = exchange.getIn().getHeader(S3Constants.SERVER_SIDE_ENCRYPTION,
            getConfiguration().getServerSideEncryption(), String.class);
    if (encryption != null) {
        objectMetadata.setSSEAlgorithm(encryption);
    }

    return objectMetadata;
}

From source file:gov.usgs.cida.iplover.util.ImageStorage.java

public static String save(byte[] parsedImage, String uuid) throws IOException {

    AmazonS3 s3 = prepS3Client();//w  w  w  .  ja va 2 s  . c o m

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(parsedImage.length);

    LOG.trace("Setting up image key.");
    //Build key, split by date uploaded
    Date d = new Date();
    String fname = uuid + ".jpg";
    String fileId = simp.format(d) + "/" + fname;
    String fileKey = KEY_BASE + "/" + fileId;

    s3.putObject(BUCKET_NAME, fileKey, new ByteArrayInputStream(parsedImage), metadata);

    LOG.trace("Image uploaded.");

    //The date directory and filename are the "unique key"
    return (fileId);
}