MichaelQuigley
MichaelQuigley

Reputation: 21

accessing data from s3 using xcode

I am not quite sure how to download data from S3 with XCode. Any help on how to do this would be greatly appreciated. I have tried the following code to access an image from S3,

AmazonCredentials *cred = [[Amazon alloc] initWithAccessKey:accessKey withSecretKey:secretAccessKey];
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithCredentials:cred];
S3GetObjectRequest *s3Request = [[S3GetObjectRequest alloc] initWithKey:urlPath      withBucket:bucket];
s3Request.delegate = self;
S3GetObjectResponse *s3Response = [s3 getObject:s3Request];

NSData*data = s3Response.body;
image = [UIImage imageWithData:data];

When I run the program, I get the exception "EXC_BAD_ACCESS (code = 2, address = 0x0)." I am also unsure about what exactly to include in the bucket name. Should the bucket string be just "nameOfBucket"? or something like "topLevelFolder/nameOfBucket"? Also, what specifically should be included in the "urlPath"? I think that my exception probably has to do with incorrect bucket and urlPath names.

EDIT: I found that we weren't getting any data from S3 and the solution was to remove the line that said "s3Request.delegate = self;".

Upvotes: 2

Views: 1555

Answers (2)

DavidB
DavidB

Reputation: 61

Assigning a delegate s3Request.delegate = self; causes the AmazonS3Client to send messages to the delegate methods (AmazonServiceRequestDelegate) and preempts messages to S3GetObjectResponse

The two most common delegate methods are:

-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error

Upvotes: 0

nerowolfe
nerowolfe

Reputation: 4817

Here are two helper methods for getting image and checking if image exists

#import <AWSiOSSDK/S3/AmazonS3Client.h>

+(NSData *)getImage:(NSString *)imageID inFolder:(NSString *)folderName
{
// Initial the S3 Client.
//folderName = bucket name
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
s3.timeout = 1000;

@try {


    NSString *pictName = [NSString stringWithFormat:@"%@%@", imageID, @".jpg"];

    S3GetObjectRequest *porr = [[S3GetObjectRequest alloc] initWithKey:pictName withBucket:folderName];
    // Get the image data from the specified s3 bucket and object.
    S3GetObjectResponse *response = [s3 getObject:porr]; 

    NSData *imageData = response.body;

    return imageData;

}
@catch (AmazonClientException *exception) {
    return nil;
}
}



+(BOOL)isImageExists:(NSString *)imageID inFolder:(NSString *)folderName
{
@try {

    AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
    NSString *pictName = [NSString stringWithFormat:@"%@%@", imageID, @".jpg"];
    NSLog(@"Start checking a full size image %@", imageID);

    S3GetObjectMetadataRequest *porr = [[S3GetObjectMetadataRequest alloc] initWithKey:pictName withBucket:folderName];

    S3GetObjectResponse *response = [s3 getObjectMetadata:porr];

    if(response)
    {
        return YES;
    }
}
@catch (AmazonServiceException *ex) {
    NSLog(@"AmazonServiceException in isImageExists %@", ex.description);
    return NO;
}
@catch (NSException *exception) {
    NSLog(@"NSException in isImageExists %@", exception.description);
    return NO;
}

return NO;
}

Upvotes: 2

Related Questions