简体   繁体   中英

Multipart Upload to Amazon S3 with IOS 6

Trying to upload files bigger than 5 mb to Amazon S3

http://aws.amazon.com/articles/0006282245644577 , here it is stated that

// The S3UploadInputStream was deprecated after the release of iOS6

Files under 5 mb I can easily upload over wifi with:

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 );
    }

Since S3UploadInputStream *stream was deprecated , how can I check if file is bigger than 5mb and use multipart upload in IOS 6 or later versions.

Thanks,

I hope you need to check the size of a local file exceeds 5MB right ?

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
}

Or from the NSData you can take the Length and perform the same calculation.

New release of AWS SDK solved my problem.

New AWS SDK is handling files size automatically you need to create trasnfer manager variable and delegate it to s3 object

something like

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

Here is the blog post for full details; http://mobile.awsblog.com/post/TxIRFEQTW9XU8G/S3TransferManager-for-iOS-Part-I-Asynchronous-Uploads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM