minio客户端文件直传

satuo20 8月前 ⋅ 271 阅读

minio客户端文件直传

流程:1. 获取上传凭证 2. 上传

java代码

package com.yjzhixue.cmadmin.web;

import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.http.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * 生成客户端上传凭证(URL)
 */
public class MINIOComponent {

    private String accessKey;
    private String secretKey;
    private String endpoint;    //http://192.168.0.69:9000

    /**
     * 获取上传凭证
     * @param bucket    桶名
     * @param objectName 对象名
     * @param expirySecond 过期时间(秒)
     * @return
     * @throws Exception
     */
    public String getPresignedObjectUrl(String bucket, String objectName, int expirySecond) throws Exception {
        MinioClient minioClient = MinioClient.builder().endpoint(endpoint)
                .credentials(accessKey, secretKey).build();
        Map<String, String> reqParams = new HashMap<>();
        reqParams.put("response-content-type", "application/json");
        String product = minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.PUT) //这里必须是PUT,如果是GET的话就是文件访问地址了。如果是POST上传会报错.
                        .bucket(bucket)
                        .object(objectName)
                        .expiry(expirySecond) // 凭证过期时间,单位秒
                        .extraQueryParams(reqParams)
                        .build());
        return product;
    }

    public static void main(String[] args) throws Exception {
        // 样例
        MINIOComponent component = new MINIOComponent();
        component.accessKey = "7L9xvYEQ0YBO8UH6dk16";
        component.secretKey = "wjPTPgMXbPahVv1W099ncbNoAd2oW9brIWQzKHlC";
        component.endpoint = "http://192.168.0.69:9000";
        String bucket = "my-datas";
        String objectName = "BaiduIMInstaller-02.zip";
        int expirySecond = 60 * 20;
        String presignedObjectUrl = component.getPresignedObjectUrl(bucket, objectName, expirySecond);
        System.err.println(presignedObjectUrl);


    }
}

upload.html

<!DOCTYPE html>
<html>
  <head lang="en">
    <meta charset="UTF-8" />
    
    <title></title>
  </head>
  <body>
    <form id="uploadForm"  >
      文件:<input id="file" type="file" name="file" />
      <input type="submit" value="提交 submit" />
    </form>
   
  </body>
  
    $(function () {
 
      var putUrl = "http://192.168.0.69:9000/my-datas/BaiduIMInstaller-02.zip?response-content-type=application%2Fjson&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=7L9xvYEQ0YBO8UH6dk16%2F20240506%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240506T081513Z&X-Amz-Expires=1200&X-Amz-SignedHeaders=host&X-Amz-Signature=4030eba0b2c2d03263c6f6721bd3b8fbfa85fa51bbd926d41d7bdcba40687e0c";
      //监听表单提交,变为异步提交表单
      $("#uploadForm").on("submit", function (event) {
        var form = this; //this代表的就是当前提交表单的DOM对象
        //用H5的FormData对象对表单数据进行构造
 
        console.log($('#file'))
        console.log($('#file')[0].files[0])
 
        $.ajax({
          url: putUrl,
          type: "put",
          // data:  $('#uploadForm')[0][2].files[0], //直接将文件对象传输
          data:  $('#file')[0].files[0], //直接将文件对象传输
          // dataType: "JSON",
          async: true,
          //要想用jquery的ajax来提交FormData数据,
          //则必须要把这两项设为false
          processData: false,
          contentType: false,
          error: function (xhr, status, error) {
            alert("请求出错!"+error);
          },
          success: function (result) {
            alert("表单提交成功!");
          },
        });
        //阻止表单的提交事件
        return false;
      });
     
    });
  
</html>

全部评论: 0

    我有话说: