<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      阿里云 OSS postObject V4 使用

      背景

      類似預簽名的方式,后端生成了簽名和policy, 前端使用表單提交。

      提示

      如果可以,盡量使用簡單的方式,比如前端用accessKeyId+accessKeySecret 的方式直接putObject.但這種方法會暴露secret。

      也可以通過自己服務后臺上傳到aliyun oss. 但這種方式會占用自己服務的帶寬。

      如果使用post Object 的方式,很麻煩,官方文檔也的也不好(寫的跟si一樣)。調試了1天,幫doubao的幫助下才搞定。

      后端代碼

          public AliyunOssSignatureView genSignature() throws Exception {
      
              // 步驟1:創建policy。
              ObjectMapper mapper = new ObjectMapper();
      
              Map<String, Object> policy = new HashMap<>();
              policy.put("expiration", formatISODateTime());
      
              List<Object> conditions = new ArrayList<>();
      
              Map<String, String> bucketCondition = new HashMap<>();
              bucketCondition.put("bucket", bucketName);
              conditions.add(bucketCondition);
      
              Map<String, String> signatureVersionCondition = new HashMap<>();
              signatureVersionCondition.put("x-oss-signature-version", "OSS4-HMAC-SHA256");
              conditions.add(signatureVersionCondition);
      
              Map<String, String> credentialCondition = new HashMap<>();
              credentialCondition.put("x-oss-credential", accessKeyId + "/" + formatISODate() +  "/" + region + "/oss/aliyun_v4_request"); // 替換為實際的 access key id
              conditions.add(credentialCondition);
      
              Map<String, String> dateCondition = new HashMap<>();
              dateCondition.put("x-oss-date", formateDateTimeZ());
              conditions.add(dateCondition);
      
              conditions.add(Arrays.asList("content-length-range", 1L, 1024L*1024*1024*5));
              conditions.add(Arrays.asList("eq", "$success_action_status", "201"));
      //        conditions.add(Arrays.asList("starts-with", "$key", "user/eric/"));
      //        conditions.add(Arrays.asList("in", "$content-type", Arrays.asList("image/jpg", "image/png")));
      //        conditions.add(Arrays.asList("not-in", "$cache-control", Arrays.asList("no-cache")));
      
              policy.put("conditions", conditions);
      
              String jsonPolicy = mapper.writeValueAsString(policy);
              // 步驟2:構造待簽名字符串(StringToSign)。
              String stringToSign = new String(Base64.encodeBase64(jsonPolicy.getBytes()));
      
      
              // 步驟3:計算SigningKey。
              byte[] dateKey = hmacsha256(("aliyun_v4" + accessKeySecret).getBytes(), formatISODate());
              byte[] dateRegionKey = hmacsha256(dateKey, region);
              byte[] dateRegionServiceKey = hmacsha256(dateRegionKey, "oss");
              byte[] signingKey = hmacsha256(dateRegionServiceKey, "aliyun_v4_request");
      
              // 步驟4:計算Signature。
              byte[] result = hmacsha256(signingKey, stringToSign);
              String signature = BinaryUtil.toHex(result);
              log.info("policy:{}, base64 policy:{}, signature:{}", jsonPolicy, stringToSign, signature);
              return new AliyunOssSignatureView(stringToSign, signature);
          }
      
       public static byte[] hmacsha256(byte[] key, String data) {
              try {
                  // 初始化HMAC密鑰規格,指定算法為HMAC-SHA256并使用提供的密鑰。
                  SecretKeySpec secretKeySpec = new SecretKeySpec(key, "HmacSHA256");
      
                  // 獲取Mac實例,并通過getInstance方法指定使用HMAC-SHA256算法。
                  Mac mac = Mac.getInstance("HmacSHA256");
                  // 使用密鑰初始化Mac對象。
                  mac.init(secretKeySpec);
      
                  // 執行HMAC計算,通過doFinal方法接收需要計算的數據并返回計算結果的數組。
                  byte[] hmacBytes = mac.doFinal(data.getBytes());
      
                  return hmacBytes;
              } catch (Exception e) {
                  throw new RuntimeException("Failed to calculate HMAC-SHA256", e);
              }
          }
      
          /**
           * 獲取ISO格式時間
           * 2024-12-03T13:00:00Z
           * @return current time iso format
           */
          public static String formatISODateTime() {
              ZonedDateTime utcTime = ZonedDateTime.now();
              utcTime = utcTime.plusHours(1);
              return utcTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
          }
      
          public static String formatISODate() {
      
              LocalDate utcDate = LocalDate.now(ZoneOffset.UTC);
              return utcDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
          }
      
          public static String formateDateTimeZ() {
              ZonedDateTime utcTime = ZonedDateTime.now(ZoneOffset.UTC);
              return utcTime.format(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'"));
          }

      其中region 一定要注意,是類似cn-beijing 這樣的,而不是oss-cn-beijing.

      endpoint 是 https://bucketName.oss-cn-beijing.alyuncs.com

      上面使用的base64是apache 的。不是java自帶的

       

       

      前端代碼

       

      filed的順序一定不能錯,錯了就不好使

      image

       policy 和 x-oss-signature 都是后端生成返回給前端。

      key 是上傳后的文件名稱

      前端可以把policy base64 decode一下。在里面取x-oss-signature-version,x-oss-credential, x-oss-date,success_action_status這些值 。

      file 是要上傳的文件

       

      參考文檔

      https://help.aliyun.com/zh/oss/developer-reference/postobject

      https://help.aliyun.com/zh/oss/developer-reference/signature-version-4-recommend?spm=a2c4g.11186623.0.0.700a713f3JNlJV#79752ac45behk

       

      posted on 2025-10-30 09:57  cococooder  閱讀(161)  評論(1)    收藏  舉報

      主站蜘蛛池模板: 中文字幕在线视频不卡一区二区| 国产成人一区二区免av| 亚洲成人精品一区二区中| 国产亚欧女人天堂AV在线| 成人无码午夜在线观看| 97一区二区国产好的精华液| 99久久婷婷国产综合精品| 欧美牲交a欧美牲交aⅴ免费真| 色久综合色久综合色久综合| 又大又粗又硬又爽黄毛少妇| 国产乱妇乱子视频在播放| 久久精品国产91精品亚洲| 国产成人剧情AV麻豆果冻| 中文字幕在线日韩| 好吊视频专区一区二区三区| 亚洲国产精品综合久久网络| 超碰国产天天做天天爽| 日韩有码中文字幕国产| 国产成人无码一二三区视频| 最新国产AV最新国产在钱| 国产精品免费视频不卡| 九九热精品在线观看| 午夜精品极品粉嫩国产尤物| 乱码精品一区二区三区| 色综合网天天综合色中文| 九九热在线免费播放视频| AV最新高清无码专区| 国产午夜大地久久| 人妻激情视频一区二区三区| 国产日韩入口一区二区| 欧美精品一区二区在线观看播放| 久久日韩精品一区二区五区| 国产三级精品三级在线观看| 精品精品国产国产自在线| 卡一卡2卡3卡精品网站| 久久精品娱乐亚洲领先| 国内精品综合九九久久精品| 日本a在线播放| 夜夜躁狠狠躁日日躁| 九九热99精品视频在线| 欧美性猛交xxxx免费看|