文件不存在时,AWS S3 HEAD请求未返回404 [英] AWS S3 HEAD request not returning 404 when file doesn't not exist

查看:305
本文介绍了文件不存在时,AWS S3 HEAD请求未返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码检查S3上是否存在文件.不幸的是,我永远不会出错.看来,AWS开发工具包应该返回一个错误,但这不是因为该主体为空并且无法在S3错误中反序列化.

I'm using the following code to check if a file exists on S3. Unfortunately, I'm never getting an error. It seems to be that the AWS SDK should return an error but that it doesn't because the body is empty and cannot be deserialized in a S3 error.

如何解决此问题?一种解决方案是检查响应中的404,但我找不到使用task进行此操作的方法.

How can I work around this issue? A solution would be to check myself for the 404 in the response but I don't find a way to do that with the task.

s3.headObject(headRequest).continue({ task in
            task.
            if let taskError = task.error {
                if let error = task.error as NSError? {
                    if error.domain == AWSS3ErrorDomain && AWSS3ErrorType(rawValue: error.code) == AWSS3ErrorType.noSuchKey {
                        return successBlock(false)
                    }
                }
                errorBlock(taskError)
                return nil
            }
            return successBlock(true)
        });

(如果错误是403,也会出现相同的问题)

(The same issue arises if the error is a 403)

推荐答案

这里的问题是您将永远不会收到Body,因为HEAD请求被设计为不返回一个Body.

The issue here is that you will never receive a Body because a HEAD request is designed not to return one.

响应HEAD请求方法(第4.3.2节) [RFC7231]的内容)从不包含消息正文,因为 响应标头字段(例如,传输编码,内容长度, 等)(如果存在),则仅指示如果 请求方法为GET([RFC7231]的4.3.1节).

Responses to the HEAD request method (Section 4.3.2 of [RFC7231]) never include a message body because the associated response header fields (e.g., Transfer-Encoding, Content-Length, etc.), if present, indicate only what their values would have been if the request method had been GET (Section 4.3.1 of [RFC7231]).

但是,您仍然可以获得HTTP状态代码,该代码应指示发生的任何错误的状态.

However, you can still get an HTTP status code, which should indicate the state of any error which occurred.

这篇关于文件不存在时,AWS S3 HEAD请求未返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