public S3Object getObject(String bucket, String key) { return getObject(new GetObjectRequest(bucket, key)); }
@Override public S3Object getObject(String bucketName, String key) throws SdkClientException, AmazonServiceException { return getObject(new GetObjectRequest(bucketName, key)); }
@Override public S3Object getObject(String bucketName, String key) throws SdkClientException, AmazonServiceException { return getObject(new GetObjectRequest(bucketName, key)); }
private S3Object getS3ObjectAndMetadata(String bucket, String key, SSECustomerKey ssecLocal) { GetObjectRequest getObjectRequest; if (ssecLocal != null) { getObjectRequest = new GetObjectRequest(bucket, key).withSSECustomerKey(ssecLocal); } else { getObjectRequest = new GetObjectRequest(bucket, key); } return s3Client.getObject(getObjectRequest); }
/** * Creates and return a get object request for an instruction file. * * @param s3objectId * an S3 object id (not the instruction file id) * @param instFileSuffix * suffix of the specific instruction file to be used, or null if * the default instruction file is to be used. */ final GetObjectRequest createInstructionGetRequest( S3ObjectId s3objectId, String instFileSuffix) { return new GetObjectRequest( s3objectId.instructionFileId(instFileSuffix)); }
AmazonS3Client s3Client = new AmazonS3Client(myCredentials); File localFile = new File("localFilename"); ObjectMetadata object = s3Client.getObject(new GetObjectRequest("bucket", "s3FileName"), localFile);
/** * Downloads the payload stored in the s3 object. * * @param path the S3 key of the object * @return an input stream containing the contents of the object * Caller is expected to close the input stream. */ @Override public InputStream download(String path) { try { S3Object s3Object = s3Client.getObject(new GetObjectRequest(bucketName, path)); return s3Object.getObjectContent(); } catch (SdkClientException e) { String msg = "Error communicating with S3"; logger.error(msg, e); throw new ApplicationException(ApplicationException.Code.BACKEND_ERROR, msg, e); } }
@Override protected InputStream openObjectStream(URI object, long start) throws IOException { final String bucket = object.getAuthority(); final String key = S3Utils.extractS3Key(object); final GetObjectRequest request = new GetObjectRequest(bucket, key); request.setRange(start); try { final S3Object s3Object = s3Client.getObject(request); if (s3Object == null) { throw new ISE( "Failed to get an s3 object for bucket[%s], key[%s], and start[%d]", bucket, key, start ); } return s3Object.getObjectContent(); } catch (AmazonS3Exception e) { throw new IOException(e); } }
@Override protected InputStream open(long offset) { return getClient().getObject(new GetObjectRequest(_bk[0], _bk[1]).withRange(offset, _to)).getObjectContent(); }
/*** * Download a S3 object to local directory * * @param s3ObjectSummary S3 object summary for the object to download * @param targetDirectory Local target directory to download the object to * @throws IOException If any errors were encountered in downloading the object */ public void downloadS3Object(S3ObjectSummary s3ObjectSummary, String targetDirectory) throws IOException { final AmazonS3 amazonS3 = getS3Client(); final GetObjectRequest getObjectRequest = new GetObjectRequest( s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey()); final S3Object s3Object = amazonS3.getObject(getObjectRequest); final String targetFile = StringUtils.removeEnd(targetDirectory, File.separator) + File.separator + s3Object.getKey(); FileUtils.copyInputStreamToFile(s3Object.getObjectContent(), new File(targetFile)); LOGGER.info("S3 object downloaded to file: " + targetFile); }
public Integer retriableCall() throws IOException { GetObjectRequest req = new GetObjectRequest(bucketName, remotePath); req.setRange(firstByte, endByte); try (S3ObjectInputStream is = s3Client.getObject(req).getObjectContent()) { byte[] readBuf = new byte[4092]; int rCnt; int readTotal = 0; int incomingOffet = off; while ((rCnt = is.read(readBuf, 0, readBuf.length)) >= 0) { System.arraycopy(readBuf, 0, b, incomingOffet, rCnt); readTotal += rCnt; incomingOffet += rCnt; } if (readTotal == 0 && rCnt == -1) return -1; offset += readTotal; return readTotal; } } }.call();
private static S3Object getObjectForKey(Key k, long offset, long length) throws IOException { String[] bk = decodeKey(k); GetObjectRequest r = new GetObjectRequest(bk[0], bk[1]); r.setRange(offset, offset + length - 1); // Range is *inclusive* according to docs??? return getClient().getObject(r); }
private ObjectMetadata downloadTo0(final File destination, RequestMetricCollector requestMetricCollector) { GetObjectRequest req = new GetObjectRequest(getBucketName(), getKey()) .withRequestMetricCollector(requestMetricCollector); return getAmazonS3Client().getObject(req, destination); }
.run("getS3Object", () -> { try { GetObjectRequest request = new GetObjectRequest(host, keyFromPath(path)).withRange(start); return s3.getObject(request).getObjectContent();
/** * Opens a new stream at mPos if the wrapped stream mIn is null. */ private void openStream() { if (mIn != null) { // stream is already open return; } GetObjectRequest getReq = new GetObjectRequest(mBucketName, mKey); // If the position is 0, setting range is redundant and causes an error if the file is 0 length if (mPos > 0) { getReq.setRange(mPos); } AmazonS3Exception lastException = null; while (mRetryPolicy.attempt()) { try { mIn = mClient.getObject(getReq).getObjectContent(); return; } catch (AmazonS3Exception e) { LOG.warn("Attempt {} to open key {} in bucket {} failed with exception : {}", mRetryPolicy.getAttemptCount(), mKey, mBucketName, e.toString()); if (e.getStatusCode() != HttpStatus.SC_NOT_FOUND) { throw e; } // Key does not exist lastException = e; } } // Failed after retrying key does not exist throw lastException; }
private ObjectMetadata downloadTo0(final OutputStream output, RequestMetricCollector requestMetricCollector) { GetObjectRequest req = new GetObjectRequest(getBucketName(), getKey()) .withRequestMetricCollector(requestMetricCollector); S3Object s3Object = getAmazonS3Client().getObject(req); S3ObjectInputStream objectContent = s3Object.getObjectContent(); try { byte[] buffer = new byte[1024 * 10]; int bytesRead = -1; while ((bytesRead = objectContent.read(buffer)) > -1) { output.write(buffer, 0, bytesRead); } } catch (IOException ioe) { objectContent.abort(); throw new SdkClientException("Unable to transfer content from Amazon S3 to the output stream", ioe); } finally { try { objectContent.close(); } catch (IOException ioe) {} } return s3Object.getObjectMetadata(); }
@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"); } }