Search in sources :

Example 81 with S3Client

use of software.amazon.awssdk.crt.s3.S3Client in project aws-java-nio-spi-for-s3 by awslabs.

the class S3ClientStore method clientForRegion.

private S3Client clientForRegion(String regionString) {
    // It may be useful to further cache clients for regions although at some point clients for buckets may need to be
    // specialized beyond just region end points.
    Region region = regionString.equals("") ? Region.US_EAST_1 : Region.of(regionString);
    logger.debug("bucket region is: '{}'", region.id());
    return S3Client.builder().region(region).overrideConfiguration(conf -> conf.retryPolicy(builder -> builder.retryCondition(retryCondition).backoffStrategy(backoffStrategy))).build();
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) S3AsyncClient(software.amazon.awssdk.services.s3.S3AsyncClient) ApiCallAttemptTimeoutException(software.amazon.awssdk.core.exception.ApiCallAttemptTimeoutException) S3Client(software.amazon.awssdk.services.s3.S3Client) LoggerFactory(org.slf4j.LoggerFactory) RetryableException(software.amazon.awssdk.core.exception.RetryableException) IOException(java.io.IOException) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Collectors(java.util.stream.Collectors) ApiCallTimeoutException(software.amazon.awssdk.core.exception.ApiCallTimeoutException) EqualJitterBackoffStrategy(software.amazon.awssdk.core.retry.backoff.EqualJitterBackoffStrategy) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) Stream(java.util.stream.Stream) software.amazon.awssdk.core.retry.conditions(software.amazon.awssdk.core.retry.conditions) Duration(java.time.Duration) URI(java.net.URI) Region(software.amazon.awssdk.regions.Region) HttpStatusCode(software.amazon.awssdk.http.HttpStatusCode) Region(software.amazon.awssdk.regions.Region)

Example 82 with S3Client

use of software.amazon.awssdk.crt.s3.S3Client in project aws-codeguru-cli by aws.

the class ArtifactAdapterTest method test_zipAndUpload_happyCaseBuildDir.

@Test
public void test_zipAndUpload_happyCaseBuildDir() throws Exception {
    val tempDir = Files.createTempDirectory("test_zipAndUpload_happyCaseGitFilesOnly");
    val bucketName = "some-bucket";
    // only include files from the util dir.
    val repoDir = Paths.get("./test-data/fake-repo");
    val buildArtifacts = repoDir.resolve("build-dir/lib");
    val config = Configuration.builder().s3Client(s3client).interactiveMode(false).build();
    Answer<Object> answer = invocationOnMock -> {
        Path filePath = invocationOnMock.getArgument(1);
        if (!filePath.toString().contains("analysis-bin")) {
            // only look at the artifacts.
            return null;
        }
        Assertions.assertTrue(filePath.toFile().isFile());
        try (val zipFile = new ZipFile(filePath.toFile())) {
            val entries = zipFile.entries();
            int count = 0;
            while (entries.hasMoreElements()) {
                val s = entries.nextElement().getName();
                Assertions.assertTrue(s.endsWith("included.txt"));
                // count the files that are not in the git folder.
                count++;
            }
            Assertions.assertEquals(1, count);
        }
        return null;
    };
    doAnswer(answer).when(s3client).putObject(any(PutObjectRequest.class), any(Path.class));
    val metaData = ArtifactAdapter.zipAndUpload(config, tempDir, repoDir, Arrays.asList(repoDir), Arrays.asList(buildArtifacts), bucketName);
    Assertions.assertNotNull(metaData.getBuildKey());
    Assertions.assertNotNull(metaData.getSourceKey());
}
Also used : lombok.val(lombok.val) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockTextTerminal(org.beryx.textio.mock.MockTextTerminal) Arrays(java.util.Arrays) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Files(java.nio.file.Files) TextIO(org.beryx.textio.TextIO) Mock(org.mockito.Mock) S3Client(software.amazon.awssdk.services.s3.S3Client) Configuration(com.amazonaws.gurureviewercli.model.Configuration) lombok.val(lombok.val) Test(org.junit.jupiter.api.Test) Answer(org.mockito.stubbing.Answer) List(java.util.List) Assumptions(org.junit.jupiter.api.Assumptions) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Paths(java.nio.file.Paths) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Assertions(org.junit.jupiter.api.Assertions) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) Collections(java.util.Collections) Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) Test(org.junit.jupiter.api.Test)

