Search in sources :

Example 26 with S3Object

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
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) InputStream(java.io.InputStream) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) File(java.io.File)

Example 27 with S3Object

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();
}
Also used : FileStore(org.molgenis.data.file.FileStore) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) AmazonBucketClientImpl(org.molgenis.amazon.bucket.client.AmazonBucketClientImpl) S3Object(com.amazonaws.services.s3.model.S3Object) BeforeTest(org.testng.annotations.BeforeTest)

Example 28 with S3Object

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);
}
Also used : InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 29 with S3Object

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();
}
Also used : S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 30 with S3Object

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Test(org.junit.jupiter.api.Test)

Aggregations

S3Object (com.amazonaws.services.s3.model.S3Object)76 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)22 InputStream (java.io.InputStream)22 IOException (java.io.IOException)21 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Test (org.junit.Test)15 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 AmazonS3 (com.amazonaws.services.s3.AmazonS3)11 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)11 AmazonServiceException (com.amazonaws.AmazonServiceException)10 Date (java.util.Date)9 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)8 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)8 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)7 AmazonClientException (com.amazonaws.AmazonClientException)6 FileOutputStream (java.io.FileOutputStream)6 Test (org.testng.annotations.Test)6