ill_always_be_a_warriors
ill_always_be_a_warriors

Reputation: 1566

Image download from AWS S3 taking too long using the AWSiOSSDK.framework

It's taking ~7.5 seconds to download a 700 KB image file from AWS S3.

I wrote this method to download an image from AWS S3:

+ (UIImage *)downloadImageFromPath:(NSString *)path
{
    NSDate *methodStart = [NSDate date];

    S3GetObjectRequest *getObjectRequest = [[S3GetObjectRequest alloc] initWithKey:path withBucket:AWS_BUCKET];

    AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];

    S3GetObjectResponse *response;
    @try
    {
        response = [s3 getObject:getObjectRequest];
    }
    @catch(NSException* ex)
    {
        return [UIImage imageNamed:@"blank.jpg"];
    }

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

    NSDate *methodFinish = [NSDate date];
    NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
    NSLog(@"executionTime = %f", executionTime);

    return image;
}

I just added code at the beginning and end to time the method.

As I mentioned above, it's taking ~7.5 seconds to complete.

I'm running it in the background with GCD with priority DISPATCH_QUEUE_PRIORITY_HIGH, and there's nothing else in the queue when it runs.

Anyone know why it's taking so long? Would be happy to provide more info.

Upvotes: 4

Views: 5577

Answers (1)

MCKapur
MCKapur

Reputation: 9157

This is a known problem, S3 alone is slow when it comes to the upload and download of images.

Well, it actually isn't a problem when you enable CloudFront and use other AWS services.

What you are looking for is the Cloudfront CDN from Amazon. You can specify an origin (that would be your S3 bucket in your case) and then your files will be distributed to all the Amazon Cloudfront edge locations worldwide. Check out their FAQ and the list of edge locations.

You would upload to a central server, where majority of your users would be (estimation). And download from a signed URL in which directs you to a server closest to the user that contains a copy of your bucket... You can also cache the images on the servers to make uploading and downloading faster.

Also, I am not sure how you determine path (the key), but if you are listing keys from S3 buckets, stop that! Its slow! You should use DynamoDB or SimpleDB, both alternatives to storing small datasets, DynamoDB is super fast but doesn't really have query options, you can store all keys into DynamoDB with a certain format and match them.

You could use SimpleDB and store all your keys with relevant attributes (properties) and query SimpleDB using SELECT expressions similar to SQL -- but a slightly different syntax

There are a lot of things you can do to optimize S3!

If you need detailed steps on everything to do, I will do it if you put a bounty on the question. Its not easy picking this up from the documentation but could take me hours to write as well, and I have school!

After following all these steps, I reduced ~8 seconds to almost the snap of a thumb!

Upvotes: 7

Related Questions