从AWS S3太长使用AWSiOSSDK.framework图片下载图片下载、太长、AWS、AWSiOSSDK

2023-09-11 23:46:21 作者:倾尽余生讨你欢

它采取〜7.5秒内从AWS S3下载一个700KB的图像文件。

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

我写了这个方法下载从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;
}

我只是增加code。在开始和结束时间的方法。

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

正如我上面提到的,它采取〜7.5秒内完成。

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

我把它运行在背景GCD优先 DISPATCH_QUEUE_PRIORITY_HIGH ,并没有什么别的队列运行时。

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.

推荐答案

这是一个已知的问题,S3单是慢当涉及到图像的上传和下载。

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

嗯,这其实不是一个问题,当您启用CloudFront的,并使用其他AWS服务。

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

您正在寻找什么是的Cloudfront CDN从亚马逊。你可以指定一个原点(这将是你的情况你的S3存储桶),然后你的文件将被分发给全世界所有亚马逊的Cloudfront边沿位置。看看他们常见问题解答和的边缘位置列表。

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.

您将上传到一个中央服务器,其中大部分用户将是(估计)。而从它指导你到一个服务器最接近包含你的水桶副本的用户标识的URL下载...你也可以缓存在服务器上的图像进行上传和下载速度更快。

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.

另外,我不知道你怎么确定路径(关键),但如果你是从S3桶上市键,停止!它的慢!您应该使用DynamoDB或SimpleDB的,两者的替代品存放小数据集,DynamoDB是超级快,但并没有真正有查询选项,可以将所有钥匙进入DynamoDB存储一定的格式和匹配。

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.

您可以使用SimpleDB的和存储所有密钥使用SELECT EX pressions类似于SQL相关的属性(属性)和查询SimpleDB的 - 但一个稍微不同的语法

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

有很多事情可以做,以优化S3!

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!

在以下所有步骤,我减少了〜8秒拇指几乎瞬间!

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