从iOS / Android应用迁移s3下载到CloudFront

我们有iOS和Android应用程序下载s3的文件。 由于我们拥有全球客户,所以我们希望迁移到CloudFront。

我明白,为了做到这一点,我需要从/构build一个URL并发送NSURLRequest。 这意味着我不再使用专用的S3GetObjectRequest从桶中获取对象。

我想知道这是否是唯一的方法,或者有一种方法可以使用cloudFront与s3课程一起工作?

这是我的代码(iOS):

- (int) request:(NSString*)request response:(NSData**)response { NSInteger httpStatus = 0; NSData* theData = NULL; NSString* cloudFrontDomain = [self cloudFrontDomain]; if(cloudFrontDomain){ NSString *urlString = [cloudFrontDomain stringByAppendingPathComponent: request]; NSURL *url = [[NSURL alloc] initWithString:urlString]; NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]; NSError *error = nil; NSHTTPURLResponse *httpResponse = nil; theData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&httpResponse error:&error]; httpStatus = [httpResponse statusCode]; } else{ NSString* bucket = [self s3BucketName]; S3GetObjectRequest* s3Request = [[S3GetObjectRequest alloc] initWithKey: request withBucket:bucket]; S3GetObjectResponse* s3Response = [s3Service getObject:s3Request]; theData = [s3Response body]; httpStatus = [s3Response httpStatusCode]; [s3Request release]; } if (httpStatus == 200) { *response = [[NSData alloc] initWithData:theData]; } return httpStatus; } 

安卓:

 byte[] request(String key) { String cloudFrontDomain = getCloudFrontDomain(); if (cloudFrontDomain != null){ try { String url = cloudFrontDomain + "/" + key; URL myFileURL = new URL(url); InputStream response = myFileURL.openStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(response, outputStream); return outputStream.toByteArray(); } catch ... { .... } } else{ String bucket = getS3BucketName(); GetObjectRequest request = new GetObjectRequest(bucket, key); try { S3Object response = s3Service.getObject(request); if (null != response){ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); IOUtils.copy(response.getObjectContent(), outputStream); return outputStream.toByteArray(); } else { return null; } }catch ... { ... } } }