我正在使用亚马逊提供的示例制作应用程序以将视频上传到服务器(S3)。该示例生成一个存储桶并在那里存储图像。在我的情况下,我使用以前生成的存储桶来存储视频,但如果我擦除存储桶生成代码,视频上传就会停止。 生成的存储桶不用于存储视频。此外,如果存储桶生成引发异常(例如,因为存储桶已存在),文件上载仍然有效。
我认为在发送文件之前我还需要进行另一次初始化。 有什么想法吗?
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.s3 = nil;
aws_bucketName = @"bucketname"; // this is not the real name
aws_key = @"**** HIDDEN ********";
aws_secretKey = @"**** HIDDEN ********";
if(self.s3 == nil)
{
// Initial the S3 Client.
self.s3 = [[AmazonS3Client alloc] initWithAccessKey:aws_key withSecretKey:aws_secretKey];
self.s3.endpoint = [AmazonEndpoints s3Endpoint:US_EAST_1]; // the bucket region is 'US Standard'
//*******************************//
// @try
// {
// // Create the picture bucket.
// NSLog(@"Start with bucket generation");
// S3CreateBucketRequest *createBucketRequest = [[S3CreateBucketRequest alloc] initWithName:aws_bucketName andRegion:[S3Region USStandard]];
//
// NSLog(@"Buquet requested, now response [bucket: %@]", aws_bucketName);
// S3CreateBucketResponse *createBucketResponse = [self.s3 createBucket:createBucketRequest];
//
// NSLog(@"Bucket generated");
// if(createBucketResponse.error != nil)
// {
// NSLog(@"Error: %@", createBucketResponse.error);
// }
// }
//
// @catch (NSException *exception)
// {
// NSLog(@"There was an exception when connecting to s3: %@",exception.description);
// }
//**********************************//
}
}
- (IBAction)uploadFileButtonTouchUp:(id)sender
{
NSLog(@"Upload started");
// Get file path
NSString* fileName = @"upTest.mp4";
NSString *fileDirectory = [[NSBundle mainBundle] resourcePath];
NSString *finalFilePath = [fileDirectory stringByAppendingFormat:@"/%@", fileName ];
NSLog(@"The file to be uploaded is: %@", finalFilePath);
// Call file upload
NSURL *videoURL = [NSURL fileURLWithPath:finalFilePath];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
[self uploadWithData2:videoData videoName:@"iosUploadTest.mp4" bucketName:aws_bucketName];
}
//*************************************************//
//********* File upload **********//
//*************************************************//
- (void)uploadWithData2:(NSData *)data videoName:(NSString *)videoName bucketName:(NSString*)bucketName
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Upload image data. Remember to set the content type.
S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:videoName
inBucket:bucketName];
por.contentType = @"movie/mov";
por.data = data;
// Put the image data into the specified s3 bucket and object.
S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];
dispatch_async(dispatch_get_main_queue(), ^{
if(putObjectResponse.error != nil)
{
NSLog(@"Error: %@", putObjectResponse.error);
}
else
{
NSLog(@"Video uploaded succesfuly");
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
});
});
}
对存储桶生成代码进行了注释。
答案 0 :(得分:2)
尝试将内容类型设置为video/mp4
,以减轻MIME类型可能出现的无法预料的问题。
我还建议reading this,它包含一些不同的上传方法,并像你一样处理线程。
答案 1 :(得分:0)
您能否更具体地了解您遇到的错误?您似乎正在处理存储区初始化中的异常,但不会在您的上传代码中处理异常。 SDK中是否启用了例外?您可以阅读this blog post以在AWS SDK for iOS中启用/禁用例外。
如果事实上启用了异常,则需要在上传代码中添加@try { } @catch ()
块,这样可以捕获生成的异常。
您还可以在SDK中启用详细日志记录,以使用[AmazonLogger verboseLogging]
查看对服务的原始请求。包括帖子中的任何错误将更好地帮助您进一步调试问题。