minio通过预签名方式上传,下载,查看

获取一个指定了 HTTP 方法、到期时间和自定义请求参数的对象URL地址,也就是返回带签名的URL,这个地址可以提供给没有登录的第三方共享访问或者上传对象。

后端:

 /**
     * 获得上传的签名URL
     * @param fileName 文件名
     * @param durationSeconds 签名有效期秒
     * @return
     */
    public static MinioPresignedObjectUrl getPresignedObjectUrl2upload(String fileName, Integer durationSeconds) {
        initMinio(minioUrl, minioName,minioPass);
        MinioPresignedObjectUrl minioUrl = new MinioPresignedObjectUrl();
        String objectName = fileName2objectName("",fileName);
        String  url =
                null;
        try {

            url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.PUT)
                            .bucket(bucketName)
                            .object(objectName)
                            .expiry(durationSeconds, TimeUnit.SECONDS)
                            .build());
            minioUrl.setPresignedObjectUrl(url);
            minioUrl.setPresignedObjectUrl(minioUrlDomain+bucketName+"/"+objectName);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return minioUrl;
    }

    /**
     * 获得下载的URL
     * @param fileName 文件名
     * @param durationSeconds 签名有效期秒
     * @return
     */
    public static MinioPresignedObjectUrl getPresignedObjectUrl2download(String fileName, Integer durationSeconds) {
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put("response-content-type", "application/json");
        MinioPresignedObjectUrl minioUrl = new MinioPresignedObjectUrl();
        String objectName = fileName2objectName("",fileName);
        String  url = null;
        try {
            url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.GET)
                            .bucket(bucketName)
                            .object(objectName)
                            .expiry(durationSeconds, TimeUnit.SECONDS)
                            .extraQueryParams(reqParams)
                            .build());
            minioUrl.setPresignedObjectUrl(url);
            minioUrl.setPresignedObjectUrl(minioUrlDomain+bucketName+"/"+objectName);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return minioUrl;
    }

    /**
     * 获取查看文件元数据url
     * @param fileName 文件名
     * @param durationSeconds 签名有效期秒
     * @return
     */
    public static MinioPresignedObjectUrl getPresignedObjectUrl2metadata(String fileName, Integer durationSeconds) {
        Map<String, String> reqParams = new HashMap<String, String>();
        reqParams.put("response-content-type", "application/json");
        MinioPresignedObjectUrl minioUrl = new MinioPresignedObjectUrl();
        String objectName = fileName2objectName("",fileName);
        String  url =
                null;
        try {

            url = minioClient.getPresignedObjectUrl(
                    GetPresignedObjectUrlArgs.builder()
                            .method(Method.HEAD)
                            .bucket(bucketName)
                            .object(objectName)
                            .expiry(durationSeconds, TimeUnit.SECONDS)
                            .extraQueryParams(reqParams)
                            .build());
            minioUrl.setPresignedObjectUrl(url);
            minioUrl.setPresignedObjectUrl(minioUrlDomain+bucketName+"/"+objectName);
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return minioUrl;
    }

前端:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="file" id="selector" multiple>
<button onclick="upload()">Upload</button>

<div id="status">No uploads</div>
<script type="text/javascript">
    // `upload` iterates through all files selected and invokes a helper function called `retrieveNewURL`.
    function upload() {
        // Get selected files from the input element.
        var files = document.querySelector("#selector").files;
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            // 从服务器获取一个URL
            retrieveNewURL(file, (file, url) => {
                // 上传文件到服务器
                uploadFile(file, url);
            });
        }
    }
    // 发请求到Node.js server获取上传URL。
    // `retrieveNewURL` accepts the name of the current file and invokes the `/presignedUrl` endpoint to
    // generate a pre-signed URL for use in uploading that file:
    function retrieveNewURL(file, cb) {
        cb(file,"http://zzdust.oss.jinhukeji.net:9000/test/test.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=hinmuminio%2F20220801%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220801T082305Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=98fca4ad9f5212d9e0eae9729350d866a6a333676aa1fc7547692ce453b75a17")

    }
    // 使用Fetch API来上传文件到S3。
    // ``uploadFile` accepts the current filename and the pre-signed URL. It then uses `Fetch API`
    // to upload this file to S3 at `play.min.io:9000` using the URL:
    function uploadFile(file, url) {
        if (document.querySelector('#status').innerText === 'No uploads') {
            document.querySelector('#status').innerHTML = '';
        }
        fetch(url, {
            method: 'PUT',
            body: file
        }).then(() => {
            // If multiple files are uploaded, append upload status on the next line.
            document.querySelector('#status').innerHTML += `<br>Uploaded ${file.name}.`;
        }).catch((e) => {
            console.error(e);
        });
    }
</script>
</body>
</html>

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青苔猿猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值