亚马逊s3中的分段上传不适用于ios

时间:2013-05-27 12:11:38

标签: ios objective-c xcode amazon-web-services

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

-(void)multipartUpload:(NSData*)imageData inBucket:(NSString*)bucket forKey:(NSString*)key
{
    NSLog(@"Coming");
    bool using3G = ![self isWifiAvailable];

    AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
    @try {
        NSLog(@"Coming111");
        [s3 createBucketWithName:bucket];

        S3InitiateMultipartUploadRequest *initReq = [[S3InitiateMultipartUploadRequest alloc] initWithKey:key inBucket:bucket];
        S3MultipartUpload *upload = [s3 initiateMultipartUpload:initReq].multipartUpload;
        S3CompleteMultipartUploadRequest *compReq = [[S3CompleteMultipartUploadRequest alloc] initWithMultipartUpload:upload];

        int numberOfParts = [self countParts:imageData];
        for ( int part = 0; part < numberOfParts; part++ ) {
            NSData *dataForPart = [self getPart:part fromData:imageData];

            // The S3UploadInputStream was deprecated after the release of iOS6.
            S3UploadInputStream *stream = [S3UploadInputStream inputStreamWithData:dataForPart];
            if ( using3G ) {
                // If connected via 3G "throttle" the stream.
                stream.delay = 0.2; // In seconds
                stream.packetSize = 16; // Number of 1K blocks
            }

            S3UploadPartRequest *upReq = [[S3UploadPartRequest alloc] initWithMultipartUpload:upload];
            upReq.partNumber = ( part + 1 );
            upReq.contentLength = [dataForPart length];
            upReq.stream = stream;

            S3UploadPartResponse *response = [s3 uploadPart:upReq];
            [compReq addPartWithPartNumber:( part + 1 ) withETag:response.etag];
        }

        [s3 completeMultipartUpload:compReq];
    }
    @catch ( AmazonServiceException *exception ) {
        NSLog( @"Multipart Upload Failed, Reason: %@", exception  );
    }
}

-(NSData*)getPart:(int)part fromData:(NSData*)fullData
{
    NSRange range;
    range.length = PART_SIZE;
    range.location = part * PART_SIZE;

    int maxByte = (part + 1) * PART_SIZE;
    if ( [fullData length] < maxByte ) {
        range.length = [fullData length] - range.location;
    }

    return [fullData subdataWithRange:range];
}

-(int)countParts:(NSData*)fullData
{
    int q = (int)([fullData length] / PART_SIZE);
    int r = (int)([fullData length] % PART_SIZE);

    return ( r == 0 ) ? q : q + 1;
}

不上传视频。请帮帮我。

0 个答案:

没有答案