使用IOS 6分段上传到Amazon S3

时间:2013-04-03 14:49:35

标签: ios ios6 amazon-s3

尝试将大于5 MB的文件上传到Amazon S3

http://aws.amazon.com/articles/0006282245644577,此处声明

// The S3UploadInputStream was deprecated after the release of iOS6

5 MB以下的文件我可以通过wifi轻松上传:

NSData *dataForAamzon = [[NSFileManager defaultManager] contentsAtPath:pathForFiile];
@try {
        NSString *uploadName= @"somestring";


        S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uploadName
                                                                 inBucket:[Constants userEventBucket]];
        por.contentType = nil;
        por.data        = dataForAamzon;



        // Put the image data into the specified s3 bucket and object.
        S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];

        [self performSelector:@selector(uploadAllFilesToAmazon:error:) withObject:nil withObject:putObjectResponse.error];

    }
    @catch ( AmazonServiceException *exception ) {
        NSLog( @"Upload Failed, Reason: %@", exception );
    }

由于S3UploadInputStream *stream已被弃用,如何检查文件是否大于5mb并在IOS 6或更高版本中使用分段上传。

谢谢,

2 个答案:

答案 0 :(得分:0)

我希望你需要检查一个本地文件的大小超过5MB吧?

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:_client.filename error:nil];
if(fileAttributes)
{
    NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
    long totalFileSize = [fileSizeNumber longLongValue];
    if(totalFileSize > 5*1024*1024)
    // Go for multipart upload
    else 
    // Go for Direct Put request
}

或者从NSData中,您可以获取长度并执行相同的计算。

答案 1 :(得分:-1)

AWS SDK的新版本解决了我的问题。

新的AWS SDK自动处理文件大小您需要创建trasnfer manager变量并将其委托给s3对象

类似

-(void) uploadAgendatoAmazon
{
    //pass string in memory to nsdictionary
    NSData * data = [_uploadPlistString dataUsingEncoding:NSUTF8StringEncoding];

    NSString *uploadName= [NSString stringWithFormat:@"%@/%@/agenda.plist",[[NSUserDefaults standardUserDefaults] valueForKey:@"email"],self.textEventName.text];

    self.s3.maxRetries = 10;
    self.s3.timeout = 60;
    self.tm = [S3TransferManager new];
    self.tm.s3 = self.s3;
    self.tm.operationQueue.maxConcurrentOperationCount = 2;

   S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uploadName
                                                                 inBucket:[Constants userEventBucket]];
    por.contentType = nil;
    por.data        = data;
    por.delegate = self;
    por.requestTag = @"uploadAgendatoAmazon";
    // Put the image data into the specified s3 bucket and object.
    [self.tm upload:por];
}

这是博客文章的全部细节; http://mobile.awsblog.com/post/TxIRFEQTW9XU8G/S3TransferManager-for-iOS-Part-I-Asynchronous-Uploads