0

I want to upload video files to S3 buckets from iPhone using iOS SDK. i need help. I did image uploading on to Amazon S3, and it's working fine, but when I rewrite this code for uploading video it's something wrong.

iosDev
  • 11
  • 5

2 Answers2

0

Maybe you should paste some code here. Also, there are two versions of AWS SDK, are you using the latest version, or the deprecated version?

Hao Xi
  • 351
  • 4
  • 12
0

Please take a look on amazon documentation it will help you to start with basics including,

  1. Setup the SDK (setup SDK using frameworks will be an easy way for starters, Details will be available in this link)

  2. Getting Cognito client initialization code(For AWS authentication of your app)

  3. Create and Configure an S3 Bucket

After completing above steps you can easily upload file to S3. Implement below code on your project,

#import <AWSS3/AWSS3.h>
#import <AWSCore/AWSCore.h>
#import <AWSCognito/AWSCognito.h>

- (void)viewDidLoad
{
    [super viewDidLoad];
/* Below three lines are called Cognito client initialization code please change the regiontype and indentityPoolId with yours */  

    AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSWest2 identityPoolId:@"us-west-2:73ab7333-bqw1-4a8e-b220-9f085cff50yo"];

    AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2 credentialsProvider:credentialsProvider];

    [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];  
    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
    mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];  
    mediaUI.allowsEditing = YES;  
    mediaUI.delegate = self;  

    [self presentViewController:mediaUI animated:YES completion:nil];  


}

- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];  

    if ([type isEqualToString:(NSString *)kUTTypeVideo] ||
        [type isEqualToString:(NSString *)kUTTypeMovie]) { // movie != video  

        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        [self amazonS3Upload:videoURL];
     }
}

- (void)amazonS3Upload:(NSURL *) uploadUrl
{
    // amazon web service s3 api
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.bucket = @"myTest-bucket"; // Your Bucket Name
    uploadRequest.key = @"myTestFile.mp4"; // Your File Name in Bucket
    uploadRequest.body = uploadUrl;
    uploadRequest.uploadProgress =  ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){
        dispatch_async(dispatch_get_main_queue(), ^{
            //Update progress.
            NSLog(@"UPLOAD PROGRESS: %lld : %lld : %lld", bytesSent,totalBytesSent,totalBytesExpectedToSend);
        });
    };

    [[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
                withBlock:^id(AWSTask *task) {
                                                           if (task.error) {
                                                               if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
                                                                   switch (task.error.code) {
                                                                       case AWSS3TransferManagerErrorCancelled:
                                                                       case AWSS3TransferManagerErrorPaused:
                                                                           break;

                                                                       default:
                                                                           NSLog(@"Error: %@", task.error);
                                                                           break;
                                                                   }
                                                               } else {
                                                                   // Unknown error.
                                                                   NSLog(@"Error: %@", task.error);
                                                               }
                                                           }

                                                           if (task.result) {
                                                               AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
                                                               NSLog(@"upload response: %@", uploadOutput);
                                                               // The file uploaded successfully.
                                                           }
                                                           return nil;
                                                       }];

}
arunjos007
  • 4,105
  • 1
  • 28
  • 43