生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。方法声明如下:
public String presignedGetObject(String bucketName, String objectName) public String presignedGetObject(String bucketName, String objectName, Integer expires)
其中:
bucketName:存储桶名称
objectName:存储桶里的对象名称
expiry:失效时间(以秒为单位),默认是7天,不得大于七天
实例:生成 files 存储桶下面 308608.jpg 图片对象的临时访问 URL 地址,默认该地址有效期为7天。使用该地址可以在浏览器中查看该图片对象,不用输入 accessKey 和 securetKey。代码如下:
package com.hxstrive.minio.demos.presigned; import io.minio.MinioClient; import io.minio.errors.MinioException; /** * 获取指定存储桶下对象的临时访问 URL 地址 * @author Administrator 2021/8/5 22:43 * @version 1.0 */ public class PresignedGetObject { /** MinioClient.presignedGetObject() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); // 生成 files 存储桶下面 308608.jpg 图片对象的临时访问地址 // 默认该地址有效期为7天 String url = minioClient.presignedGetObject( "files", "308608.jpg"); System.out.println(url); // 生成 files 存储桶下面 308608.jpg 图片对象的临时访问地址 // 手动指定过期时间为 30 秒 String url2 = minioClient.presignedGetObject( "files", "308608.jpg", 30); System.out.println(url2); } catch (MinioException e) { e.printStackTrace(); } } }
生成一个给HTTP PUT请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。方法声明如下:
public String presignedPutObject(String bucketName, String objectName) public String presignedPutObject(String bucketName, String objectName, Integer expires)
其中:
bucketName:存储桶名称
objectName:存储桶里的对象名称
expiry:失效时间(以秒为单位),默认是7天,不得大于七天
实例:获取一个 PUT 地址,然后使用 HttpComponent 库发起一个 PUT 请求,将一段文本保存到对象中;再次获取对象的 GET 地址,获取对象内容且输出到控制台。代码如下:
package com.hxstrive.minio.demos.presigned; import io.minio.MinioClient; import io.minio.errors.MinioException; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.InputStream; /** * 生成一个给 HTTP PUT 请求用的 presigned URL * @author Administrator 2021/8/5 22:50 * @version 1.0 */ public class PresignedPutObject { /** MinioClient.presignedPutObject() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); // 获取指定存储桶下对象的 PUT URL 地址 String putUrl = minioClient.presignedPutObject( "files", "myObject.txt"); System.out.println("put url = " + putUrl); // 模拟一个 HTTP PUT 请求 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(putUrl); httpPut.setEntity(new StringEntity("Hello, World")); CloseableHttpResponse responsePut = httpclient.execute(httpPut); StatusLine statusLinePut = responsePut.getStatusLine(); System.out.println("status code = " + statusLinePut.getStatusCode()); System.out.println("reason phrase = " + statusLinePut.getReasonPhrase()); // 获取刚刚上传文件内容 String getUrl = minioClient.presignedGetObject( "files", "myObject.txt"); System.out.println("get url = " + getUrl); HttpGet httpGet = new HttpGet(getUrl); CloseableHttpResponse responseGet = httpclient.execute(httpGet); StatusLine statusLineGet = responseGet.getStatusLine(); System.out.println("status code = " + statusLineGet.getStatusCode()); System.out.println("reason phrase = " + statusLineGet.getReasonPhrase()); if(statusLineGet.getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = responseGet.getEntity(); InputStream inputStream = httpEntity.getContent(); byte[] bytes = new byte[(int)(httpEntity.getContentLength())]; inputStream.read(bytes); inputStream.close(); System.out.println("content = " + new String(bytes)); } } catch (MinioException e) { e.printStackTrace(); } } }
允许给POST请求的presigned URL设置策略,比如接收对象上传的存储桶名称的策略,key名称前缀,过期策略。方法声明如下:
public Map<String, String> presignedPostPolicy(PostPolicy policy)
其中:
policy:对象的post策略
实例:获取存储桶 files 中 myObject.txt 对象的策略信息,代码如下:
package com.hxstrive.minio.demos.presigned; import io.minio.MinioClient; import io.minio.PostPolicy; import io.minio.errors.MinioException; import java.time.ZonedDateTime; import java.util.Map; /** * 给POST请求的presigned URL设置策略,比如接收对象上传的存储桶名称的策略,key名称前缀,过期策略 * @author Administrator 2021/8/6 8:50 * @version 1.0 */ public class PresignedPostPolicy { /** MinioClient.presignedGetObject() example. */ public static void main(String[] args) throws Exception { try { MinioClient minioClient = new MinioClient( "http://127.0.0.1:9000", "root", "mypassword"); // ZonedDateTime 需要JDK1.8+ ZonedDateTime expirationDate = ZonedDateTime.now(); PostPolicy postPolicy = new PostPolicy( "files", "myObject.txt", expirationDate); Map<String,String> map = minioClient.presignedPostPolicy(postPolicy); for(Map.Entry<String,String> entry : map.entrySet()) { System.out.println(entry.getKey() + " = " + entry.getValue()); } } catch (MinioException e) { e.printStackTrace(); } } }
输出如下:
bucket = files x-amz-date = 20210806T042936Z x-amz-signature = 04dacf1fcb35e572634519a5383f370366185110350bc9bf5a66c54dcff1c0cd key = myObject.txt x-amz-algorithm = AWS4-HMAC-SHA256 x-amz-credential = root/20210806/zh-east-1/s3/aws4_request policy = eyJleHBpcmF0aW9uIjoiMjAyMS0wOC0wNlQwNDoyOTozNS4zODBaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkYnVja2V0IiwiZmlsZXMiXSxbImVxIiwiJGtleSIsIm15T2JqZWN0LnR4dCJdLFsiZXEiLCIkeC1hbXotYWxnb3JpdGhtIiwiQVdTNC1ITUFDLVNIQTI1NiJdLFsiZXEiLCIkeC1hbXotY3JlZGVudGlhbCIsInJvb3QvMjAyMTA4MDYvemgtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJdLFsiZXEiLCIkeC1hbXotZGF0ZSIsIjIwMjEwODA2VDA0MjkzNloiXV19