如何在S3PutObjectRequest完成时添加侦听器?

时间:2012-06-13 10:42:34

标签: objective-c ios amazon-s3 amazon

我正在使用iOS SDK上传到Amazon S3,它运行良好,但我希望能够在加载完成后触发方法。

这是我的代码:

AmazonS3Client *s3 = [[[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY] autorelease];
// Create the picture bucket.
[s3 createBucket:[[[S3CreateBucketRequest alloc] initWithName:[Constants pictureBucket]] autorelease]];
NSString *picName = [NSString stringWithFormat:@"%@%d", PICTURE_NAME, counter];
// Upload image data.  Remember to set the content type.
S3PutObjectRequest *por = [[[S3PutObjectRequest alloc] initWithKey:picName inBucket:[Constants pictureBucket]] autorelease];
NSLog(@"------------ SUBMITTING img :%@", picName);
por.contentType = @"image/jpeg";
por.data        = imageData;
counter++;                   
// Put the image data into the specified s3 bucket and object.
[s3 putObject:por];

非常感谢任何帮助谢谢!

2 个答案:

答案 0 :(得分:1)

Amazon SDK Docs开始,您似乎获得了S3PutObjectResponse

所以

S3PutObjectResponse *response  = [s3 putObject:por];
if ([response isFinishedLoading]) {
    //do something
}

或者您正在搜索connectionDidFinishLoading:,它是来自NSURLConnection的委托方法,它似乎相应地用于AmazonServiceResponse Class Reference

在你.h文件中声明你符合NSURLConnection的委托协议

@interface MyClass : NSObject <NSURLConnectionDelegate>
.m文件中的

实现了你想要的委托方法

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
      //do your stuff here
}

告诉NSURLConnection您处理.m文件中的委托方法

S3PutObjectRequest *por = [[[S3PutObjectRequest alloc] initWithKey:picName inBucket:[Constants pictureBucket]] autorelease];
por.urlRequest.delegate = self; // this is important !!!

一般来说,你应该习惯与代表一起工作,因为他们经常被iOS SDK使用!!

您可以在此处找到其他文档:Delegates and Data Sources

答案 1 :(得分:0)

我还有一件事要添加到评论中(我知道我在这里踩到了行为,但是代表阻止我发表评论)。我运行这两行只是为了安全,因为我发现第一行不能保持其值一致:

    por.delegate = self;
    [por setDelegate:self];

由于你是像我这样的新手,代表本质上是处理程序,当它调用有时需要或不需要的强制方法时对象看起来。如果将委托设置为self,则意味着putObjectRequest将在self调用时引用强制方法,例如Pfitz的答案中的方法。对于UITableView,委托方法的示例是(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,其中对象UITableView将引用self以查找方法cellForRowAtIndexPath以填充其对象的单元队列。