Search in sources :

Example 11 with Bucket

use of software.amazon.awssdk.services.s3.model.Bucket in project beam by apache.

the class S3ReadableSeekableByteChannel method read.

@Override
public int read(ByteBuffer destinationBuffer) throws IOException {
    if (!isOpen()) {
        throw new ClosedChannelException();
    }
    if (!destinationBuffer.hasRemaining()) {
        return 0;
    }
    if (position == contentLength) {
        return -1;
    }
    if (s3ResponseInputStream == null) {
        GetObjectRequest.Builder builder = GetObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).sseCustomerKey(config.getSSECustomerKey().getKey()).sseCustomerAlgorithm(config.getSSECustomerKey().getAlgorithm());
        if (position > 0) {
            builder.range(String.format("bytes=%s-%s", position, contentLength));
        }
        GetObjectRequest request = builder.build();
        try {
            s3ResponseInputStream = s3Client.getObject(request);
        } catch (SdkClientException e) {
            throw new IOException(e);
        }
        s3ObjectContentChannel = Channels.newChannel(new BufferedInputStream(s3ResponseInputStream, 1024 * 1024));
    }
    int totalBytesRead = 0;
    int bytesRead = 0;
    do {
        totalBytesRead += bytesRead;
        try {
            bytesRead = s3ObjectContentChannel.read(destinationBuffer);
        } catch (SdkServiceException e) {
            throw new IOException(e);
        }
    } while (bytesRead > 0);
    position += totalBytesRead;
    return totalBytesRead;
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) SdkServiceException(software.amazon.awssdk.core.exception.SdkServiceException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 12 with Bucket

use of software.amazon.awssdk.services.s3.model.Bucket in project beam by apache.

the class S3WritableByteChannel method flush.

private void flush() throws IOException {
    uploadBuffer.flip();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(uploadBuffer.array(), 0, uploadBuffer.limit());
    UploadPartRequest request = UploadPartRequest.builder().bucket(path.getBucket()).key(path.getKey()).uploadId(uploadId).partNumber(partNumber++).contentLength((long) uploadBuffer.limit()).sseCustomerKey(config.getSSECustomerKey().getKey()).sseCustomerAlgorithm(config.getSSECustomerKey().getAlgorithm()).sseCustomerKeyMD5(config.getSSECustomerKey().getMD5()).contentMD5(Base64.getEncoder().encodeToString(md5.digest())).build();
    UploadPartResponse response;
    try {
        response = s3Client.uploadPart(request, RequestBody.fromInputStream(inputStream, request.contentLength()));
    } catch (SdkClientException e) {
        throw new IOException(e);
    }
    CompletedPart part = CompletedPart.builder().partNumber(request.partNumber()).eTag(response.eTag()).build();
    uploadBuffer.clear();
    md5.reset();
    completedParts.add(part);
}
Also used : SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ByteArrayInputStream(java.io.ByteArrayInputStream) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) IOException(java.io.IOException) UploadPartResponse(software.amazon.awssdk.services.s3.model.UploadPartResponse)

Example 13 with Bucket

use of software.amazon.awssdk.services.s3.model.Bucket in project beam by apache.

the class S3FileSystemTest method matchNonGlobNotReadSeekEfficient.

@Test
public void matchNonGlobNotReadSeekEfficient() {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Config("s3"));
    S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/filethatexists");
    long lastModifiedMillis = 1540000000000L;
    HeadObjectResponse headObjectResponse = HeadObjectResponse.builder().contentLength(100L).lastModified(Instant.ofEpochMilli(lastModifiedMillis)).contentEncoding("gzip").build();
    when(s3FileSystem.getS3Client().headObject(argThat(new GetHeadObjectRequestMatcher(HeadObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).build())))).thenReturn(headObjectResponse);
    MatchResult result = s3FileSystem.matchNonGlobPath(path);
    assertThat(result, MatchResultMatcher.create(ImmutableList.of(MatchResult.Metadata.builder().setSizeBytes(100).setLastModifiedMillis(lastModifiedMillis).setResourceId(path).setIsReadSeekEfficient(false).build())));
}
Also used : HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Example 14 with Bucket

