使用MinIO CopyObjectAsync遇AuthorizationException的解决方法
MinIO CopyObjectAsync 签名不匹配问题修复
调用MinIO的CopyObjectAsync方法复制图片时触发Minio.Exceptions.AuthorizationException,错误提示:
我们计算的请求签名与您提供的签名不匹配。请检查您的密钥和签名方法。
其他如添加、删除图片的操作均可正常执行,项目中图片名称采用Guid格式。原实现代码如下:
try { var copySourceObjectArgs = new CopySourceObjectArgs() .WithBucket("src-img.jpg") .WithObject("srcbucket"); var copyObjectArgs = new CopyObjectArgs() .WithBucket("new-img-name.jpg") .WithObject("newbucket") .WithCopyObjectSource(copySourceObjectArgs); await _minIOClient.CopyObjectAsync(copyObjectArgs).ConfigureAwait(false); } catch (Exception exception) { Console.WriteLine(exception.ToString()); }
MinIO客户端初始化代码:
_minIOClient = new MinioClient() .WithEndpoint(EndPoint) .WithCredentials(AccessKey, SecretAccessKey) .WithSSL() .Build();
问题根源
原代码中CopySourceObjectArgs和CopyObjectArgs的WithBucket与WithObject参数完全颠倒:将文件名传给了WithBucket,桶名传给了WithObject,导致请求的资源路径错误,服务器计算签名的依据与客户端不一致,最终触发签名不匹配的异常。
修正后的代码
将WithBucket和WithObject的参数正确对应,WithBucket传入桶名,WithObject传入对象(图片)名称:
try { // 源对象参数:WithBucket传源桶名,WithObject传源图片名称 var copySourceObjectArgs = new CopySourceObjectArgs() .WithBucket("srcbucket") .WithObject("src-img.jpg"); // 目标对象参数:WithBucket传目标桶名,WithObject传目标图片名称 var copyObjectArgs = new CopyObjectArgs() .WithBucket("newbucket") .WithObject("new-img-name.jpg") .WithCopyObjectSource(copySourceObjectArgs); await _minIOClient.CopyObjectAsync(copyObjectArgs).ConfigureAwait(false); } catch (Exception exception) { Console.WriteLine(exception.ToString()); }
补充说明
MinIO的请求签名生成逻辑依赖于请求中的资源路径(桶名+对象名),当参数颠倒后,客户端生成签名时使用的路径是src-img.jpg/srcbucket,而服务器期望的路径是srcbucket/src-img.jpg,两者不一致导致签名校验失败。修正参数对应关系后,签名生成的依据与服务器一致,即可解决该异常。
内容的提问来源于stack exchange,提问作者Vladimir
相关产品推荐
相关产品推荐

