Uploading to Amazon-S3 via AFNetworking

2019-02-15 13:59发布

问题:

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:

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;