Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1355,15 +1355,17 @@ public void getObject(String bucketName, String objectName, String fileName)


/**
* Returns an presigned URL to download the object in the bucket with given expiry time.
* Returns an presigned URL to download the object in the bucket with given expiry time with custom request params.
*
* </p><b>Example:</b><br>
* <pre>{@code String url = minioClient.presignedGetObject("my-bucketname", "my-objectname", 60 * 60 * 24);
* <pre>{@code String url = minioClient.presignedGetObject("my-bucketname", "my-objectname", 60 * 60 * 24, reqParams);
* System.out.println(url); }</pre>
*
* @param bucketName Bucket name.
* @param objectName Object name in the bucket.
* @param expires Expiration time in seconds of presigned URL.
* @param reqParams Override values for set of response headers. Currently supported request parameters are
* [response-expires, response-content-type, response-cache-control, response-content-disposition]
*
* @return string contains URL to download the object.
*
Expand All @@ -1373,7 +1375,8 @@ public void getObject(String bucketName, String objectName, String fileName)
* @throws NoSuchAlgorithmException upon requested algorithm was not found during signature calculation
* @throws InvalidExpiresRangeException upon input expires is out of range
*/
public String presignedGetObject(String bucketName, String objectName, Integer expires)
public String presignedGetObject(String bucketName, String objectName, Integer expires,
Map<String, String> reqParams)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException, InvalidExpiresRangeException {
Expand All @@ -1386,11 +1389,37 @@ public String presignedGetObject(String bucketName, String objectName, Integer e
String region = BucketRegionCache.INSTANCE.region(bucketName);

Request request = createRequest(Method.GET, bucketName, objectName, region,
null, null, null, null, 0);
null, reqParams, null, null, 0);
HttpUrl url = Signer.presignV4(request, region, accessKey, secretKey, expires);
return url.toString();
}

/**
* Returns an presigned URL to download the object in the bucket with given expiry time.
*
* </p><b>Example:</b><br>
* <pre>{@code String url = minioClient.presignedGetObject("my-bucketname", "my-objectname", 60 * 60 * 24);
* System.out.println(url); }</pre>
*
* @param bucketName Bucket name.
* @param objectName Object name in the bucket.
* @param expires Expiration time in seconds of presigned URL.
*
* @return string contains URL to download the object.
*
* @throws InvalidBucketNameException upon an invalid bucket name
* @throws InvalidKeyException upon an invalid access key or secret key
* @throws IOException upon signature calculation failure
* @throws NoSuchAlgorithmException upon requested algorithm was not found during signature calculation
* @throws InvalidExpiresRangeException upon input expires is out of range
*/
public String presignedGetObject(String bucketName, String objectName, Integer expires)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException, InvalidExpiresRangeException {
return presignedGetObject(bucketName, objectName, expires, null);
}


/**
* Returns an presigned URL to download the object in the bucket with default expiry time.
Expand All @@ -1413,7 +1442,7 @@ public String presignedGetObject(String bucketName, String objectName)
throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
InternalException, InvalidExpiresRangeException {
return presignedGetObject(bucketName, objectName, DEFAULT_EXPIRY_TIME);
return presignedGetObject(bucketName, objectName, DEFAULT_EXPIRY_TIME, null);
}


Expand Down
67 changes: 66 additions & 1 deletion functional/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static void removeBucket_test() throws Exception {
}

/**
* Tear down test setup.
* Tear down test setup.
*/
public static void setup() throws Exception {
client.makeBucket(bucketName);
Expand Down Expand Up @@ -595,6 +595,70 @@ public static void presignedGetObject_test2() throws Exception {
client.removeObject(bucketName, fileName);
}

/**
* public String presignedGetObject(String bucketName, String objectName, Integer expires, Map reqParams).
*/
public static void presignedGetObject_test3() throws Exception {
System.out.println("Test: presignedGetObject(String bucketName, String objectName, Integer expires, "
+ "Map<String, String> reqParams)");
String fileName = createFile(3 * MB);
client.putObject(bucketName, fileName, fileName);

Map<String, String> reqParams = new HashMap<>();
reqParams.put("response-content-type", "application/json");

String urlString = client.presignedGetObject(bucketName, fileName, 3600, reqParams);
Request.Builder requestBuilder = new Request.Builder();
Request request = requestBuilder
.url(HttpUrl.parse(urlString))
.method("GET", null)
.build();
OkHttpClient transport = new OkHttpClient();
Response response = transport.newCall(request).execute();

if (response != null) {
if (response.isSuccessful()) {
OutputStream os = Files.newOutputStream(Paths.get(fileName + ".downloaded"), StandardOpenOption.CREATE);
ByteStreams.copy(response.body().byteStream(), os);
if (!response.header("Content-Type").equals("application/json")) {
throw new Exception("[FAILED] Test: presignedGetObject(String bucketName, String objectName,"
+ " Integer expires, Map<String, String> reqParams)"
+ ", Response: " + response);
}
response.body().close();
os.close();
} else {
String errorXml = "";

// read entire body stream to string.
Scanner scanner = new java.util.Scanner(response.body().charStream()).useDelimiter("\\A");
if (scanner.hasNext()) {
errorXml = scanner.next();
}

throw new Exception("[FAILED] Test: presignedGetObject(String bucketName, String objectName,"
+ " Integer expires, Map<String, String> reqParams)"
+ ", Response: " + response
+ ", Error: " + errorXml);
}
} else {
throw new Exception("[FAILED] Test: presignedGetObject(String bucketName, String objectName,"
+ " Integer expires, Map<String, String> reqParams)"
+ ", Error: <No response from server>");
}

if (!Arrays.equals(Files.readAllBytes(Paths.get(fileName)),
Files.readAllBytes(Paths.get(fileName + ".downloaded")))) {
throw new Exception("[FAILED] Test: presignedGetObject(String bucketName, String objectName,"
+ " Integer expires, Map<String, String> reqParams)"
+ ", Error: <Content differs>");
}

Files.delete(Paths.get(fileName));
Files.delete(Paths.get(fileName + ".downloaded"));
client.removeObject(bucketName, fileName);
}

/**
* public String presignedPutObject(String bucketName, String objectName).
*/
Expand Down Expand Up @@ -801,6 +865,7 @@ public static void main(String[] args) {

presignedGetObject_test1();
presignedGetObject_test2();
presignedGetObject_test3();

presignedPutObject_test1();
presignedPutObject_test2();
Expand Down