簡體   English   中英

如何使用AWS iOS SDK向aws clouds發出請求?

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

我有一個客戶端通過cloudsearch在他們的網站上運行他們的搜索功能。 我已經閱讀了幾天的文檔,並且無法成功搜索請求。 我創建了一個NSMutableRequest對象,並通過AWSSignature方法[signature interceptRequest:request]運行該請求; 但我的task.result回來了(null)。

這是我的代碼:

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;
}];

我是在正確的軌道上,還是有更好的方法通過aws iOS sdk做到這一點?

為了給羅伯特評論的骨子多一點點,我在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM