Skip to content

Commit

Permalink
Adds get object with offset and implicit length. Fixes minio#200.
Browse files Browse the repository at this point in the history
  • Loading branch information
fkautz committed Jul 13, 2015
1 parent c66a5d1 commit 877258f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/minio/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,24 @@ public void removeObject(String bucket, String key) throws IOException, ClientEx
throw new IOException();
}

/**
* Returns an InputStream containing a subset of the object. The InputStream must be
* closed or the connection will remain open.
*
* @param bucket object's bucket
* @param key object's key
* @param offsetStart Offset from the start of the object.
*
* @return an InputStream containing the object. Close the InputStream when done.
*
* @throws IOException upon connection failure
* @throws ClientException upon failure from server
*/
public InputStream getObject(String bucket, String key, long offsetStart) throws IOException, ClientException {
ObjectStat stat = statObject(bucket, key);
long length = stat.getLength() - offsetStart;
return getObject(bucket, key, offsetStart, length);
}

/**
* Returns an InputStream containing a subset of the object. The InputStream must be
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/minio/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,40 @@ public LowLevelHttpResponse execute() throws IOException {
Assert.fail("Should of thrown an exception");
}

public void testGetObjectWithOffset() throws IOException, ClientException {
final String expectedObject = "world";

MockHttpTransport transport = new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
response.addHeader("Content-Length", "6");
response.addHeader("Content-Type", "application/octet-stream");
response.addHeader("ETag", "\"5eb63bbbe01eeed093cb22bb8f5acdc3\"");
response.addHeader("Last-Modified", "Mon, 04 May 2015 07:58:51 GMT");
response.addHeader("Accept-Ranges", "bytes");
response.addHeader("Content-Range", "5-10/11");
response.setStatusCode(206);
response.setContent(expectedObject.getBytes("UTF-8"));
return response;
}
};
}
};

// get request
Client client = Client.getClient("http://localhost:9000");
client.setTransport(transport);
InputStream object = client.getObject("bucket", "key", 6);
byte[] result = new byte[5];
int read = object.read(result);
result = Arrays.copyOf(result, read);
assertEquals(expectedObject, new String(result, "UTF-8"));
}

@Test
public void testListObjects() throws IOException, XmlPullParserException, ParseException, ClientException {
final String body = "<ListBucketResult xmlns=\"http://doc.s3.amazonaws.com/2006-03-01\"><Name>bucket</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><Delimiter></Delimiter><IsTruncated>false</IsTruncated><Contents><Key>key</Key><LastModified>2015-05-05T02:21:15.716Z</LastModified><ETag>\"5eb63bbbe01eeed093cb22bb8f5acdc3\"</ETag><Size>11</Size><StorageClass>STANDARD</StorageClass><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner></Contents><Contents><Key>key2</Key><LastModified>2015-05-05T20:36:17.498Z</LastModified><ETag>\"2a60eaffa7a82804bdc682ce1df6c2d4\"</ETag><Size>1661</Size><StorageClass>STANDARD</StorageClass><Owner><ID>minio</ID><DisplayName>minio</DisplayName></Owner></Contents></ListBucketResult>";
Expand Down

0 comments on commit 877258f

Please sign in to comment.