Showing
5 changed files
with
56 additions
and
12 deletions
| @@ -391,7 +391,6 @@ static NSError *ASITooMuchRedirectionError; | @@ -391,7 +391,6 @@ static NSError *ASITooMuchRedirectionError; | ||
| 391 | CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)header, (CFStringRef)[requestHeaders objectForKey:header]); | 391 | CFHTTPMessageSetHeaderFieldValue(request, (CFStringRef)header, (CFStringRef)[requestHeaders objectForKey:header]); |
| 392 | } | 392 | } |
| 393 | 393 | ||
| 394 | - | ||
| 395 | // If this is a post request and we have data in memory send, add it to the request | 394 | // If this is a post request and we have data in memory send, add it to the request |
| 396 | if ([self postBody]) { | 395 | if ([self postBody]) { |
| 397 | CFHTTPMessageSetBody(request, (CFDataRef)postBody); | 396 | CFHTTPMessageSetBody(request, (CFDataRef)postBody); |
| @@ -15,7 +15,7 @@ extern NSString *const ASIS3AccessPolicyPrivate; // This is the default in S3 wh | @@ -15,7 +15,7 @@ extern NSString *const ASIS3AccessPolicyPrivate; // This is the default in S3 wh | ||
| 15 | extern NSString *const ASIS3AccessPolicyPublicRead; | 15 | extern NSString *const ASIS3AccessPolicyPublicRead; |
| 16 | extern NSString *const ASIS3AccessPolicyPublicReadWrote; | 16 | extern NSString *const ASIS3AccessPolicyPublicReadWrote; |
| 17 | extern NSString *const ASIS3AccessPolicyAuthenticatedRead; | 17 | extern NSString *const ASIS3AccessPolicyAuthenticatedRead; |
| 18 | -bug report in lighthouse | 18 | + |
| 19 | @interface ASIS3Request : ASIHTTPRequest { | 19 | @interface ASIS3Request : ASIHTTPRequest { |
| 20 | 20 | ||
| 21 | // Your S3 access key. Set it on the request, or set it globally using [ASIS3Request setSharedAccessKey:] | 21 | // Your S3 access key. Set it on the request, or set it globally using [ASIS3Request setSharedAccessKey:] |
| @@ -37,7 +37,7 @@ static NSString *sharedSecretAccessKey = nil; | @@ -37,7 +37,7 @@ static NSString *sharedSecretAccessKey = nil; | ||
| 37 | 37 | ||
| 38 | + (id)requestWithBucket:(NSString *)bucket path:(NSString *)path | 38 | + (id)requestWithBucket:(NSString *)bucket path:(NSString *)path |
| 39 | { | 39 | { |
| 40 | - ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/%@",bucket,path]]] autorelease]; | 40 | + ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://s3.amazonaws.com/%@/%@",bucket,path]]] autorelease]; |
| 41 | [request setBucket:bucket]; | 41 | [request setBucket:bucket]; |
| 42 | [request setPath:path]; | 42 | [request setPath:path]; |
| 43 | return request; | 43 | return request; |
| @@ -49,14 +49,14 @@ static NSString *sharedSecretAccessKey = nil; | @@ -49,14 +49,14 @@ static NSString *sharedSecretAccessKey = nil; | ||
| 49 | [request setPostBodyFilePath:filePath]; | 49 | [request setPostBodyFilePath:filePath]; |
| 50 | [request setShouldStreamPostDataFromDisk:YES]; | 50 | [request setShouldStreamPostDataFromDisk:YES]; |
| 51 | [request setRequestMethod:@"PUT"]; | 51 | [request setRequestMethod:@"PUT"]; |
| 52 | - [request setMimeType:[ASIS3Request mimeTypeForFileAtPath:path]]; | 52 | + [request setMimeType:[ASIS3Request mimeTypeForFileAtPath:filePath]]; |
| 53 | return request; | 53 | return request; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | 56 | ||
| 57 | + (id)listRequestWithBucket:(NSString *)bucket prefix:(NSString *)prefix maxResults:(int)maxResults marker:(NSString *)marker | 57 | + (id)listRequestWithBucket:(NSString *)bucket prefix:(NSString *)prefix maxResults:(int)maxResults marker:(NSString *)marker |
| 58 | { | 58 | { |
| 59 | - ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/?prefix=/%@&max-keys=%hi&marker=%@",bucket,prefix,maxResults,marker]]] autorelease]; | 59 | + ASIS3Request *request = [[[ASIS3Request alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://s3.amazonaws.com/%@?prefix=/%@&max-keys=%hi&marker=%@",bucket,prefix,maxResults,marker]]] autorelease]; |
| 60 | [request setBucket:bucket]; | 60 | [request setBucket:bucket]; |
| 61 | return request; | 61 | return request; |
| 62 | } | 62 | } |
| @@ -74,12 +74,17 @@ static NSString *sharedSecretAccessKey = nil; | @@ -74,12 +74,17 @@ static NSString *sharedSecretAccessKey = nil; | ||
| 74 | [task setLaunchPath: @"/usr/bin/file"]; | 74 | [task setLaunchPath: @"/usr/bin/file"]; |
| 75 | [task setArguments:[NSMutableArray arrayWithObjects:@"-Ib",path,nil]]; | 75 | [task setArguments:[NSMutableArray arrayWithObjects:@"-Ib",path,nil]]; |
| 76 | 76 | ||
| 77 | - NSPipe *pipe = [NSPipe pipe]; | 77 | + NSPipe *outputPipe = [NSPipe pipe]; |
| 78 | - [task setStandardOutput:pipe]; | 78 | + [task setStandardOutput:outputPipe]; |
| 79 | 79 | ||
| 80 | - NSFileHandle *file = [pipe fileHandleForReading]; | 80 | + NSFileHandle *file = [outputPipe fileHandleForReading]; |
| 81 | 81 | ||
| 82 | [task launch]; | 82 | [task launch]; |
| 83 | + [task waitUntilExit]; | ||
| 84 | + | ||
| 85 | + if ([task terminationStatus] != 0) { | ||
| 86 | + return @"application/octet-stream"; | ||
| 87 | + } | ||
| 83 | 88 | ||
| 84 | NSString *mimeTypeString = [[[NSString alloc] initWithData:[file readDataToEndOfFile] encoding: NSUTF8StringEncoding] autorelease]; | 89 | NSString *mimeTypeString = [[[NSString alloc] initWithData:[file readDataToEndOfFile] encoding: NSUTF8StringEncoding] autorelease]; |
| 85 | return [[mimeTypeString componentsSeparatedByString:@";"] objectAtIndex:0]; | 90 | return [[mimeTypeString componentsSeparatedByString:@";"] objectAtIndex:0]; |
| @@ -89,7 +94,7 @@ static NSString *sharedSecretAccessKey = nil; | @@ -89,7 +94,7 @@ static NSString *sharedSecretAccessKey = nil; | ||
| 89 | - (void)setDate:(NSDate *)date | 94 | - (void)setDate:(NSDate *)date |
| 90 | { | 95 | { |
| 91 | NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; | 96 | NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; |
| 92 | - [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzzz"]; | 97 | + [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"]; |
| 93 | [self setDateString:[dateFormatter stringFromDate:date]]; | 98 | [self setDateString:[dateFormatter stringFromDate:date]]; |
| 94 | } | 99 | } |
| 95 | 100 | ||
| @@ -135,10 +140,10 @@ static NSString *sharedSecretAccessKey = nil; | @@ -135,10 +140,10 @@ static NSString *sharedSecretAccessKey = nil; | ||
| 135 | [self addRequestHeader:@"Authorization" value:authorizationString]; | 140 | [self addRequestHeader:@"Authorization" value:authorizationString]; |
| 136 | } | 141 | } |
| 137 | 142 | ||
| 138 | -- (void)startRequest | 143 | +- (void)main |
| 139 | { | 144 | { |
| 140 | [self generateS3Headers]; | 145 | [self generateS3Headers]; |
| 141 | - [super startRequest]; | 146 | + [super main]; |
| 142 | } | 147 | } |
| 143 | 148 | ||
| 144 | #pragma mark Shared access keys | 149 | #pragma mark Shared access keys |
| @@ -79,10 +79,49 @@ | @@ -79,10 +79,49 @@ | ||
| 79 | [request generateS3Headers]; | 79 | [request generateS3Headers]; |
| 80 | success = [[[request requestHeaders] valueForKey:@"Authorization"] isEqualToString:@"AWS 0PN5J17HBGZHT7JJ3X82:dxhSBHoI6eVSPcXJqEghlUzZMnY="]; | 80 | success = [[[request requestHeaders] valueForKey:@"Authorization"] isEqualToString:@"AWS 0PN5J17HBGZHT7JJ3X82:dxhSBHoI6eVSPcXJqEghlUzZMnY="]; |
| 81 | GHAssertTrue(success,@"Failed to generate the correct authorisation header for a list request"); | 81 | GHAssertTrue(success,@"Failed to generate the correct authorisation header for a list request"); |
| 82 | - | ||
| 83 | } | 82 | } |
| 84 | 83 | ||
| 84 | +// To run this test, uncomment and fill in your S3 access details | ||
| 85 | +/* | ||
| 86 | +- (void)testREST | ||
| 87 | +{ | ||
| 88 | + NSString *secretAccessKey = @"my-secret-key"; | ||
| 89 | + NSString *accessKey = @"my-access-key"; | ||
| 90 | + NSString *bucket = @"bucket-name"; | ||
| 91 | + NSString *path = @"path/to/file"; | ||
| 92 | + | ||
| 93 | + // Create the fle | ||
| 94 | + NSString *text = @"This is my content"; | ||
| 95 | + NSString *filePath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"testfile.txt"]; | ||
| 96 | + [[text dataUsingEncoding:NSUTF8StringEncoding] writeToFile:filePath atomically:NO]; | ||
| 97 | + | ||
| 98 | + // PUT the file | ||
| 99 | + ASIS3Request *request = [ASIS3Request PUTRequestForFile:filePath withBucket:bucket path:path]; | ||
| 100 | + [request setRequestMethod:@"PUT"]; | ||
| 101 | + [request setSecretAccessKey:secretAccessKey]; | ||
| 102 | + [request setAccessKey:accessKey]; | ||
| 103 | + [request start]; | ||
| 104 | + BOOL success = [[request responseString] isEqualToString:@""]; | ||
| 105 | + GHAssertTrue(success,@"Failed to PUT a file to S3"); | ||
| 85 | 106 | ||
| 107 | + // GET the file | ||
| 108 | + request = [ASIS3Request requestWithBucket:bucket path:path]; | ||
| 109 | + [request setSecretAccessKey:secretAccessKey]; | ||
| 110 | + [request setAccessKey:accessKey]; | ||
| 111 | + [request start]; | ||
| 112 | + success = [[request responseString] isEqualToString:@"This is my content"]; | ||
| 113 | + GHAssertTrue(success,@"Failed to GET the correct data from S3"); | ||
| 114 | + | ||
| 115 | + // DELETE the file | ||
| 116 | + request = [ASIS3Request requestWithBucket:bucket path:path]; | ||
| 117 | + [request setSecretAccessKey:secretAccessKey]; | ||
| 118 | + [request setRequestMethod:@"DELETE"]; | ||
| 119 | + [request setAccessKey:accessKey]; | ||
| 120 | + [request start]; | ||
| 121 | + success = [[request responseString] isEqualToString:@""]; | ||
| 122 | + GHAssertTrue(success,@"Failed to DELETE the file from S3"); | ||
| 123 | +} | ||
| 124 | +*/ | ||
| 86 | 125 | ||
| 87 | 126 | ||
| 88 | @end | 127 | @end |
-
Please register or login to post a comment