适用于IOS的AWS SDK从v1到v2关于" S3objectsummary"

时间:2015-01-22 15:53:45

标签: ios amazon-s3 aws-sdk

这是progressbar的功能。在sdk v1我可以使用“S3Objectsummary”来了解该文件的摘要,但在sdk v2我找不到“S3Objectsummary”。

哪一个是v2中类似的?如果有人能展示一个很棒的例子。

另外,我和

有同样的问题
S3GetObjectRequest/S3GetObjectResponse/S3PutObjectRequest/AmazonClientException

代码位于sdk ios v1

-(AmazonS3Client *)s3{
[self validateCredentials];
return s3;}

-(void)validateCredentials{
NSLog(@"validating credentials.");
if (s3 == nil) {
    [self clearCredentials];
    s3  = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
}
}

-(void)setProgressBar{
[delegate setProgressStatus:progPercent];
}

-(void)downloadPlists{
@try {
    NSArray *Plists = [[self s3] listObjectsInBucket:@"~~~~"];

    float numfile = 1;
    float totalfiles = [Plists count];

    for (S3ObjectSummary *file in Plists) {
        float percent = numfile/totalfiles;
        progPercent = [NSNumber numberWithFloat:percent];
        [self performSelectorOnMainThread:@selector(setProgressBar) withObject:progPercent waitUntilDone:YES];
        numfile++;
        NSString *key = [file key];
        NSLog(@"key: %@", key);
        if ([key rangeOfString:@".plist"].location != NSNotFound) {
            NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

            NSString *plistFilePath = [NSString stringWithFormat:@"%@/Plists/%@",docDir, key];
            NSLog(@"plistFilePath: %@", plistFilePath);

            S3GetObjectRequest *plist = [[S3GetObjectRequest alloc] initWithKey:key withBucket:@"~~~~~"];
            S3GetObjectResponse *getObjectResponse = [[self s3] getObject:plist];

            NSData *data2 = [NSData dataWithData: getObjectResponse.body];

            NSString *courseFilePath = [plistFilePath substringToIndex:[plistFilePath rangeOfString:@"/" options:NSBackwardsSearch].location];
            bool testDirectoryCreated = [[NSFileManager defaultManager]createDirectoryAtPath: courseFilePath
                                                                 withIntermediateDirectories: YES 
                                                                                  attributes: nil 
                                                                                       error: NULL];
            if (!testDirectoryCreated)
                NSLog(@"error creating test directory.");

            if (![data2 writeToFile:plistFilePath atomically:YES])
                NSLog(@"error writing to path.");
        }
    }
}
@catch (NSException *exception) {
    UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle:@"Oops!" message:[NSString stringWithFormat: @"There was an error performing this operation. Please try again later. Error: %@", exception] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil];
    [failureAlert show];
}

}

我尝试在代码中的v2中执行相同的操作,如下所示,代码是否正确?

-(void)downloadPlists

{

    AWSS3 *s3 = [AWSS3 defaultS3];

    AWSS3ListObjectsRequest *listObjectReq=[AWSS3ListObjectsRequest new];
    listObjectReq.bucket=@"PLists";

    [[[s3 listObjects:listObjectReq] continueWithBlock:^id(BFTask *task) {
        if(task.error){
            NSLog(@"the request failed. error %@",task.error);
        }
        if(task.result){
            AWSS3ListObjectsOutput *listObjectsOutput=task.result;
            NSArray *Plists = task.result;    //Is the result of task in listObjectOutput a NSArray?
            float numfile = 1;
            float totalfiles = [Plists count];
            for(AWSS3Object *file in listObjectsOutput.contents){
                float percent = numfile/totalfiles;
                progPercent = [NSNumber numberWithFloat:percent];
                [self performSelectorOnMainThread:@selector(setProgressBar) withObject:progPercent waitUntilDone:YES];
                numfile++;
                NSString *key = [file key];
                NSLog(@"key: %@", key);
                if ([key rangeOfString:@".plist"].location != NSNotFound) {
                    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

                    NSString *plistFilePath = [NSString stringWithFormat:@"%@/Plists/%@",docDir, key];
                    NSLog(@"plistFilePath: %@", plistFilePath);


                    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
                    AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
                    downloadRequest.bucket = @"PLists";
                    downloadRequest.key = key;
                    //downloadRequest.downloadingFileURL=[NSURL fileURLWithPath: @"???"];  I'm not sure the path.  In sdk V1 there is no URL ?

                    [[transferManager download: downloadRequest] continueWithBlock:^id(BFTask *task) {
                        if (task.error) {

                            UIAlertView *failureAlert = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                                                                   message:[NSString stringWithFormat: @"There was an error performing this operation. Please try again later. Error: %@", task.error]
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"Okay"
                                                                         otherButtonTitles: nil];
                            [failureAlert show];
                        }

                        if (task.result) {
                            AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;

                            NSData *data2 = [NSData dataWithData: downloadOutput.body];

                            NSString *courseFilePath = [plistFilePath substringToIndex:[plistFilePath rangeOfString:@"/" options:NSBackwardsSearch].location];
                            bool testDirectoryCreated = [[NSFileManager defaultManager]createDirectoryAtPath: courseFilePath
                                                                                 withIntermediateDirectories: YES
                                                                                                  attributes: nil
                                                                                                       error: NULL];
                            if (!testDirectoryCreated)
                                NSLog(@"error creating test directory.");

                            if (![data2 writeToFile:plistFilePath atomically:YES])
                                NSLog(@"error writing to path.");

                        }
                        return nil;
                    }];

            }

        }
            return nil;
        }

        return nil;
    }] waitUntilFinished];   //In the integration test still use the "waitUntilFinisher".But in the "Working with BFTask" said the continueWithBolck won't execute until the previous asychronous call has already finished exceuting?

}

1 个答案:

答案 0 :(得分:0)

v2中的

AWSS3Object相当于v1中的S3ObjectSummary

您没有调用- listObjects:,因此您的v2代码段不起作用。您应该以integration test为例进行说明。请注意,您应该避免在生产应用中调用- waitUntilFinished。有关详细信息,请参阅Working with BFTask