@Override protected InputStream open(long offset) { return getClient().getObject(new GetObjectRequest(_bk[0], _bk[1]).withRange(offset, _to)).getObjectContent(); }
.run("getS3Object", () -> { try { GetObjectRequest request = new GetObjectRequest(host, keyFromPath(path)).withRange(start); return s3.getObject(request).getObjectContent();
@Override public InputStream openStream() throws IOException { try { final long start; final long end = objectMetadata.getContentLength() - 1; if (offset > 0 && offset < objectMetadata.getContentLength()) { start = offset; } else if (offset < 0 && (-1 * offset) < objectMetadata.getContentLength()) { start = objectMetadata.getContentLength() + offset; } else { start = 0; } final GetObjectRequest request = new GetObjectRequest(config.getS3Bucket(), taskKey) .withMatchingETagConstraint(objectMetadata.getETag()) .withRange(start, end); return service.getObject(request).getObjectContent(); } catch (AmazonServiceException e) { throw new IOException(e); } } }
/** * Non-authenticated encryption schemes can do range GETs without an issue. */ public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException { SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey(); AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder .standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfiguration(CryptoMode.EncryptionOnly)) .withEncryptionMaterials(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(secretKey))) .build(); s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents"); System.out.println(s3Encryption.getObject(new GetObjectRequest(BUCKET_NAME, ENCRYPTED_KEY) .withRange(0, 2))); }
/** * Returns an object representing the details and data of an item in S3. * * @param bucket * the bucket containing the object. * @param objectKey * the key identifying the object. * @param byteRangeStart * include only a portion of the object's data - starting at this point * @param byteRangeEnd * include only a portion of the object's data - ending at this point * @return the object with the given key in S3, including details and data * @throws SdkClientException */ public S3Object getS3Object( Bucket bucket, String objectKey, Long byteRangeStart, Long byteRangeEnd ) throws SdkClientException { if ( byteRangeStart != null && byteRangeEnd != null ) { GetObjectRequest rangeObjectRequest = new GetObjectRequest( bucket.getName(), objectKey ).withRange( byteRangeStart, byteRangeEnd ); return s3Client.getObject( rangeObjectRequest ); } else { return s3Client.getObject( bucket.getName(), objectKey ); } }
/** * Strict authenticated encryption mode does not support ranged GETs. This is because we must use AES/CTR for ranged * GETs which is not an authenticated encryption algorithm. To do a partial get using authenticated encryption you have to * get the whole object and filter to the data you want. */ public void strictAuthenticatedEncryption_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmException { SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey(); AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder .standard() .withRegion(Regions.US_WEST_2) .withCryptoConfiguration(new CryptoConfiguration(CryptoMode.StrictAuthenticatedEncryption)) .withEncryptionMaterials(new StaticEncryptionMaterialsProvider(new EncryptionMaterials(secretKey))) .build(); s3Encryption.putObject(BUCKET_NAME, ENCRYPTED_KEY, "some contents"); try { s3Encryption.getObject(new GetObjectRequest(BUCKET_NAME, ENCRYPTED_KEY).withRange(0, 2)); } catch (SecurityException e) { System.err.println("Range GET is not supported with authenticated encryption"); } }
getObjectRequest.withRange(startingByte, lastByte);
public Path call() throws Exception { final Path chunkPath = (chunk == 0) ? downloadTo : Paths.get(downloadTo + "_" + chunk + "_" + retryNum); chunkPath.toFile().deleteOnExit(); final long startTime = System.currentTimeMillis(); final long byteRangeStart = chunk * chunkSize; final long byteRangeEnd = Math.min((chunk + 1) * chunkSize - 1, length); log.info("Downloading {} - chunk {} (retry {}) ({}-{}) to {}", s3Artifact.getFilename(), chunk, retryNum, byteRangeStart, byteRangeEnd, chunkPath); GetObjectRequest getObjectRequest = new GetObjectRequest(s3Artifact.getS3Bucket(), s3Artifact.getS3ObjectKey()) .withRange(byteRangeStart, byteRangeEnd); S3Object fetchedObject = s3.getObject(getObjectRequest); try (InputStream is = fetchedObject.getObjectContent()) { Files.copy(is, chunkPath, StandardCopyOption.REPLACE_EXISTING); } log.info("Finished downloading chunk {} (retry {}) of {} ({} bytes) in {}", chunk, retryNum, s3Artifact.getFilename(), byteRangeEnd - byteRangeStart, JavaUtils.duration(startTime)); return chunkPath; }; };
private S3Object initStream( long offset, String bucket, String key, int blockSize, AmazonS3 s3Client) { try { S3Object object = s3Client.getObject( (new GetObjectRequest(bucket, key)) .withRange(offset, offset + blockSize)); return object; } catch (Exception e) { LOGGER.warning(e.getMessage()); } return null; }
@Override public InputStream getContent(final int from, final int to) { return client.getObject(new GetObjectRequest(bucketName, key).withRange(from, to)).getObjectContent(); } }
private String readFirstLineFromS3File(String bucket, String path) { S3Object s3Object; try { // In the unlikely case that someone replaced the Stash file with a malicious file intended to cause // a memory overrun restrict the file contents fetched to a reasonably high limit. s3Object = _s3.getObject(new GetObjectRequest(bucket, path).withRange(0, 2048)); } catch (AmazonS3Exception e) { if (e.getStatusCode() == 404) { throw new StashNotAvailableException(); } throw e; } try (BufferedReader in = new BufferedReader(new InputStreamReader(s3Object.getObjectContent(), Charsets.UTF_8))) { return in.readLine(); } catch (IOException e) { throw Throwables.propagate(e); } }
private String readFirstLineFromS3File(String bucket, String path) { S3Object s3Object; try { // In the unlikely case that someone replaced the Stash file with a malicious file intended to cause // a memory overrun restrict the file contents fetched to a reasonably high limit. s3Object = _s3.getObject(new GetObjectRequest(bucket, path).withRange(0, 2048)); } catch (AmazonS3Exception e) { if (e.getStatusCode() == 404) { throw new StashNotAvailableException(); } throw e; } try (BufferedReader in = new BufferedReader(new InputStreamReader(s3Object.getObjectContent(), Charsets.UTF_8))) { return in.readLine(); } catch (IOException e) { throw Throwables.propagate(e); } }
@Override protected InputStream open(long offset) { return getClient().getObject(new GetObjectRequest(_bk[0], _bk[1]).withRange(offset, _to)).getObjectContent(); }
.withRange(targetPos, contentRangeFinish - 1); wrappedStream = client.getObject(request).getObjectContent(); contentRangeStart = targetPos;
.withRange(targetPos, contentRangeFinish - 1); wrappedStream = client.getObject(request).getObjectContent(); contentRangeStart = targetPos;
/** * Make S3 request with a specified range. * @param rng Range to request * @param ver Version of object to fetch * @return Request */ private GetObjectRequest request(final Range rng, final Version ver) { final GetObjectRequest request = new GetObjectRequest(this.bucket, this.key); if (!rng.equals(Range.ENTIRE)) { request.withRange(rng.first(), rng.last()); } if (!ver.latest()) { request.withVersionId(ver.version()); } return request; }
.withRange(targetPos, contentRangeFinish - 1); if (S3AEncryptionMethods.SSE_C.equals(serverSideEncryptionAlgorithm) && StringUtils.isNotBlank(serverSideEncryptionKey)){
@Override public InputStream openStream() throws IOException { try { final long start; final long end = objectMetadata.getContentLength() - 1; if (offset > 0 && offset < objectMetadata.getContentLength()) { start = offset; } else if (offset < 0 && (-1 * offset) < objectMetadata.getContentLength()) { start = objectMetadata.getContentLength() + offset; } else { start = 0; } final GetObjectRequest request = new GetObjectRequest(config.getS3Bucket(), taskKey) .withMatchingETagConstraint(objectMetadata.getETag()) .withRange(start, end); return service.getObject(request).getObjectContent(); } catch (AmazonServiceException e) { throw new IOException(e); } } }
s3Object = _s3.getObject(new GetObjectRequest(_bucket, _key).withRange(start, end));
s3Object = _s3.getObject(new GetObjectRequest(_bucket, _key).withRange(start, end));