public S3Object getObject(GetObjectRequest request) { return amazonS3.getObject(serverSideEncryption.decorate(request)); }
@Override public S3Object getS3ObjectStream() { S3Object s3Object = s3.getObject(getObjectRequest); download.setS3Object(s3Object); return s3Object; }
try { AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); String bucketName = getBucketName(); s3.createBucket(bucketName); S3Object object = s3.getObject(bucketName, getKey()); } catch (AmazonServiceException e) { String errorCode = e.getErrorCode(); if (!errorCode.equals("NoSuchKey")) { throw e; } Logger.getLogger(getClass()).debug("No such key!!!", e); }
@Override public InputStream read( UUID appId, Entity entity, long offset, long length ) throws Exception { S3Object object = getS3Client().getObject( bucketName, AssetUtils.buildAssetKey( appId, entity ) ); byte data[] = null; if ( offset == 0 && length == FIVE_MB ) { return object.getObjectContent(); } else { object.getObjectContent().read(data, Ints.checkedCast(offset), Ints.checkedCast(length)); } return new ByteArrayInputStream(data); }
/** * 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); } }
S3Object object = s3.getObject(getPartRequest);
@Override protected InputStream open(long offset) { return getClient().getObject(new GetObjectRequest(_bk[0], _bk[1]).withRange(offset, _to)).getObjectContent(); }
/** * 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 ); } }
/*** * 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();
@Override public OutputContentFile getFile(String merchantStoreCode, FileContentType fileContentType, String contentName) throws ServiceException { try { // get buckets String bucketName = bucketName(); final AmazonS3 s3 = s3Client(); S3Object o = s3.getObject(bucketName, nodePath(merchantStoreCode, fileContentType) + contentName); byte[] byteArray = IOUtils.toByteArray(o.getObjectContent()); ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length); baos.write(byteArray, 0, byteArray.length); OutputContentFile ct = new OutputContentFile(); ct.setFile(baos); LOGGER.info("Content getFile"); return ct; } catch (final Exception e) { LOGGER.error("Error while getting file", e); throw new ServiceException(e); } }
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); }
/** * 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; }
try { GetObjectRequest request = new GetObjectRequest(host, keyFromPath(path)).withRange(start); return s3.getObject(request).getObjectContent();
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(); }
S3Object o = s3.getObject(bucketName, os.getKey()); byte[] byteArray = IOUtils.toByteArray(o.getObjectContent()); ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
S3Object o = s3.getObject(bucketName, os.getKey()); byte[] byteArray = IOUtils.toByteArray(o.getObjectContent()); ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
S3Object obj = s3.getObject(bucketName, key);
@Test public void s3Test() throws IOException { AmazonS3 s3 = AmazonS3ClientBuilder .standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(S3)) .withCredentials(localstack.getDefaultCredentialsProvider()) .build(); s3.createBucket("foo"); s3.putObject("foo", "bar", "baz"); final List<Bucket> buckets = s3.listBuckets(); assertEquals("The created bucket is present", 1, buckets.size()); final Bucket bucket = buckets.get(0); assertEquals("The created bucket has the right name", "foo", bucket.getName()); assertEquals("The created bucket has the right name", "foo", bucket.getName()); final ObjectListing objectListing = s3.listObjects("foo"); assertEquals("The created bucket has 1 item in it", 1, objectListing.getObjectSummaries().size()); final S3Object object = s3.getObject("foo", "bar"); final String content = IOUtils.toString(object.getObjectContent(), Charset.forName("UTF-8")); assertEquals("The object can be retrieved", "baz", content); } }