我在尝试在Amazon S3服务器上下载图片时遇到了问题。
我收到以下错误:
Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: binary/octet-stream"
有人有想法吗?
答案 0 :(得分:15)
此错误由
生成- (BOOL)validateResponse:(NSHTTPURLResponse *)response
data:(NSData *)data
error:(NSError * __autoreleasing *)error
AFHTTPResponseSerializer的方法,如果出现无法接受的MIME类型的响应。
您可以通过向响应序列化程序添加必需的MIME类型来修复它
// In this sample self is inherited from AFHTTPSessionManager
self.responseSerializer = [AFImageResponseSerializer serializer];
NSSet *set = self.responseSerializer.acceptableContentTypes;
self.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"binary/octet-stream"];
或者您可以修改AFImageResponseSerializer:
- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", @"binary/octet-stream", nil];
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
self.imageScale = [[UIScreen mainScreen] scale];
self.automaticallyInflatesResponseImage = YES;
#endif
return self;
}
但问题的根源可能是您使用错误的MIME类型或根本没有类型将图像保存到Amazon。在我的代码中,我使用以下代码将图像保存到亚马逊
S3PutObjectRequest *putObjectRequest = [ [ S3PutObjectRequest alloc ] initWithKey:keyImage inBucket:self.s3BucketName ];
putObjectRequest.contentType = @"image/jpeg";
putObjectRequest.data = UIImageJPEGRepresentation( [ image fixOrientation ], 0.5f );
putObjectRequest.cannedACL = [ S3CannedACL publicRead ];
答案 1 :(得分:0)
更新了Boto 3
您可以通过S3 Web界面查看它的MIME类型。首先选择您遇到问题的文件,然后选择“属性”视图。在此视图中打开“元数据”部分。
如果您的Content-Type是二进制/八位字节流,则取消设置。在Boto 3中设置它与Boto 2的上述答案不同。这是我的工作方式:
filename = "/home/me/image42.jpeg" #the file I want to upload
bucketname = "myNewBucket" #name of my S3 bucket
key = "myImages/image42.jpeg" #desired name in S3 bucket
s3.Object(bucketname, key).put(Body=open(filename, 'rb'), ACL='public-read',ContentType='image/jpeg')
答案 2 :(得分:0)
AFNetworking 3.1.0
您应该只修改现有图像响应序列化程序上的acceptableContentTypes
属性。您可以使用AFHTTPSessionManager
来执行此操作。
NSURL *url = [NSURL URLWithString:@"http://..."];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
serializer.acceptableContentTypes = [NSSet setWithObject:@"binary/octet-stream"];
manager.responseSerializer = serializer;
然后,您可以使用manager
获取网址内容:
[manager GET:[url absoluteString] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// ...
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// ...
}];
或使用setImageWithURLRequest:placeholderImage:success:^failure:^
获取图片:
UIImageView *imageView = [UIImageView new];
[[UIImageView sharedImageDownloader] setSessionManager:manager];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://..."]];
[imageView setImageWithURLRequest:urlRequest placeholderImage:[UIImage new] success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
// but image can be NSData instead of UIImage
} failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
// ...
}];
但响应可以是NSData而不是UIImage。
我希望它对你有所帮助。