Skip to content

Instantly share code, notes, and snippets.

@nimran
Created March 22, 2017 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nimran/008fac7f9a9a6c88f166e2108465ac39 to your computer and use it in GitHub Desktop.
Save nimran/008fac7f9a9a6c88f166e2108465ac39 to your computer and use it in GitHub Desktop.
Amazon S3 Integration iOS
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
{
NSString *S3AccessKey = @"";
NSString *S3SecretKey = @"";
// Initialize based on static credential provider
AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:S3AccessKey secretKey:S3SecretKey];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2 credentialsProvider:credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
return YES;
}
/*
* Method to upload the file
* @localPath - image path
* @fileName - Name of the file
*/
-(void) uploadFile : (NSString*) localPath withFileName:(NSString*) fileName{
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.contentType = @"image/png";
uploadRequest.body = [NSURL fileURLWithPath:localPath];
uploadRequest.key = fileName;
uploadRequest.bucket = @"your-bucket name";
[uploadRequest setUploadProgress:^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){
CGFloat progressDone = ((CGFloat)totalBytesSent)/((CGFloat)totalBytesExpectedToSend);
dispatch_async(dispatch_get_main_queue(),
^{
NSLog(@"Progress %f",progressDone);
});
}];
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[indicator startAnimating];
[[transferManager upload:uploadRequest] continueWithBlock:^id(AWSTask *task)
{
if (task.result)
{
__block NSString *url = [NSString stringWithFormat:@"%@",uploadRequest.key];
NSLog(@"%@",url);
[self presignedUrl : fileName];
[indicator stopAnimating];
dispatch_async(dispatch_get_main_queue(), ^{
});
} else if (task.error) {
[indicator stopAnimating];
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain])
{
switch (task.error.code)
{
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:break;
default:NSLog(@"Upload failed: [%@]", task.error);break;
}
}
else
{
NSLog(@"Upload failed: [%@]", task.error);
}
}
return nil;
}];
}
/*
* Method to generate presigned url to store the image path
* in DB or download the file
*/
-(void) presignedUrl :(NSString*) image {
AWSS3GetPreSignedURLRequest *getPreSignedURLRequest = [AWSS3GetPreSignedURLRequest new];
getPreSignedURLRequest.bucket = @"your-bucket";
getPreSignedURLRequest.key = image;
getPreSignedURLRequest.HTTPMethod = AWSHTTPMethodGET;
getPreSignedURLRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600]; //please change validity as per your
[[[AWSS3PreSignedURLBuilder defaultS3PreSignedURLBuilder] getPreSignedURL:getPreSignedURLRequest]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(@"Error: %@",task.error);
} else {
NSURL *presignedURL = task.result;
NSLog(@"download presignedURL is: \n%@", presignedURL);
}
return nil;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment