2

我正在尝试使用 Amazon S3 SDK 将大文件部分上传到 S3 服务器。我有以下代码:

-(void)multipartUpload:(NSData*)dataToUpload inBucket:(NSString*)bucket forKey:(NSString*)key
{
    //bool using3G = ![self isWifiAvailable];

    AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:kS3ApplicationKey withSecretKey:kS3ApplicationSecret];

    s3.timeout = 99999999;

    @try {
        //[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:dataToUpload];


        for ( int part = 0; part < numberOfParts; part++ ) {
            NSLog(@"%d", part);

            NSData *dataForPart = [self getPart:part fromData:dataToUpload];

            // The S3UploadInputStream was deprecated after the release of iOS6.
            NSInputStream *stream = [NSInputStream inputStreamWithData:dataForPart];

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

            S3UploadPartResponse *response = [s3 uploadPart:upReq]; // <----- null returned here :(
            [compReq addPartWithPartNumber:( part + 1 ) withETag:response.etag];
        }

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

问题是 uploadPart 方法返回 null 而不是实际的服务器响应,这会导致下一行出现异常。

我已经实现了所有的委托方法来检查错误,但没有任何报告。

代码从这里复制: http: //aws.amazon.com/articles/0006282245644577

任何想法为什么它不起作用?

4

1 回答 1

3

uploadPart方法返回nil,因为您设置delegate了对象的属性S3UploadPartRequest。您需要response.etagrequest:didCompleteWithResponse:委托方法中检索,或者在没有委托的情况下使用同步调用。AWS 开发人员博客解释了如何正确使用:http AmazonServiceRequestDelegate: //mobile.awsblog.com/post/Tx2XVU3IRV1HBOJ/Using-the-AWS-SDK-for-iOS-Asynchronously-Part-I-Sync-vs-Async

于 2013-01-10T03:54:50.037 回答