Example 83 with S3Client

use of software.amazon.awssdk.crt.s3.S3Client in project aws-codeguru-cli by aws.

the class ArtifactAdapterTest method test_zipAndUpload_happyCaseGitFilesOnly.

@Test
public void test_zipAndUpload_happyCaseGitFilesOnly() throws Exception {
    val repoDir = Paths.get("./test-data/two-commits").toRealPath();
    val tempDir = Files.createTempDirectory("test_zipAndUpload_happyCaseGitFilesOnly");
    val bucketName = "some-bucket";
    // only include files from the util dir.
    final List<Path> buildDirs = Collections.emptyList();
    val mockTerminal = new MockTextTerminal();
    // answer No to the question if only files under version control should be scanned.
    mockTerminal.getInputs().add("y");
    val config = Configuration.builder().s3Client(s3client).interactiveMode(true).textIO(new TextIO(mockTerminal)).versionedFiles(Arrays.asList(repoDir.resolve("test.txt"))).build();
    Answer<Object> answer = invocationOnMock -> {
        Path filePath = invocationOnMock.getArgument(1);
        Assertions.assertTrue(filePath.toFile().isFile());
        try (val zipFile = new ZipFile(filePath.toFile())) {
            val entries = zipFile.entries();
            int count = 0;
            while (entries.hasMoreElements()) {
                val s = entries.nextElement().getName();
                val original = repoDir.resolve(s).toFile();
                Assertions.assertTrue(original.isFile(), "Not a valid file: " + original);
                Assertions.assertFalse(s.startsWith(".."));
                if (!s.startsWith("git/")) {
                    // count the files that are not in the git folder.
                    count++;
                }
            }
            Assertions.assertEquals(1, count, "Unexpected number of files in zip.");
        }
        return null;
    };
    doAnswer(answer).when(s3client).putObject(any(PutObjectRequest.class), any(Path.class));
    val metaData = ArtifactAdapter.zipAndUpload(config, tempDir, repoDir, Arrays.asList(repoDir), buildDirs, bucketName);
    Assertions.assertNull(metaData.getBuildKey());
    Assertions.assertNotNull(metaData.getSourceKey());
}
Also used : lombok.val(lombok.val) Path(java.nio.file.Path) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockTextTerminal(org.beryx.textio.mock.MockTextTerminal) Arrays(java.util.Arrays) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Files(java.nio.file.Files) TextIO(org.beryx.textio.TextIO) Mock(org.mockito.Mock) S3Client(software.amazon.awssdk.services.s3.S3Client) Configuration(com.amazonaws.gurureviewercli.model.Configuration) lombok.val(lombok.val) Test(org.junit.jupiter.api.Test) Answer(org.mockito.stubbing.Answer) List(java.util.List) Assumptions(org.junit.jupiter.api.Assumptions) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Paths(java.nio.file.Paths) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Assertions(org.junit.jupiter.api.Assertions) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) Collections(java.util.Collections) MockTextTerminal(org.beryx.textio.mock.MockTextTerminal) ZipFile(java.util.zip.ZipFile) TextIO(org.beryx.textio.TextIO) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) Test(org.junit.jupiter.api.Test)

Example 84 with S3Client

use of software.amazon.awssdk.crt.s3.S3Client in project aws-codeguru-cli by aws.

the class ArtifactAdapterTest method test_zipAndUpload_happyCaseSourceOnly.

