use of software.amazon.awssdk.services.s3.model.S3Object in project opentest by mcdcorp.
the class GetS3Object method run.
@Override
public void run() {
super.run();
String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
String bucket = this.readStringArgument("bucket");
String objectKey = this.readStringArgument("objectKey");
String targetFilePath = this.readStringArgument("targetFile");
Boolean overwrite = this.readBooleanArgument("overwrite", false);
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile));
S3Object object = s3Client.getObject(new GetObjectRequest(bucket, objectKey));
InputStream objectDataStream = object.getObjectContent();
if (targetFilePath != null) {
File targetFile = new File(targetFilePath);
if (!targetFile.isAbsolute()) {
targetFile = Paths.get(this.getActor().getTempDir().getAbsolutePath(), targetFilePath).toFile();
}
targetFile.getParentFile().mkdirs();
try {
if (overwrite) {
Files.copy(objectDataStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} else {
Files.copy(objectDataStream, targetFile.toPath());
}
} catch (Exception ex) {
throw new RuntimeException(String.format("Failed to transfer data from the input stream into file %s", targetFilePath), ex);
}
this.writeArgument("targetFile", targetFile.getAbsolutePath());
} else {
// TODO: Make targetFile arg optional so this branch can execute.
// Read data in memory and write it to an output value
}
}
use of software.amazon.awssdk.services.s3.model.S3Object in project molgenis by molgenis.
the class AmazonBucketClientImplTest method setUp.
@BeforeTest
public void setUp() {
client = mock(AmazonS3Client.class);
fileStore = mock(FileStore.class);
s3Object = mock(S3Object.class);
httpRequestBase = mock(HttpRequestBase.class);
amazonBucketClient = new AmazonBucketClientImpl();
}
use of software.amazon.awssdk.services.s3.model.S3Object in project molgenis by molgenis.
the class AmazonBucketClientImpl method downloadFile.
@Override
public File downloadFile(AmazonS3 s3Client, FileStore fileStore, String jobIdentifier, String bucketName, String keyName, String extension, boolean isExpression, String targetEntityType) throws IOException {
String key;
// This is indicated by the "isExpression" boolean
if (isExpression) {
key = this.getMostRecentMatchingKey(s3Client, bucketName, keyName);
} else {
key = keyName;
}
S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, key));
InputStream in = s3Object.getObjectContent();
return storeFile(fileStore, key, extension, targetEntityType, jobIdentifier, in);
}
use of software.amazon.awssdk.services.s3.model.S3Object in project qpp-conversion-tool by CMSgov.
the class StorageServiceImpl method getFileByLocationId.
/**
* Performs a {@link GetObjectRequest} to the S3 bucket by file id for the file
*
* @param fileLocationId Id of the file to search for
* @return file found from S3
*/
@Override
public InputStream getFileByLocationId(String fileLocationId) {
final String bucketName = environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE);
if (Strings.isNullOrEmpty(bucketName)) {
API_LOG.warn("No bucket name is specified.");
return null;
}
API_LOG.info("Retrieving file {} from bucket {}", fileLocationId, bucketName);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileLocationId);
S3Object s3Object = amazonS3.getObject(getObjectRequest);
API_LOG.info("Successfully retrieved file {} from S3 bucket {}", getObjectRequest.getKey(), getObjectRequest.getBucketName());
return s3Object.getObjectContent();
}
use of software.amazon.awssdk.services.s3.model.S3Object in project qpp-conversion-tool by CMSgov.
the class StorageServiceImplTest method envVariablesPresent.
@Test
void envVariablesPresent() {
S3Object s3ObjectMock = mock(S3Object.class);
s3ObjectMock.setObjectContent(new ByteArrayInputStream("1234".getBytes()));
Mockito.when(amazonS3Client.getObject(any(GetObjectRequest.class))).thenReturn(s3ObjectMock);
Mockito.when(environment.getProperty(Constants.BUCKET_NAME_ENV_VARIABLE)).thenReturn("meep");
underTest.getFileByLocationId("meep");
verify(s3ObjectMock, times(1)).getObjectContent();
}
Aggregations