@Override public <T> T readObject(String bucketName, String key, Class<T> objectType) { GetObjectResult<T> result = getObject(new GetObjectRequest(bucketName, key), objectType); return result == null ? null : result.getObject(); }
@Override public GetObjectResult<InputStream> getObject(String bucketName, String key) { if (aclMap.containsKey(key)) { return new GetObjectResult<>(); } else { return null; } } }
public S3ObjectMetadata getObjectMetadata() { return S3ObjectMetadata.fromHeaders(getHeaders()); }
@Override public <T> GetObjectResult<T> getObject(GetObjectRequest request, Class<T> objectType) { try { if (request.getRange() == null) { // enable checksum of the object (verification is handled in interceptor) request.property(RestUtil.PROPERTY_VERIFY_READ_CHECKSUM, Boolean.TRUE); } GetObjectResult<T> result = new GetObjectResult<T>(); ClientResponse response = executeRequest(client, request); fillResponseEntity(result, response); result.setObject(response.getEntity(objectType)); return result; } catch (S3Exception e) { // a 304 or 412 means If-* headers were used and a condition failed if (e.getHttpCode() == 304 || e.getHttpCode() == 412) return null; throw e; } }
@Override public InputStream readObjectStream(String bucketName, String key, Range range) { GetObjectResult<InputStream> result = getObject(new GetObjectRequest(bucketName, key).withRange(range), InputStream.class); return result == null ? null : result.getObject(); }
@Override public <T> T readObject(String bucketName, String key, String versionId, Class<T> objectType) { GetObjectResult<T> result = getObject(new GetObjectRequest(bucketName, key).withVersionId(versionId), objectType); return result == null ? null : result.getObject(); }
@Override public InputStream call() { GetObjectRequest request = new GetObjectRequest(config.getBucketName(), key).withVersionId(versionId); return s3.getObject(request, InputStream.class).getObject(); } }, OPERATION_OPEN_DATA_STREAM);
@Synchronized @Override public void putObject(String bucketName, String key, Range range, Object content) { byte[] totalByes = new byte[Math.toIntExact(range.getLast() + 1)]; try { if (range.getFirst() != 0) { int bytesRead = client.getObject(bucketName, key).getObject().read(totalByes, 0, Math.toIntExact(range.getFirst())); if (bytesRead != range.getFirst()) { throw new IllegalStateException("Unable to read from the object " + key); } } int bytesRead = ((InputStream) content).read(totalByes, Math.toIntExact(range.getFirst()), Math.toIntExact(range.getLast() + 1 - range.getFirst())); if (bytesRead != range.getLast() + 1 - range.getFirst()) { throw new IllegalStateException("Not able to read from input stream."); } client.putObject(new PutObjectRequest(bucketName, key, (Object) new ByteArrayInputStream(totalByes))); aclMap.put(key, aclMap.get(key).withSize(range.getLast() - 1)); } catch (IOException e) { throw new S3Exception("NoObject", HttpStatus.SC_NOT_FOUND, "NoSuchKey", key); } }
InputStream objectStream = s3.getObject(bucketName, key).getObject(); ChecksummedInputStream md5Stream = new ChecksummedInputStream(objectStream, new RunningChecksum(ChecksumAlgorithm.MD5)); byte[] buffer = new byte[128 * 1024];
@Ignore // only perform this test on a co-located ECS! @Test public void testVeryLargeUploadStream() throws Exception { String key = "large-stream-upload"; long size = 512L * 1024 * 1024 + 10; // 512MB + 10 bytes InputStream stream = new RandomInputStream(size); SyncObject object = new SyncObject(testStorage, key, new ObjectMetadata().withContentLength(size), stream, null); storage.updateObject(key, object); // hyphen denotes an MPU Assert.assertTrue(s3.getObjectMetadata(bucketName, key).getETag().contains("-")); // verify bytes read from source // first wait a tick so the perf counter has at least one interval Thread.sleep(1000); Assert.assertEquals(size, object.getBytesRead()); Assert.assertTrue(testStorage.getReadRate() > 0); // need to read the entire object since we can't use the ETag InputStream objectStream = s3.getObject(bucketName, key).getObject(); ChecksummedInputStream md5Stream = new ChecksummedInputStream(objectStream, new RunningChecksum(ChecksumAlgorithm.MD5)); byte[] buffer = new byte[128 * 1024]; int c; do { c = md5Stream.read(buffer); } while (c >= 0); md5Stream.close(); Assert.assertEquals(object.getMd5Hex(true).toUpperCase(), md5Stream.getChecksum().getHexValue().toUpperCase()); }