@Test
public void test_zipAndUpload_happyCaseSourceOnly() throws Exception {
    val repoDir = Paths.get("./");
    // skip the test if the test container stripped to the top level .git folder
    Assumptions.assumeTrue(repoDir.resolve(".git").toFile().isDirectory());
    val tempDir = Files.createTempDirectory("test_zipAndUpload_happyCase");
    val bucketName = "some-bucket";
    val sourceDirs = Arrays.asList(Paths.get("src"));
    final List<Path> buildDirs = Collections.emptyList();
    val config = Configuration.builder().s3Client(s3client).build();
    Answer<Object> answer = invocationOnMock -> {
        Path filePath = invocationOnMock.getArgument(1);
        Assertions.assertTrue(filePath.toFile().isFile());
        try (val zipFile = new ZipFile(filePath.toFile())) {
            val entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                val s = entries.nextElement().getName();
                val original = repoDir.resolve(s).toFile();
                Assertions.assertTrue(original.isFile(), "Not a valid file: " + original);
                Assertions.assertFalse(s.startsWith(".."));
            }
        }
        return null;
    };
    doAnswer(answer).when(s3client).putObject(any(PutObjectRequest.class), any(Path.class));
    val metaData = ArtifactAdapter.zipAndUpload(config, tempDir, repoDir, sourceDirs, buildDirs, bucketName);
    Assertions.assertNull(metaData.getBuildKey());
    Assertions.assertNotNull(metaData.getSourceKey());
}
Also used : lombok.val(lombok.val) Path(java.nio.file.Path) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockTextTerminal(org.beryx.textio.mock.MockTextTerminal) Arrays(java.util.Arrays) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Files(java.nio.file.Files) TextIO(org.beryx.textio.TextIO) Mock(org.mockito.Mock) S3Client(software.amazon.awssdk.services.s3.S3Client) Configuration(com.amazonaws.gurureviewercli.model.Configuration) lombok.val(lombok.val) Test(org.junit.jupiter.api.Test) Answer(org.mockito.stubbing.Answer) List(java.util.List) Assumptions(org.junit.jupiter.api.Assumptions) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Paths(java.nio.file.Paths) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Assertions(org.junit.jupiter.api.Assertions) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) Collections(java.util.Collections) ZipFile(java.util.zip.ZipFile) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) Test(org.junit.jupiter.api.Test)

Example 85 with S3Client

use of software.amazon.awssdk.crt.s3.S3Client in project beam by apache.

the class S3WritableByteChannelTest method writeFromOptions.

private void writeFromOptions(S3Options options, boolean writeReadOnlyBuffer) throws IOException {
    S3Client mockS3Client = mock(S3Client.class, withSettings().defaultAnswer(RETURNS_SMART_NULLS));
    S3ResourceId path = S3ResourceId.fromUri("s3://bucket/dir/file");
    Supplier channel = () -> new S3WritableByteChannel(mockS3Client, path, "text/plain", S3FileSystemConfiguration.fromS3Options(options));
    write(mockS3Client, channel, path, options.getSSEAlgorithm(), toMd5(options.getSSECustomerKey()), options.getSSEKMSKeyId(), options.getS3UploadBufferSizeBytes(), options.getBucketKeyEnabled(), writeReadOnlyBuffer);
}
Also used : S3Client(software.amazon.awssdk.services.s3.S3Client)

Aggregations

S3Client (software.amazon.awssdk.services.s3.S3Client)266 Test (org.junit.jupiter.api.Test)87 Region (software.amazon.awssdk.regions.Region)80 Test (org.junit.Test)54 URI (java.net.URI)36 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)28 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)28 ArrayList (java.util.ArrayList)26 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)24 List (java.util.List)23 Collectors (java.util.stream.Collectors)22 IOException (java.io.IOException)20 InputStream (java.io.InputStream)20 S3ClientBuilder (software.amazon.awssdk.services.s3.S3ClientBuilder)20 RequestBody (software.amazon.awssdk.core.sync.RequestBody)19 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)19 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)19 Map (java.util.Map)17 S3AsyncClient (software.amazon.awssdk.services.s3.S3AsyncClient)17 S3Object (software.amazon.awssdk.services.s3.model.S3Object)17