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);
}
}
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);
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations