aws-sdk v3 访问 minio 报 SignatureDoesNotMatch 错误

注意这个问题在新版本的SDK中应该修复了

参考信息:

参考代码

import { Sha256 } from "@aws-crypto/sha256-browser"
import { S3Client } from "@aws-sdk/client-s3"
import { SignatureV4 } from "@aws-sdk/signature-v4"
import type { HttpRequest } from "@aws-sdk/types"

function getS3Clinet() {
    console.log(import.meta.env)
    return new S3Client({
        region: import.meta.env.VITE_S3_REGION,
        endpoint: import.meta.env.VITE_S3_ENDPOINT,
        forcePathStyle: true,
        signer: async () => ({
            sign: async (request: HttpRequest) => {
                request.headers["host"] = `${request.hostname}:${request.port}`
                const signatureV4 = new SignatureV4({
                    credentials: {
                        accessKeyId: import.meta.env.VITE_S3_ACCESS_KEY_ID,
                        secretAccessKey: import.meta.env.VITE_S3_SECRET_ACCESS_KEY,
                    },
                    region: import.meta.env.VITE_S3_REGION,
                    service: "s3",
                    sha256: Sha256,
                })
                const authorizatedRequest = await signatureV4.sign(request)
                return authorizatedRequest
            },
        }),
    })
}

Leave a Comment

Back to Top