Uploading to Amazon-S3 via AFNetworking

2019-02-15 13:56发布

I'm having difficulty uploading files to S3 via AFNetworking. When I attempt to upload, I get callbacks indicating a small percentage of the file is uploaded, then the callbacks cease, and one or two minutes later I get a timeout message.

I'm using AFAmazonS3Client but it seems like a pretty straight forward subclass of AFHTTPClient so it seems unlikely the problem lies there. Since I'm new to both AFNetworking and S3, I'm pretty sure the problem is typing this message :)

I've downloaded the AWS-IOS SDK and was able to post a picture from their sample code without a problem. So I don't think its a misconfiguration of my S3 account.

1条回答
劳资没心,怎么记你
2楼-- · 2019-02-15 14:25

After struggling a bit with the same problem and a bit of research, I found a solution that seems to work well.

Download the Amazon AWS SDK for iOS and add it to your project:

http://aws.amazon.com/sdkforios/

Then, implement the upload test with a custom NSOperation class as explained here:

Using delegates, operations, and queues

The AmazonServiceRequestDelegate protocol is the key to get progress updates, in a similar fashion to what AFNetworking does.

In particular, this method is what I needed:

-(void)request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten
    totalBytesWritten:(NSInteger)totalBytesWritten
    totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;
查看更多
登录 后发表回答