Search code examples
iosobjective-camazon-web-servicesamazon-s3bolts-framework

Get region of bucket using AWSS3GetBucketLocationRequest


I seem to have trouble understanding how to best deal with the BFTask (Bolts framework) return object for several AWS SDK iOS v2 methods. I am trying to get the name of the region my bucket resides in. Could anyone suggest how to do that given the locationConstraint information I am successfully receiving from the following code? Or is there a generic way to understand what the task.result object contains?

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
AWSS3 *myS3 = [[AWSS3 alloc] initWithConfiguration:self.configurationS3];
AWSS3GetBucketLocationRequest *locReq = [AWSS3GetBucketLocationRequest new];
locReq.bucket=@"testAWS";

[[myS3 getBucketLocation:locReq] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
    if(task.error != nil){
        NSLog(@"%s Location not found: [%@]",__PRETTY_FUNCTION__, task.error);
    } else {
        NSLog(@"Location found: [%@] - %li", task.result, [task.result locationConstraint]);
    }
    return nil;  
}];

Also, if anyone has suggestions for tutorials/examples for best understanding BFTask that would be helpful. Thanks for your help. Cheers, Trond ps. I also asked this question on AWS support site.


Solution

  • Here is the code snippet to determine the bucket location:

    AWSS3 *s3 = [AWSS3 defaultS3];
    AWSS3GetBucketLocationRequest *getBucketLocationRequest = [AWSS3GetBucketLocationRequest new];
    getBucketLocationRequest.bucket = testBucketNameGeneral;
    
    [[s3 getBucketLocation:getBucketLocationRequest] continueWithBlock:^id(BFTask *task) {
        if(task.error != nil){
            XCTAssertNil(task.error, @"The request failed. error: [%@]", task.error);
        }
    
        AWSS3GetBucketLocationOutput *getBucketLocationOutput = task.result;
        XCTAssertEqual(getBucketLocationOutput.locationConstraint, AWSS3BucketLocationConstraintBlank);
        switch (getBucketLocationOutput.locationConstraint) {
            case AWSS3BucketLocationConstraintBlank:
                NSLog(@"Classic Region");
                break;
            case AWSS3BucketLocationConstraintEU:
                NSLog(@"EU");
                break;
            case AWSS3BucketLocationConstraintEUWest1:
                NSLog(@"eu-west-1");
                break;
            case AWSS3BucketLocationConstraintUSWest1:
                NSLog(@"us-west-1");
                break;
            case AWSS3BucketLocationConstraintUSWest2:
                NSLog(@"us-west-2");
                break;
            case AWSS3BucketLocationConstraintAPSoutheast1:
                NSLog(@"ap-southeast-1");
                break;
            case AWSS3BucketLocationConstraintAPSoutheast2:
                NSLog(@"ap-southeast-2");
                break;
            case AWSS3BucketLocationConstraintAPNortheast1:
                NSLog(@"ap-northeast-1");
            case AWSS3BucketLocationConstraintSAEast1:
                NSLog(@"sa-east-1");
                break;
    
            case AWSS3BucketLocationConstraintUnknown:
            default:
                // Error
                break;
        }
        return nil;
    }];
    

    However, there is a bug in the SDK, and getBucketLocationOutput.locationConstraint is always AWSS3BucketLocationConstraintUnknown. We are working on the fix right now.

    The Bolts-iOS GitHub repo has a pretty good instructions on how to use Bolts.

    Hope this helps,


    Update

    The AWS Mobile SDK for iOS 2.0.4 has been released. The update fixes the issue, and now it returns the correct locationConstraint.