我想使用iOS SDK将视频文件从iPhone上传到S3存储桶?

时间:2016-09-26 06:44:45

标签: ios video amazon-s3 upload sdk

我想使用iOS SDK将视频文件从iPhone上传到S3存储桶。 我需要帮助。我把图像上传到了亚马逊S3上,而且工作正常,但是当我重写这段代码来上传视频时,这是错误的。

2 个答案:

答案 0 :(得分:0)

也许你应该在这里粘贴一些代码。此外,AWS SDK有两个版本,您使用的是最新版本还是已弃用的版本?

答案 1 :(得分:0)

请查看amazon documentation它将帮助您从基础开始,包括

  1. 设置SDK(使用框架设置SDK将是一种简单的方法 首发,详细信息将在此link

  2. 中提供
  3. 获取Cognito客户端初始化代码(用于AWS身份验证) 你的应用程序)

  4. 创建和配置S3存储桶

  5. 完成上述步骤后,您可以轻松地将文件上传到S3。在您的项目上实现以下代码,

    #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;
                                                           }];
    
    }