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>

Minio是一个开源的对象存储服务器,可以将其用作私有云存储解决方案。签名上传链接是一种通过在URL中包含签名信息来允许用户直接上传文件到Minio服务器的方法。 当使用生成的签名上传链接来上传文件时,如果出现"signaturedoesnotmatch"错误,可能由以下几个原因导致: 1. 参数错误:确保你在生成签名URL时使用了正确的参数。这包括桶名称、对象名称、请求方法、过期时间等。 2. 时间戳错误:确认你在生成签名时使用了正确的时间戳。Minio需要验证请求的时间戳,以确保请求没有过期。如果时间戳不正确,可能会导致签名不匹配的错误。 3. 签名算法错误:确保你在生成签名时使用了正确的算法。Minio支持多种签名算法,如AWS Signature Version 2和AWS Signature Version 4。确认你使用了与服务器配置相匹配的签名算法。 4. 密钥错误:验证你在生成签名时使用了正确的访问密钥和密钥ID。只有正确的密钥才能生成有效的签名,否则会导致签名不匹配的错误。 5. 网络问题:在上传文件时,也有可能是由于网络问题导致的错误。在这种情况下,检查你的网络连接,并确保能够正常访问到Minio服务器。 如果以上步骤都没有解决问题,你可以尝试查看Minio服务器的日志,这可能会提供更多的错误信息,帮助你更好地诊断问题所在。如果仍然无法解决问题,建议参考Minio文档或向Minio开发者社区寻求帮助。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

青苔猿猿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值