use of software.amazon.awssdk.services.s3.model.Bucket in project beam by apache.

the class S3FileSystemTest method testAtomicCopy.

private void testAtomicCopy(S3FileSystem s3FileSystem, SSECustomerKey sseCustomerKey) throws IOException {
    S3ResourceId sourcePath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/from");
    S3ResourceId destinationPath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/to");
    CopyObjectResponse.Builder builder = CopyObjectResponse.builder();
    String sseCustomerKeyMd5 = toMd5(sseCustomerKey);
    if (sseCustomerKeyMd5 != null) {
        builder.sseCustomerKeyMD5(sseCustomerKeyMd5);
    }
    CopyObjectResponse copyObjectResponse = builder.build();
    CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder().copySource(sourcePath.getBucket() + "/" + sourcePath.getKey()).destinationBucket(destinationPath.getBucket()).destinationBucket(destinationPath.getKey()).sseCustomerKey(sseCustomerKey.getKey()).copySourceSSECustomerAlgorithm(sseCustomerKey.getAlgorithm()).build();
    when(s3FileSystem.getS3Client().copyObject(any(CopyObjectRequest.class))).thenReturn(copyObjectResponse);
    assertEquals(sseCustomerKeyMd5, s3FileSystem.getS3Client().copyObject(copyObjectRequest).sseCustomerKeyMD5());
    HeadObjectResponse headObjectResponse = HeadObjectResponse.builder().build();
    s3FileSystem.atomicCopy(sourcePath, destinationPath, headObjectResponse);
    verify(s3FileSystem.getS3Client(), times(2)).copyObject(any(CopyObjectRequest.class));
}
Also used : CopyObjectRequest(software.amazon.awssdk.services.s3.model.CopyObjectRequest) HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) CopyObjectResponse(software.amazon.awssdk.services.s3.model.CopyObjectResponse)

Example 15 with Bucket

use of software.amazon.awssdk.services.s3.model.Bucket in project beam by apache.

the class S3FileSystemTest method matchNonGlobNullContentEncodingWithOptions.

@Test
public void matchNonGlobNullContentEncodingWithOptions() {
    S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());
    S3ResourceId path = S3ResourceId.fromUri("s3://testbucket/testdirectory/filethatexists");
    long lastModifiedMillis = 1540000000000L;
    HeadObjectResponse headObjectResponse = HeadObjectResponse.builder().contentLength(100L).lastModified(Instant.ofEpochMilli(lastModifiedMillis)).contentEncoding(null).build();
    when(s3FileSystem.getS3Client().headObject(argThat(new GetHeadObjectRequestMatcher(HeadObjectRequest.builder().bucket(path.getBucket()).key(path.getKey()).build())))).thenReturn(headObjectResponse);
    MatchResult result = s3FileSystem.matchNonGlobPath(path);
    assertThat(result, MatchResultMatcher.create(ImmutableList.of(MatchResult.Metadata.builder().setSizeBytes(100).setLastModifiedMillis(lastModifiedMillis).setResourceId(path).setIsReadSeekEfficient(true).build())));
}
Also used : HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) MatchResult(org.apache.beam.sdk.io.fs.MatchResult) S3TestUtils.buildMockedS3FileSystem(org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem) Test(org.junit.Test)

Aggregations

S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)60 S3Client (software.amazon.awssdk.services.s3.S3Client)53 Region (software.amazon.awssdk.regions.Region)47 Bucket (com.amazonaws.services.s3.model.Bucket)32 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 IOException (java.io.IOException)18 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)18 S3Object (software.amazon.awssdk.services.s3.model.S3Object)17 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)14 HeadObjectResponse (software.amazon.awssdk.services.s3.model.HeadObjectResponse)14 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)14 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)10 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)10 CompleteMultipartUploadRequest (software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest)9 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem)8 MatchResult (org.apache.beam.sdk.io.fs.MatchResult)8 Date (java.util.Date)7 CompletedPart (software.amazon.awssdk.services.s3.model.CompletedPart)7