Transfer s3 to CloudFront from iOS / Android app

We have apps for iOS and Android that download files for s3. Since we have customers all over the world, we would like to switch to CloudFront.

I understand that for this I need to build a url from / and send NSURLRequest. This means that I no longer use the dedicated S3GetObjectRequest to get the object from the bucket.

Interestingly, is this the only way to do this, or is there a way to work with s3 classes with a cloud platform?

this is my code (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; } 

Android:

 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 ... { ... } } } 
+4
source share

All Articles