简体   繁体   English

如何使用AWS iOS SDK向aws clouds发出请求?

[英]How can I make a request to aws cloudsearch using the AWS iOS SDK?

I have a client that runs their search functionality on their website through cloudsearch. 我有一个客户端通过cloudsearch在他们的网站上运行他们的搜索功能。 I have been going through the documentation for days, and haven't been able to make a successful search request. 我已经阅读了几天的文档,并且无法成功搜索请求。 I created an NSMutableRequest object, and am running that request through the AWSSignature method [signature interceptRequest:request]; 我创建了一个NSMutableRequest对象,并通过AWSSignature方法[signature interceptRequest:request]运行该请求; but my task.result is coming back (null). 但我的task.result回来了(null)。

Here is my code: 这是我的代码:

AWSTask *task = [signature interceptRequest:request];

[task continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) {
    NSLog(@"task.result fromSearch:%@", task.result);
    NSData *responseData = task.result;
    NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"newStr:%@", newStr);
    NSLog(@"task.error:%@", task.error);
    return nil;
}];

Am I on the right track, or is there a better way to do this through the aws iOS sdk? 我是在正确的轨道上,还是有更好的方法通过aws iOS sdk做到这一点?

To put a little more flesh on the bones of Robert's comment, I did it with some help from AFNetworking like so: 为了给罗伯特评论的骨子多一点点,我在AFNetworking的帮助下做到了这样:

#import <AWSCore/AWSSignature.h>
#import <AWSCore/AWSService.h>
#import <AWSCore/AWSCategory.h>
#import <AWSCore/AWSCredentialsProvider.h>
#import <AWSCore/AWSTask.h>
#import "AFNetworking.h"

- (void)viewDidLoad {
    [super viewDidLoad];
    self.queue = [[NSOperationQueue alloc] init];
}

- (void)performSearch {    
    AWSAnonymousCredentialsProvider* credentialsProvider = [[AWSAnonymousCredentialsProvider alloc] init];
    NSString* searchHost = @"<CloudSearchEndPoint>.eu-west-1.cloudsearch.amazonaws.com";
    NSString* query = [self.searchTerms aws_stringWithURLEncoding];
    NSURL* searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@/2013-01-01/search?q=%@", searchHost, query]];

    AWSEndpoint* endpoint = [[AWSEndpoint alloc] initWithURL:searchURL];
    AWSSignatureV4Signer* signer = [[AWSSignatureV4Signer alloc] initWithCredentialsProvider:credentialsProvider endpoint:endpoint];
    NSMutableURLRequest* mutableRequest = [[NSMutableURLRequest alloc] initWithURL:searchURL];
    AWSTask* task = [signer interceptRequest:mutableRequest];

    [task continueWithBlock:^id(AWSTask* _Nonnull t) {
        if (t.error) {
            NSLog(@"Error: %@", t.error);
        } else if (t.completed) {
            NSLog(@"Result is %@", t.result);
        }               

        AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:mutableRequest success:^(NSURLRequest* request, NSHTTPURLResponse* response, id JSON) {
            NSLog(@"Success fetching results!");
            if (JSON) {
                NSDictionary* hitsContainer = [JSON objectForKey:@"hits"];
                NSArray* hits = [hitsContainer objectForKey:@"hit"];
                NSMutableArray* allResults = [[NSMutableArray alloc] initWithCapacity:hits.count];
                for (NSDictionary* hit in hits) {
                    NSDictionary* fields = [hit objectForKey:@"fields"];
                    [allResults addObject:fields];
                }
                self.searchResults = allResults;
                [self.tableView reloadData];
            }
        }

        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            NSLog(@"Failure fetching search results :-( %@", error);
        }
     ];

    [self.queue addOperation:operation];

    return nil;
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 swift:如何在aws-api-gateway的生成的iOS SDK中的PUT请求中设置主体? - swift: How can I set the body in the PUT request in the generated iOS sdk for aws-api-gateway? 使用AWS开发工具包iOS登录AWS控制台 - Signing In to AWS console using AWS SDK iOS AWS开发工具包iOS-如何确定上载请求的完成百分比 - AWS SDK iOS - how to determine the percentage completion of upload request 如何允许访客 Amplify 用户使用 AWS Amplify iOS 开发工具包从我的 S3 存储桶下载文件? - How can I allow guest Amplify users to download files from my S3 bucket using the AWS Amplify iOS SDK? 为什么我无法捕获此iOS AWS SDK网络错误? - Why can't I catch this iOS AWS SDK network error? 使用 aws-sdk-ios 设置 aws-lex 请求属性 - setting aws-lex request attributes with aws-sdk-ios 如何使用适用于javascript的aws-sdk将图像从iOS应用程序(使用Kony构建)上载到AWS S3? - How do I upload an image from iOS app (built with Kony) to AWS S3 using aws-sdk for javascript? 如何检查适用于iOS的AWS开发工具包SDK的版本? - How do I check the version of the AWS SDK for iOS framework? 如何使用iOS SDK在运行时更改AWS的区域? - How to change the region of AWS at runtime using iOS sdk? 使用自定义cname上传aws ios sdk - Using custom cname for uploads with aws ios sdk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM