OSS私有库设置水印URL

直接上代码:

水印方式1:本地设置水印样式

    /**Region*/
    private static String endpoint = "";
    /**oos存储账号*/
    private static String accessKeyId = "";
    /**oos存储密码*/
    private static String accessKeySecret = "";
    /**oos存储桶*/
    public final static String bucketName = "";
    
    static OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    public static void main(String[] args) {
    	String key = "4bb8798b-b3a5-43c6-a6d1-d0520be8bf31.png";//oss上传后返回的文件名
    	String url = getWatermarkUrl(key, "文字水印");
    	System.out.println("=======:" + url);
    }
    
    /**
     * 
     * @方法名称 getWatermarkUrl
     * @功能描述 
获取带水印的
* @作者 yw * @创建时间 2020年7月22日 下午4:48:32 * @param key * @return */ public static String getWatermarkUrl(String key, String mark){ Base64 base64 = new Base64(); String base64Sign = ""; try { base64Sign = base64.encodeToString(mark.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // resize String style = "image/resize,m_fixed"; // add watermark into the image style = "image/watermark,text_" + base64Sign;//文字水印 // 设置URL过期时间为1小时 Date expiration = new Date(new Date().getTime() + 1800 * 1000); GeneratePresignedUrlRequest generatePresignedUrlRequest; generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, key); generatePresignedUrlRequest.setExpiration(expiration); //generatePresignedUrlRequest.setProcess("style/_key"); generatePresignedUrlRequest.setProcess(style); URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest); return url.toString(); }

水印方式2:使用oss建的水印

    public static String getWatermarkUrl(String key){
    	// 设置URL过期时间为1小时
    	Date expiration = new Date(new Date().getTime() + 1800 * 1000);//30min
    	GeneratePresignedUrlRequest generatePresignedUrlRequest;
    	generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, key);
    	generatePresignedUrlRequest.setExpiration(expiration);
    	generatePresignedUrlRequest.setProcess("style/_longone");//使用oss的样式
    	URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
    	return url.toString();
    }

OSS私有库设置水印URL_第1张图片

你可能感兴趣的:(笔记,开发问题,OSS)