Search in sources :

Example 1 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project airavata-mft by apache.

the class S3IncomingConnector method downloadChunk.

@Override
public void downloadChunk(int chunkId, long startByte, long endByte, String downloadFile) throws Exception {
    GetObjectRequest rangeObjectRequest = new GetObjectRequest(resource.getS3Storage().getBucketName(), resource.getFile().getResourcePath());
    rangeObjectRequest.setRange(startByte, endByte - 1);
    ObjectMetadata objectMetadata = s3Client.getObject(rangeObjectRequest, new File(downloadFile));
    logger.info("Downloaded S3 chunk to path {} for resource id {}", downloadFile, resource.getResourceId());
}
Also used : GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) File(java.io.File)

Example 2 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project amazon-athena-serverless-ehms-connector by aws-samples.

the class S3Helper method getResponseFromS3.

public String getResponseFromS3(String s3Path) throws IOException {
    if (s3Path == null) {
        throw new IOException("S3 path is null");
    }
    AmazonS3URI s3URI = new AmazonS3URI(s3Path);
    String bucket = s3URI.getBucket();
    String key = s3URI.getKey();
    BufferedReader br = new BufferedReader(new InputStreamReader(s3Client.getObject(new GetObjectRequest(bucket, key)).getObjectContent()));
    String line;
    StringBuilder sb = new StringBuilder();
    // read the files until it reaches the end of the file
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    return sb.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI)

Example 3 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project zuliasearch by zuliaio.

the class S3DocumentStorage method getAssociatedDocumentStream.

@Override
public InputStream getAssociatedDocumentStream(String uniqueId, String filename) throws Exception {
    FindIterable<Document> found = client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + FILE_UNIQUE_ID_KEY, String.join("-", uniqueId, filename)));
    Document doc = found.first();
    if (null != doc) {
        Document s3Info = doc.get("s3", Document.class);
        GetObjectRequest gor = GetObjectRequest.builder().bucket(s3Info.getString("bucket")).key(s3Info.getString("key")).build();
        ResponseInputStream<GetObjectResponse> results = s3.getObject(gor);
        return new BufferedInputStream(results);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) BufferedInputStream(java.io.BufferedInputStream) Document(org.bson.Document) AssociatedDocument(io.zulia.message.ZuliaBase.AssociatedDocument) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 4 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project zuliasearch by zuliaio.

the class S3DocumentStorage method parseTOC.

private AssociatedDocument parseTOC(Document doc) throws IOException {
    AssociatedDocument.Builder aBuilder = AssociatedDocument.newBuilder();
    aBuilder.setFilename(doc.getString(FILENAME));
    Document meta = doc.get("metadata", Document.class);
    aBuilder.setDocumentUniqueId(meta.getString(DOCUMENT_UNIQUE_ID_KEY));
    aBuilder.setTimestamp(meta.getLong(TIMESTAMP));
    aBuilder.setIndexName(indexName);
    meta.remove(TIMESTAMP);
    meta.remove(DOCUMENT_UNIQUE_ID_KEY);
    meta.remove(FILE_UNIQUE_ID_KEY);
    aBuilder.setMetadata(ZuliaUtil.mongoDocumentToByteString(meta));
    Document s3Info = doc.get("s3", Document.class);
    GetObjectRequest gor = GetObjectRequest.builder().bucket(s3Info.getString("bucket")).key(s3Info.getString("key")).build();
    ResponseInputStream<GetObjectResponse> results = s3.getObject(gor);
    aBuilder.setDocument(ByteString.readFrom(results));
    return aBuilder.build();
}
Also used : AssociatedDocument(io.zulia.message.ZuliaBase.AssociatedDocument) GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) Document(org.bson.Document) AssociatedDocument(io.zulia.message.ZuliaBase.AssociatedDocument) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Example 5 with GetObjectRequest

use of com.amazonaws.s3.model.GetObjectRequest in project proxima-platform by O2-Czech-Republic.

the class S3BlobPath method read.

@Override
public ReadableByteChannel read() {
    final long contentLength = getBlob().getSize();
    return new SeekableByteChannel() {

        private boolean open = true;

        private S3Object object;

        private ReadableByteChannel contentChannel;

        private long position = 0;

        @Override
        public int read(ByteBuffer dst) throws IOException {
            ensureOpen();
            if (position >= contentLength) {
                // End of file.
                return -1;
            }
            if (object == null) {
                final S3FileSystem fs = (S3FileSystem) getFileSystem();
                final GetObjectRequest request = new GetObjectRequest(fs.getBucket(), getBlobName());
                @Nullable final SSECustomerKey sseCustomerKey = fs.getSseCustomerKey();
                if (sseCustomerKey != null) {
                    request.setSSECustomerKey(sseCustomerKey);
                }
                if (position > 0) {
                    request.setRange(position, contentLength);
                }
                object = fs.client().getObject(request);
                contentChannel = Channels.newChannel(new BufferedInputStream(object.getObjectContent(), 1024 * 1024));
            }
            int totalBytesRead = 0;
            int bytesRead = 0;
            do {
                totalBytesRead += bytesRead;
                try {
                    bytesRead = contentChannel.read(dst);
                } catch (AmazonClientException e) {
                    throw new IOException(e);
                }
            } while (bytesRead > 0);
            position += totalBytesRead;
            return totalBytesRead;
        }

        @Override
        public long position() throws IOException {
            ensureOpen();
            return position;
        }

        @Override
        public SeekableByteChannel position(long newPosition) throws IOException {
            ensureOpen();
            Preconditions.checkArgument(newPosition >= 0, "New position is too low.");
            Preconditions.checkArgument(newPosition < contentLength, "New position is too high.");
            if (newPosition == position) {
                return this;
            }
            // next call to read()
            if (object != null) {
                object.close();
                object = null;
            }
            position = newPosition;
            return this;
        }

        @Override
        public long size() throws IOException {
            ensureOpen();
            return contentLength;
        }

        @Override
        public boolean isOpen() {
            return open;
        }

        @Override
        public void close() throws IOException {
            if (object != null) {
                object.close();
                object = null;
            }
            open = false;
        }

        @Override
        public int write(ByteBuffer src) {
            throw new NonWritableChannelException();
        }

        @Override
        public SeekableByteChannel truncate(long size) {
            throw new NonWritableChannelException();
        }

        private void ensureOpen() throws IOException {
            if (!open) {
                throw new ClosedChannelException();
            }
        }
    };
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SeekableByteChannel(java.nio.channels.SeekableByteChannel) SSECustomerKey(com.amazonaws.services.s3.model.SSECustomerKey) BufferedInputStream(java.io.BufferedInputStream) S3Object(com.amazonaws.services.s3.model.S3Object) NonWritableChannelException(java.nio.channels.NonWritableChannelException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) Nullable(javax.annotation.Nullable)

Aggregations

GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)139 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)123 S3Object (com.amazonaws.services.s3.model.S3Object)80 Test (org.junit.Test)77 IOException (java.io.IOException)63 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)56 File (java.io.File)40 Instant (java.time.Instant)33 InputStream (java.io.InputStream)32 PresignedGetObjectRequest (software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest)31 URI (java.net.URI)30 Region (software.amazon.awssdk.regions.Region)30 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)29 Duration (java.time.Duration)26 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)25 S3Presigner (software.amazon.awssdk.services.s3.presigner.S3Presigner)25 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)23 Mockito (org.mockito.Mockito)23 RunWith (org.junit.runner.RunWith)22 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)22