生成oss私有地址的访问URL

satuo20 1年前 ⋅ 295 阅读
package com.yjzhixue.cmadmin.web.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.yjzhixue.common.api.exception.ServiceException;

import java.net.URL;
import java.util.Date;


public class OssUrlUtil {

   /**
    * 根据真实地址返回带验证的地址信息
    * @param ossClient
    * @param url
    * @return
    */
   public static String createSignedUrl(OSS ossClient, String url) {
       String[] bucketInfo = getBucketInfo(url);
       // Set the validity period of the signed URL to 3,600 seconds (1 hour).
       Date expiration = new Date(new Date().getTime() + 3600 * 1000);
       // Generate the signed URL that allows HTTP GET requests. Visitors can enter the URL in a browser to access specified OSS resources.
       String surl = createSignedUrl(ossClient, bucketInfo[0], bucketInfo[1]);
//        System.err.println(surl);
       return surl;
   }

   private static String createSignedUrl(OSS ossClient, String bucket, String objectName) {

       // Set the validity period of the signed URL to 3,600 seconds (1 hour).
       Date expiration = new Date(new Date().getTime() + 3600 * 1000);
       // Generate the signed URL that allows HTTP GET requests. Visitors can enter the URL in a browser to access specified OSS resources.
       URL url = ossClient.generatePresignedUrl(bucket, objectName, expiration);
       String surl = url.toString().replace("oss-cn-shenzhen.aliyuncs", "yjzhixue");
//        System.err.println(surl);
       return surl;
   }

   private static String[] getBucketInfo(String url) {
       // https://kpy-cmadmin.yjzhixue.com/ordinaryImage/072167110565377600001/2023/02/346fb5b3-a8e3-4618-8d12-9c9bfed0d1dd.jpg?Expires=1679366604&OSSAccessKeyId=TMP.3KhLMAuyCCSE6hwic37s96216MwDAGfXggEkCBqnUCbj8cKV7mEizBcXmwxviJswnMNmz1fuorU7CABcjd1QLQEk2mYuPr&Signature=tT0CLEixsVjtd%2F%2B0eWPlmFN8peA%3D
       if (!url.startsWith("https://")) {
           throw new ServiceException("错误url");
       }
       url = url.substring(8);
//        System.err.println(url);
       String objectName = url.substring(url.indexOf("/") + 1);
       int i = objectName.indexOf("?");
       if (i != -1) {
           objectName = objectName.substring(0, i);
       }
//        System.err.println(objectName);
       String bucketName = url.substring(0, url.indexOf("."));
//        System.err.println(bucketName);
       return new String[]{bucketName, objectName};
   }

   public static void main(String[] args) {
       String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
       String accessKeyId = "accessKeyId";
       String accessKeySecret = "accessKeySecret";

       // Create an OSSClient instance.
       OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

       String url = "https://kpy-cmadmin.yjzhixue.com/ordinaryImage/072167110565377600001/2023/02/346fb5b3-a8e3-4618-8d12-9c9bfed0d1dd.jpg";
       String surl = createSignedUrl(ossClient, url);
       System.err.println(surl);

       // Shut down the OSSClient instance.
       ossClient.shutdown();
   }

}


全部评论: 0

    我有话说: