Skip to content

Commit 146c561

Browse files
balamuruganaharshavardhana
authored andcommitted
getObjectUrl: add new api to get object URL. (#507)
The URL is only useful to retrieve the object's data if the object has public read permissions. Fixes #502
1 parent 4963d03 commit 146c561

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

api/src/main/java/io/minio/MinioClient.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,39 @@ public ObjectStat statObject(String bucketName, String objectName)
11421142
}
11431143

11441144

1145+
/**
1146+
* Gets object's URL in given bucket. The URL is ONLY useful to retrieve the object's data if the object has
1147+
* public read permissions.
1148+
*
1149+
* <p><b>Example:</b>
1150+
* <pre>{@code String url = minioClient.getObjectUrl("my-bucketname", "my-objectname");
1151+
* System.out.println(url); }</pre>
1152+
*
1153+
* @param bucketName Bucket name.
1154+
* @param objectName Object name in the bucket.
1155+
*
1156+
* @return string contains URL to download the object.
1157+
*
1158+
* @throws InvalidBucketNameException upon invalid bucket name is given
1159+
* @throws NoResponseException upon no response from server
1160+
* @throws IOException upon connection error
1161+
* @throws XmlPullParserException upon parsing response xml
1162+
* @throws ErrorResponseException upon unsuccessful execution
1163+
* @throws InternalException upon internal library error
1164+
*/
1165+
public String getObjectUrl(String bucketName, String objectName)
1166+
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
1167+
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
1168+
InternalException {
1169+
updateRegionCache(bucketName);
1170+
String region = BucketRegionCache.INSTANCE.region(bucketName);
1171+
1172+
Request request = createRequest(Method.GET, bucketName, objectName, region, null, null, null, null, 0);
1173+
HttpUrl url = request.httpUrl();
1174+
return url.toString();
1175+
}
1176+
1177+
11451178
/**
11461179
* Gets entire object's data as {@link InputStream} in given bucket. The InputStream must be closed
11471180
* after use else the connection will remain open.

api/src/test/java/io/minio/MinioClientTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ public void testPresignGetObject()
182182
assertEquals(presignedObjectUrl.isEmpty(), false);
183183
}
184184

185+
@Test
186+
public void testGetObjectUrl()
187+
throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException {
188+
MockWebServer server = new MockWebServer();
189+
server.start();
190+
191+
// get request
192+
MinioClient client = new MinioClient(server.url(""));
193+
String objectUrl = client.getObjectUrl(BUCKET, "key");
194+
assertEquals(objectUrl.isEmpty(), false);
195+
}
196+
185197
@Test
186198
public void testGetObject()
187199
throws NoSuchAlgorithmException, InvalidKeyException, IOException, XmlPullParserException, MinioException {

0 commit comments

Comments
 (0)