Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from achainan/master
Browse files Browse the repository at this point in the history
Allows user to post/put files to any location.
  • Loading branch information
mattt committed Aug 12, 2012
2 parents 2f96fff + 6ccf773 commit 9d1274b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 2 additions & 0 deletions AFAmazonS3Client.h
Expand Up @@ -121,6 +121,7 @@ extern NSString * const kAFAmazonS3BaseURLString;
Adds an object to a bucket using forms.
*/
- (void)postObjectWithFile:(NSString *)path
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
success:(void (^)(id responseObject))success
Expand All @@ -130,6 +131,7 @@ extern NSString * const kAFAmazonS3BaseURLString;
Adds an object to a bucket for a user that has write access to the bucket. A success response indicates the object was successfully stored; if the object already exists, it will be overwritten.
*/
- (void)putObjectWithFile:(NSString *)path
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
success:(void (^)(id responseObject))success
Expand Down
22 changes: 13 additions & 9 deletions AFAmazonS3Client.m
Expand Up @@ -34,7 +34,8 @@ @interface AFAmazonS3Client ()
@property (readwrite, nonatomic, copy) NSString *secret;

- (void)setObjectWithMethod:(NSString *)method
file:(NSString *)path
file:(NSString *)filePath
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progressBlock
success:(void (^)(id responseObject))success
Expand Down Expand Up @@ -76,7 +77,7 @@ - (NSURL *)baseURL {
if (_s3_baseURL && self.bucket) {
return [NSURL URLWithString:[NSString stringWithFormat:kAFAmazonS3BucketBaseURLFormatString, self.bucket]];
}

return _s3_baseURL;
}

Expand Down Expand Up @@ -197,21 +198,23 @@ - (void)getObjectWithPath:(NSString *)path
}

- (void)postObjectWithFile:(NSString *)path
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
[self setObjectWithMethod:@"POST" file:path parameters:parameters progress:progress success:success failure:failure];
[self setObjectWithMethod:@"POST" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure];
}

- (void)putObjectWithFile:(NSString *)path
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
[self setObjectWithMethod:@"PUT" file:path parameters:parameters progress:progress success:success failure:failure];
[self setObjectWithMethod:@"PUT" file:path destinationPath:destinationPath parameters:parameters progress:progress success:success failure:failure];
}

- (void)deleteObjectWithPath:(NSString *)path
Expand All @@ -222,22 +225,23 @@ - (void)deleteObjectWithPath:(NSString *)path
}

- (void)setObjectWithMethod:(NSString *)method
file:(NSString *)path
parameters:(NSDictionary *)parameters
file:(NSString *)filePath
destinationPath:(NSString *)destinationPath
parameters:(NSDictionary *)parameters
progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];
[fileRequest setCachePolicy:NSURLCacheStorageNotAllowed];

NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:fileRequest returningResponse:&response error:&error];

if (data && response) {
NSMutableURLRequest *request = [self multipartFormRequestWithMethod:method path:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"file" fileName:[path lastPathComponent] mimeType:[response MIMEType]];
NSMutableURLRequest *request = [self multipartFormRequestWithMethod:method path:destinationPath parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:data name:@"file" fileName:[filePath lastPathComponent] mimeType:[response MIMEType]];
}];

AFHTTPRequestOperation *requestOperation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
Expand Down

0 comments on commit 9d1274b

Please sign in to comment.