AWS SDK for Java 1.x 403问题解决方法和原因

问题表现

使用AWS SDK for Java 1.x访问S3,已经确认文件存在,且具有权限,仍然出现403 Forbidden应答。

解决方法

升级到AWS SDK for Java 2.x。

问题原因

AWS签名机制严格依赖请求的精确路径格式,任何URI的差异(如 ///%2F )都会导致签名校验失败。AWS SDK for Java 1.x版本中,当资源路径 resourcePath 以斜杠开头时(如 /foo/... ),与 endpoint 拼接后会产生双斜杠 // 。SDK内部会将其转义为 /%2F ,导致实际请求路径与签名计算的路径不一致,触发 SignatureDoesNotMatch 错误。

关键代码分析

在AWS SDK for Java 1.x版本中,当调用 httpRequestFactory.create(request, options) 方法时,URL的生成过程涉及路径拼接逻辑与双斜杠转义机制,具体流程如下:

复制代码
@Override
   public HttpRequestBase create(final Request<?> request,
                                 final HttpClientSettings settings)
           throws
           FakeIOException {
       URI endpoint = request.getEndpoint();

       String uri;
       // skipAppendUriPath is set for APIs making requests with presigned urls. Otherwise
       // a slash will be appended at the end and the request will fail
       if (request.getOriginalRequest().getRequestClientOptions().isSkipAppendUriPath()) {
           uri = endpoint.toString();
       } else {
           /*
            * HttpClient cannot handle url in pattern of "http://host//path", so we
            * have to escape the double-slash between endpoint and resource-path
            * into "/%2F"
            */
           uri = SdkHttpUtils.appendUri(endpoint.toString(), request.getResourcePath(), true);
       }

       String encodedParams = SdkHttpUtils.encodeParameters(request);

       /*
        * For all non-POST requests, and any POST requests that already have a
        * payload, we put the encoded params directly in the URI, otherwise,
        * we'll put them in the POST request's payload.
        */
       boolean requestHasNoPayload = request.getContent() != null;
       boolean requestIsPost = request.getHttpMethod() == HttpMethodName.POST;
       boolean putParamsInUri = !requestIsPost || requestHasNoPayload;
       if (encodedParams != null && putParamsInUri) {
           uri += "?" + encodedParams;
       }

       final HttpRequestBase base = createApacheRequest(request, uri, encodedParams);
       addHeadersToRequest(base, request);
       addRequestConfig(base, request, settings);

       return base;
   }

假设原始请求参数为

复制代码
Endpoint: http://127.0.0.1/mybucket
ResourcePath: /foo/bar/... (以斜杠开头)

SdkHttpUtils.appendUri()endpointresourcePath 拼接为:

复制代码
http://127.0.0.1/mybucket//foo/bar/...

注意中间的 // 双斜杠。由于第三个参数 escapeDoubleSlash=true ,SDK会将双斜杠转义为 /%2F

复制代码
http://172.24.152.73:80/mybucket/%2Ffoo/bar/...

生成的URI变为转义后的路径,而计算签名时使用的路径是未经转义的原始路径 /mybucket//foo/bar/... ,导致签名不匹配。

相关推荐
wanhengidc16 小时前
云手机能够做些什么?
运维·服务器·人工智能·智能手机·云计算
捷智算云服务19 小时前
混合云新篇章:H100裸金属租赁与云计算的协同效应
云计算
小白考证进阶中20 小时前
自学阿里云认证,能救一个是一个!
阿里云·云计算·阿里云acp·阿里云acp认证·阿里云acp考试·阿里云acp报名·阿里云acp备考
@HNUSTer21 小时前
基于 GEE 平台用 Sentinel-1 SAR 数据实现山区潜在滑坡检测
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
iconball21 小时前
个人用云计算学习笔记 --19 (MariaDB服务器)
linux·运维·笔记·学习·云计算
Serverless 社区1 天前
阿里云函数计算 AgentRun 全新发布,构筑智能体时代的基础设施
人工智能·阿里云·云原生·serverless·云计算
sailwon1 天前
自己搭建远程桌面服务器——私有化部署RustDesk
运维·服务器·云计算·远程工作
iconball2 天前
个人用云计算学习笔记 --20 (Nginx 服务器)
linux·运维·笔记·学习·云计算
iconball2 天前
个人用云计算学习笔记 --18(NFS 服务器、iSCSI 服务器)
linux·运维·笔记·学习·云计算
₯㎕星空&繁华3 天前
阿里云服务器安装MySQL服务器
服务器·ubuntu·阿里云·云计算