diff --git a/api/src/main/java/io/minio/MinioClient.java b/api/src/main/java/io/minio/MinioClient.java index bd6d7baa1..6b26dac2b 100644 --- a/api/src/main/java/io/minio/MinioClient.java +++ b/api/src/main/java/io/minio/MinioClient.java @@ -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. * *

Example:
- *
{@code String url = minioClient.presignedGetObject("my-bucketname", "my-objectname", 60 * 60 * 24);
+   * 
{@code String url = minioClient.presignedGetObject("my-bucketname", "my-objectname", 60 * 60 * 24, reqParams);
    * System.out.println(url); }
* * @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. * @@ -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 reqParams) throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException, InternalException, InvalidExpiresRangeException { @@ -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. + * + *

Example:
+ *
{@code String url = minioClient.presignedGetObject("my-bucketname", "my-objectname", 60 * 60 * 24);
+   * System.out.println(url); }
+ * + * @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. @@ -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); } diff --git a/functional/FunctionalTest.java b/functional/FunctionalTest.java index 2e03730d0..f044b6019 100644 --- a/functional/FunctionalTest.java +++ b/functional/FunctionalTest.java @@ -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); @@ -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 reqParams)"); + String fileName = createFile(3 * MB); + client.putObject(bucketName, fileName, fileName); + + Map 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 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 reqParams)" + + ", Response: " + response + + ", Error: " + errorXml); + } + } else { + throw new Exception("[FAILED] Test: presignedGetObject(String bucketName, String objectName," + + " Integer expires, Map reqParams)" + + ", Error: "); + } + + 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 reqParams)" + + ", Error: "); + } + + Files.delete(Paths.get(fileName)); + Files.delete(Paths.get(fileName + ".downloaded")); + client.removeObject(bucketName, fileName); + } + /** * public String presignedPutObject(String bucketName, String objectName). */ @@ -801,6 +865,7 @@ public static void main(String[] args) { presignedGetObject_test1(); presignedGetObject_test2(); + presignedGetObject_test3(); presignedPutObject_test1(); presignedPutObject_test2();