Search in sources :

Example 1 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project beneficiary-fhir-data by CMSgov.

the class DataSetTestUtilities method createPutRequest.

/**
 * @param bucket the {@link Bucket} to place the new object in
 * @param keyPrefix the S3 key prefix to store the new object under
 * @param manifest the {@link DataSetManifest} to push as an object
 * @return a {@link PutObjectRequest} for the specified {@link DataSetManifest}
 */
public static PutObjectRequest createPutRequest(Bucket bucket, String keyPrefix, DataSetManifest manifest) {
    String objectKey = String.format("%s/%d_%s", keyPrefix, manifest.getSequenceId(), "manifest.xml");
    try {
        // Serialize the manifest to a byte array.
        JAXBContext jaxbContext = JAXBContext.newInstance(DataSetManifest.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        ByteArrayOutputStream manifestOutputStream = new ByteArrayOutputStream();
        marshaller.marshal(manifest, manifestOutputStream);
        byte[] manifestByteArray = manifestOutputStream.toByteArray();
        InputStream manifestInputStream = new ByteArrayInputStream(manifestByteArray);
        // If this isn't specified, the AWS API logs annoying warnings.
        ObjectMetadata manifestMetadata = new ObjectMetadata();
        manifestMetadata.setContentLength(manifestByteArray.length);
        PutObjectRequest request = new PutObjectRequest(bucket.getName(), objectKey, manifestInputStream, manifestMetadata);
        return request;
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 2 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project beneficiary-fhir-data by CMSgov.

the class DataSetTestUtilities method createPutRequest.

/**
 * @param bucket the {@link Bucket} to place the new object in
 * @param keyPrefix the S3 key prefix to store the new object under
 * @param manifest the {@link DataSetManifest} to create an object for
 * @param manifestEntry the {@link DataSetManifestEntry} to create an object for
 * @param objectContentsUrl a {@link URL} to the data to push as the new object's content
 * @return a {@link PutObjectRequest} for the specified content
 */
public static PutObjectRequest createPutRequest(Bucket bucket, String keyPrefix, DataSetManifest manifest, DataSetManifestEntry manifestEntry, URL objectContentsUrl) {
    String objectKey = String.format("%s/%s", keyPrefix, manifestEntry.getName());
    try {
        // If this isn't specified, the AWS API logs annoying warnings.
        int objectContentLength = objectContentsUrl.openConnection().getContentLength();
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(objectContentLength);
        // create md5chksum on file to be uploaded
        objectMetadata.addUserMetadata("md5chksum", ManifestEntryDownloadTask.computeMD5ChkSum(objectContentsUrl.openStream()));
        PutObjectRequest request = new PutObjectRequest(bucket.getName(), objectKey, objectContentsUrl.openStream(), objectMetadata);
        /*
       * Per https://github.com/aws/aws-sdk-java/issues/427, this is
       * required when PUTing objects from an InputStream (as opposed to a
       * File). Without it, was seeing intermittent errors.
       */
        request.getRequestClientOptions().setReadLimit(objectContentLength + 1);
        return request;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new ChecksumException("NoSuchAlgorithmException on file " + manifest.getTimestampText() + manifestEntry.getName() + "trying to build md5chksum", e);
    }
}
Also used : ChecksumException(gov.cms.bfd.pipeline.ccw.rif.extract.exceptions.ChecksumException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 3 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project beneficiary-fhir-data by CMSgov.

the class SyntheticDataUploader method uploadSyntheticData.

/**
 * @param s3Client the {@link AmazonS3} client to use
 * @param s3KeyPrefix the S3 key prefix to upload all objects under/into
 * @param syntheticDataPathGrabber the {@link Function} that returns the {@link Path} to upload
 *     from, for a given {@link SyntheticDataFile}
 * @throws MalformedURLException Any {@link MalformedURLException}s encountered will be bubbled
 *     up.
 */
private static void uploadSyntheticData(AmazonS3 s3Client, String s3KeyPrefix, Function<SyntheticDataFile, Path> syntheticDataPathGrabber) throws MalformedURLException {
    Bucket bucket = new Bucket(TestDataSetLocation.S3_BUCKET_TEST_DATA);
    // Build a DataSetManifest for the data to be uploaded.
    List<DataSetManifestEntry> manifestEntries = new LinkedList<>();
    for (SyntheticDataFile syntheticDataFile : SyntheticDataFile.values()) manifestEntries.add(new DataSetManifestEntry(syntheticDataPathGrabber.apply(syntheticDataFile).getFileName().toString(), syntheticDataFile.getRifFile().getFileType()));
    DataSetManifest manifest = new DataSetManifest(Instant.now(), 0, manifestEntries);
    // Upload the manifest and every file in it.
    PutObjectRequest manifestRequest = DataSetTestUtilities.createPutRequest(bucket, s3KeyPrefix, manifest);
    manifestRequest.setCannedAcl(CannedAccessControlList.PublicRead);
    s3Client.putObject(manifestRequest);
    LOGGER.info("Uploaded: manifest");
    for (SyntheticDataFile syntheticDataFile : SyntheticDataFile.values()) {
        DataSetManifestEntry manifestEntry = manifest.getEntries().stream().filter(e -> e.getName().equals(syntheticDataPathGrabber.apply(syntheticDataFile).getFileName().toString())).findFirst().get();
        PutObjectRequest fileRequest = DataSetTestUtilities.createPutRequest(bucket, s3KeyPrefix, manifest, manifestEntry, syntheticDataPathGrabber.apply(syntheticDataFile).toUri().toURL());
        fileRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        s3Client.putObject(fileRequest);
        LOGGER.info("Uploaded: {}", syntheticDataFile.name());
    }
}
Also used : SyntheticDataFile(gov.cms.bfd.pipeline.ccw.rif.extract.synthetic.SyntheticDataFixer.SyntheticDataFile) DataSetManifest(gov.cms.bfd.pipeline.ccw.rif.extract.s3.DataSetManifest) Bucket(com.amazonaws.services.s3.model.Bucket) DataSetManifestEntry(gov.cms.bfd.pipeline.ccw.rif.extract.s3.DataSetManifest.DataSetManifestEntry) LinkedList(java.util.LinkedList) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 4 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project beneficiary-fhir-data by CMSgov.

the class SyntheticDataUploader2 method uploadSyntheticData.

/**
 * @param s3Client the {@link AmazonS3} client to use
 * @param s3KeyPrefix the S3 key prefix to upload all objects under/into
 * @param syntheticDataPathGrabber the {@link Function} that returns the {@link Path} to upload
 *     from, for a given {@link SyntheticDataFile}
 * @throws MalformedURLException Any {@link MalformedURLException}s encountered will be bubbled
 *     up.
 */
private static void uploadSyntheticData(AmazonS3 s3Client, String s3KeyPrefix, Function<SyntheticDataFile, Path> syntheticDataPathGrabber) throws MalformedURLException {
    Bucket bucket = new Bucket(TestDataSetLocation.S3_BUCKET_TEST_DATA);
    // Build a DataSetManifest for the data to be uploaded.
    List<DataSetManifestEntry> manifestEntries = new LinkedList<>();
    for (SyntheticDataFile syntheticDataFile : SyntheticDataFile.values()) manifestEntries.add(new DataSetManifestEntry(syntheticDataPathGrabber.apply(syntheticDataFile).getFileName().toString(), syntheticDataFile.getRifFile().getFileType()));
    DataSetManifest manifest = new DataSetManifest(Instant.now(), 0, manifestEntries);
    // Upload the manifest and every file in it.
    PutObjectRequest manifestRequest = DataSetTestUtilities.createPutRequest(bucket, s3KeyPrefix, manifest);
    manifestRequest.setCannedAcl(CannedAccessControlList.PublicRead);
    s3Client.putObject(manifestRequest);
    LOGGER.info("Uploaded: manifest");
    for (SyntheticDataFile syntheticDataFile : SyntheticDataFile.values()) {
        DataSetManifestEntry manifestEntry = manifest.getEntries().stream().filter(e -> e.getName().equals(syntheticDataPathGrabber.apply(syntheticDataFile).getFileName().toString())).findFirst().get();
        PutObjectRequest fileRequest = DataSetTestUtilities.createPutRequest(bucket, s3KeyPrefix, manifest, manifestEntry, syntheticDataPathGrabber.apply(syntheticDataFile).toUri().toURL());
        fileRequest.setCannedAcl(CannedAccessControlList.PublicRead);
        s3Client.putObject(fileRequest);
        LOGGER.info("Uploaded: {}", syntheticDataFile.name());
    }
}
Also used : SyntheticDataFile(gov.cms.bfd.pipeline.ccw.rif.extract.synthetic.SyntheticDataFixer2.SyntheticDataFile) DataSetManifest(gov.cms.bfd.pipeline.ccw.rif.extract.s3.DataSetManifest) Bucket(com.amazonaws.services.s3.model.Bucket) DataSetManifestEntry(gov.cms.bfd.pipeline.ccw.rif.extract.s3.DataSetManifest.DataSetManifestEntry) LinkedList(java.util.LinkedList) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 5 with PutObjectRequest

use of com.amazonaws.s3.model.PutObjectRequest in project AlgaFoods by Dark1922.

the class S3FotoStorageService method armazenar.

@Override
public void armazenar(NovaFoto novaFoto) {
    try {
        String caminhoArquivo = getCaminhoArquivo(novaFoto.getNomeArquivo());
        // meta dados de um objeto
        var objectMetadata = new ObjectMetadata();
        // mostrar foto invez de fazer dowload
        objectMetadata.setContentType(novaFoto.getContentType());
        // submeter para api da amazon que agente ta fazendo uma requisição para colocar um objeto
        var putObjectRequest = new PutObjectRequest(storageProperties.getS3().getBucket(), caminhoArquivo, novaFoto.getInputStream(), objectMetadata).withCannedAcl(CannedAccessControlList.PublicRead);
        amazonS3.putObject(putObjectRequest);
    } catch (Exception e) {
        throw new StorageException("Não foi possivel enviar Arquivo para Amazon S3", e);
    }
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)301 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)193 ByteArrayInputStream (java.io.ByteArrayInputStream)113 Test (org.junit.Test)113 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)78 File (java.io.File)68 IOException (java.io.IOException)65 InputStream (java.io.InputStream)55 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)42 AmazonClientException (com.amazonaws.AmazonClientException)40 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)39 Upload (com.amazonaws.services.s3.transfer.Upload)37 AmazonServiceException (com.amazonaws.AmazonServiceException)35 Test (org.junit.jupiter.api.Test)30 AmazonS3 (com.amazonaws.services.s3.AmazonS3)28 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)20 Date (java.util.Date)20 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)20 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)20 RequestBody (software.amazon.awssdk.core.sync.RequestBody)19