我们有iOS和Android应用程序可下载s3的文件。由于我们拥有全球客户,因此我们希望迁移到CloudFront。
据我所知,为了做到这一点,我需要从/构建一个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 ... {
...
}
}
}