最近更新时间:2021-09-18
被处理图片是存放对象存储桶内的存储对象。
使用桶域名拼接链接可直接请求。
public void testResizeImage() { String style = "x-oss-process=image/resize,m_fixed,w_100,h_100"; try { String path = "http://" + END_POINT + "/" + objectName + "?" + style; URL newUrl = new URL(path); //download URLConnection con = newUrl.openConnection(); con.setConnectTimeout(5 * 1000); InputStream s3is = con.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("s3Demo/src/L.jpeg")); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Test resize image done!"); } |
public void testRotateImage() { String style = "x-oss-process=image/rotate,90"; try { String path = "http://" + END_POINT + "/" + objectName + "?" + style; URL newUrl = new URL(path); //download URLConnection con = newUrl.openConnection(); con.setConnectTimeout(5 * 1000); InputStream s3is = con.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("s3Demo/src/L.jpeg")); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Test rotate image done!"); } |
public void testWatermarkImage() { String style = "x-oss-process=image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ,x_0,y_0"; try { String path = "http://" + END_POINT + "/" + objectName + "?" + style; URL newUrl = new URL(path); //download URLConnection con = newUrl.openConnection(); con.setConnectTimeout(5 * 1000); InputStream s3is = con.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("s3Demo/src/L.jpeg")); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Test watermark image done!"); } |
拿到预签名后添加图片处理style。
public void testPrivateResizeImage() { String style = "x-oss-process=image/resize,m_fixed,w_100,h_100"; try { // Set the presigned URL to expire after one hour. java.util.Date expiration = new java.util.Date(); long expTimeMillis = expiration.getTime(); expTimeMillis += 1000 * 60 * 60 * 60; expiration.setTime(expTimeMillis); // Generate the presigned URL. System.out.println("Generating pre-signed URL."); GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest("", "") .withKey(objectName) .withMethod(HttpMethod.GET) .withExpiration(expiration); URL url = s3.generatePresignedUrl(generatePresignedUrlRequest); System.out.println("Pre-Signed URL: " + url.toString()); //add process style String path = url.toString(); String[] pathParts = path.split("\\?"); StringBuilder newPath = new StringBuilder(pathParts[0] + "?" + style + "&"); for (int i = 1; i < pathParts.length; i++) { newPath.append(pathParts[i]); } System.out.println("newUrl: " + newPath.toString()); URL newUrl = new URL(newPath.toString()); //download URLConnection con = newUrl.openConnection(); con.setConnectTimeout(5 * 1000); InputStream s3is = con.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("s3Demo/src/L.jpeg")); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = s3is.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } s3is.close(); fos.close(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Test resize image done!"); } } |