Permalink
Browse files

initial

  • Loading branch information...
1 parent be6bef4 commit 0ab83e65c96ac64b50fc980f5bf7299e4cc0b4e9 @0i0 committed Jul 28, 2013
Showing with 129 additions and 1 deletion.
  1. +26 −0 CLUploadService.h
  2. +98 −0 CLUploadService.m
  3. +5 −1 README.md
View
@@ -0,0 +1,26 @@
+//
+// CLUploadService.h
+// iOSClishade
+//
+// Created by 0i0 on 7/25/13.
+// Copyright (c) 2013 0i0. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "AmazonS3Client.h"
+
+typedef void(^ProgressCallback)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite);
+typedef void(^SuccessCallback)(id responseObject);
+typedef void(^FailureCallback)(NSError *error);
+
+@interface CLUploadService : NSOperation<AmazonServiceRequestDelegate> {
+@private BOOL _doneUploadingToS3;
+}
+
+-(void)uploadFileWithData:(NSData *)data
+ fileName:(NSString *)fileName
+ progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
+ success:(void (^)(id responseObject))success
+ failure:(void (^)(NSError *error))failure;
+
+@end
View
@@ -0,0 +1,98 @@
+//
+// CLUploadService.m
+//
+// Created by 0i0 on 7/25/13.
+// Copyright (c) 2013 0i0. All rights reserved.
+//
+
+#import "CLUploadService.h"
+#define kAWSAccessKeyID @"YOUR_KEY_GOES_HERE"
+#define kAWSsecret @"YOUR_SECRET_GOES_HERE"
+#define kAWSBucket @"YOUR_Bucket_GOES_HERE"
+#define kAWSPath @"YOUR_PATH_GOES_HERE"
+
+@implementation CLUploadService{
+ ProgressCallback progress;
+ SuccessCallback success;
+ FailureCallback failure;
+}
+
+-(void)uploadFileWithData:(NSData *)data
+ fileName:(NSString *)fileName
+ progress:(void (^)(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progressCallback
+ success:(void (^)(id responseObject))successCallback
+ failure:(void (^)(NSError *error))failureCallback
+{
+
+ progress = progressCallback;
+ success = successCallback;
+ failure = failureCallback;
+
+ _doneUploadingToS3 = NO;
+
+
+ AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:kAWSAccessKeyID withSecretKey:kAWSsecret];
+ dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
+ dispatch_async(queue, ^{
+
+ NSString *pathToFile = [NSString stringWithFormat:@"%@%@",kAWSPath,fileName];
+ // Upload image data. Remember to set the content type.
+ S3PutObjectRequest *por = nil;
+ por = [[S3PutObjectRequest alloc] initWithKey:pathToFile inBucket:kAWSBucket];
+ por.cannedACL = [S3CannedACL publicRead];
+ @try {
+
+ por.delegate = self;
+ por.contentType = @"image/jpeg";
+ por.data = data;
+
+ [s3Client putObject:por];
+ }
+ @catch (AmazonClientException *exception) {
+ _doneUploadingToS3 = YES;
+ }
+
+ do {
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
+ } while (!_doneUploadingToS3);
+
+ por.delegate = nil;
+ });
+
+}
+
+-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
+{
+ _doneUploadingToS3 = YES;
+ success(response);
+}
+
+-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error
+{
+ _doneUploadingToS3 = YES;
+ failure(error);
+}
+
+-(void)request:(AmazonServiceRequest *)request didFailWithServiceException:(NSException *)exception
+{
+ _doneUploadingToS3 = YES;
+ failure(nil);
+}
+
+- (void) request:(AmazonServiceRequest *)request didSendData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
+{
+ // Do what you want
+ progress(bytesWritten,totalBytesWritten,totalBytesExpectedToWrite);
+ //NSLog(@"%f%% Uploaded", (totalBytesWritten / (totalBytesExpectedToWrite * 1.0f) * 100));
+}
+
+-(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response
+{
+ // Do what you want
+}
+
+-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data
+{
+ // Do what you want
+}
+@end
View
@@ -1,4 +1,8 @@
iOS-Amazon-S3-upload-service
============================
-an class that manages a wrapper around amazon ios sdk with callbacks and progress
+A class that wraps amazon ios sdk with callbacks and progress
+
+The amzon sdk is not icluded here, I assume you have it
+
+there is no mime type detection, you'll need an external lib for this

0 comments on commit 0ab83e6

Please sign in to comment.