Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minio403问题 #1432

Closed
water2060 opened this issue Apr 12, 2024 · 2 comments
Closed

minio403问题 #1432

water2060 opened this issue Apr 12, 2024 · 2 comments
Labels
question Further information is requested

Comments

@water2060
Copy link

我能获取到具体的参数
uri:http://localhost:9000/user-profile-picture
x-amz-date:20240412T060645Z
x-amz-signature:545fcd84eac9447574832351c5b0266ad403d18135aa2412eed1dca83c27630a
x-amz-algorithm:AWS4-HMAC-SHA256
x-amz-credential:minioadmin/20240412/us-east-1/s3/aws4_request
policy:eyJleHBpcmF0aW9uIjoiMjAyNC0wNC0xMlQwNjoxMTo0NS4yMTNaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkYnVja2V0IiwidXNlci1wcm9maWxlLXBpY3R1cmUiXSxbImVxIiwiJGtleSIsIjEiXSxbInN0YXJ0cy13aXRoIiwiJENvbnRlbnQtVHlwZSIsImltYWdlLyJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3Nl0sWyJlcSIsIiR4LWFtei1hbGdvcml0aG0iLCJBV1M0LUhNQUMtU0hBMjU2Il0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwibWluaW9hZG1pbi8yMDI0MDQxMi91cy1lYXN0LTEvczMvYXdzNF9yZXF1ZXN0Il0sWyJlcSIsIiR4LWFtei1kYXRlIiwiMjAyNDA0MTJUMDYwNjQ1WiJdXX0=
key:1

但是使用StorageService里面的upload会提示403。
然而上面的参数,我用下面的代码,是可以上传成功的额,不会报403

public static void main(String[] args) throws IOException, InvalidKeyException, NoSuchAlgorithmException, ErrorResponseException, InternalException, InsufficientDataException {
String endpoint = "http://localhost:9000"; // MinIO服务的URL
String accessKey = "minioadmin"; // MinIO访问密钥
String secretKey = "minioadmin"; // MinIO访问密钥的密钥

    // 创建MinioClient对象
    MinioClient minioClient = MinioClient.builder()
            .endpoint(endpoint)
            .credentials(accessKey, secretKey)
            .build();

    // 上传文件
    try {
        String bucketName = "user-profile-picture";
        String objectName = "1";
        File file = new File("D:\\1.png"); // 读取的文件路径 // 要上传的文件内容,这里是示例内容

        // 生成POST Policy
        String policy = generatePostPolicy(bucketName, objectName);

        // 计算签名
        String signature = generateSignature(secretKey, policy);
        
        // 上传文件
        Map<String, String> headers = new HashMap<>();
        headers.put("x-amz-algorithm", "AWS4-HMAC-SHA256");
        headers.put("x-amz-credential", "minioadmin/20240412/us-east-1/s3/aws4_request");
        headers.put("x-amz-date", "20240412T050414Z");
        headers.put("policy", "eyJleHBpcmF0aW9uIjoiMjAyNC0wNC0xMlQwNTowOToxNC4xODZaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkYnVja2V0IiwidXNlci1wcm9maWxlLXBpY3R1cmUiXSxbImVxIiwiJGtleSIsIjEiXSxbInN0YXJ0cy13aXRoIiwiJENvbnRlbnQtVHlwZSIsImltYWdlLyJdLFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3Nl0sWyJlcSIsIiR4LWFtei1hbGdvcml0aG0iLCJBV1M0LUhNQUMtU0hBMjU2Il0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwibWluaW9hZG1pbi8yMDI0MDQxMi91cy1lYXN0LTEvczMvYXdzNF9yZXF1ZXN0Il0sWyJlcSIsIiR4LWFtei1kYXRlIiwiMjAyNDA0MTJUMDUwNDE0WiJdXX0=");
        headers.put("x-amz-signature", "8a27fb50dd530860bb8d188f7d8b9a8c015c623c8360bebece073e2f9dfa0420");
        headers.put("key", "1");
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        minioClient.putObject(io.minio.PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .headers(headers)
                .contentType("application/octet-stream")
                .stream(new FileInputStream(file), file.length(), -1)
                .build());

        System.out.println("File uploaded successfully.");
    } catch (MinioException e) {
        e.printStackTrace();
    }
}

报403的提示如下:
code: 91, reason: Failed to retrieve the resource because the HTTP response status code is: 403
ResponseException(requestId=null, code=91, reason=Failed to retrieve the resource because the HTTP response status code is: 403, cause=null)
at im.turms.client.exception.ResponseException$Companion.from(ResponseException.kt:70)
at im.turms.client.exception.ResponseException$Companion.from$default(ResponseException.kt:68)
at im.turms.client.service.StorageService$upload$2$1.onResponse(StorageService.kt:880)
at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1583)

@JamesChenX JamesChenX added the status: confirming Confirming whether the issue is a bug or can be a new feature label Apr 12, 2024
@JamesChenX
Copy link
Member

I just updated turms-plugin-minio days ago. You can download and use the latest turms-plugin-minio to have a try first.

And the clients should always pass the non-null mediaType value when calling uplodateXXX methods in storageService so that it can pass the Content-Type header, or you will get the 403 error.

If you have followed the steps mentioned above and still encounter 403, it is suggested you can set a break point on the HTTP response so that you can view the HTTP response body as a string, and the body will tell you the exact reason why the request fails.

@water2060
Copy link
Author

我明白了。kotlin里面需要
image
这样写就ok了

@JamesChenX JamesChenX added question Further information is requested and removed status: confirming Confirming whether the issue is a bug or can be a new feature labels Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants