Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a basic example using PUT. Obviously you should be using a queue rather than a synchronous request in the real world.</p> <p>If you change the amz headers, don't forget to update 'canonicalizedAmzHeaders' as per Amazon's instructions.</p> <pre><code>#import "ASIHTTPRequest.h" #import &lt;CommonCrypto/CommonHMAC.h&gt; ... - (void)testS3 { NSString *filePath = @"/path/to/file"; NSString *contentType = @"text/plain"; NSString *bucket = @"mybucket"; NSString *path = @"test"; NSString *secretAccessKey = @"my-secret-access-key"; NSString *accessKey = @"my-access-key"; NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzzz"]; NSString *date = [dateFormatter stringFromDate:[NSDate date]]; ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com/%@",bucket,path]]] autorelease]; [request setPostBodyFilePath:filePath]; [request setShouldStreamPostDataFromDisk:YES]; [request setRequestMethod:@"PUT"]; [request addRequestHeader:@"x-amz-acl" value:@"private"]; [request addRequestHeader:@"Content-Type" value:contentType]; [request addRequestHeader:@"Date" value:date]; NSString *canonicalizedAmzHeaders = @"x-amz-acl:private"; NSString *canonicalizedResource = [NSString stringWithFormat:@"/%@/%@",bucket,path]; NSString *stringToSign = [NSString stringWithFormat:@"PUT\n\n%@\n%@\n%@\n%@",contentType,date,canonicalizedAmzHeaders,canonicalizedResource]; NSString *signature = [self base64forData:[self HMACSHA1withKey:secretAccessKey forString:stringToSign]]; NSString *auth = [NSString stringWithFormat:@"AWS %@:%@",accessKey,signature]; [request addRequestHeader:@"Authorization" value:auth]; [request start]; NSLog(@"%@",[request responseString]); } // Source: http://stackoverflow.com/questions/476455/is-there-a-library-for-iphone-to-work-with-hmac-sha-1-encoding - (NSData *)HMACSHA1withKey:(NSString *)key forString:(NSString *)string { NSData *clearTextData = [string dataUsingEncoding:NSUTF8StringEncoding]; NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding]; uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0}; CCHmacContext hmacContext; CCHmacInit(&amp;hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length); CCHmacUpdate(&amp;hmacContext, clearTextData.bytes, clearTextData.length); CCHmacFinal(&amp;hmacContext, digest); return [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH]; } //Source http://www.cocoadev.com/index.pl?BaseSixtyFour - (NSString *)base64forData:(NSData *)data { static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; if ([data length] == 0) return @""; char *characters = malloc((([data length] + 2) / 3) * 4); if (characters == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (i &lt; [data length]) { char buffer[3] = {0,0,0}; short bufferLength = 0; while (bufferLength &lt; 3 &amp;&amp; i &lt; [data length]) buffer[bufferLength++] = ((char *)[data bytes])[i++]; // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary. characters[length++] = encodingTable[(buffer[0] &amp; 0xFC) &gt;&gt; 2]; characters[length++] = encodingTable[((buffer[0] &amp; 0x03) &lt;&lt; 4) | ((buffer[1] &amp; 0xF0) &gt;&gt; 4)]; if (bufferLength &gt; 1) characters[length++] = encodingTable[((buffer[1] &amp; 0x0F) &lt;&lt; 2) | ((buffer[2] &amp; 0xC0) &gt;&gt; 6)]; else characters[length++] = '='; if (bufferLength &gt; 2) characters[length++] = encodingTable[buffer[2] &amp; 0x3F]; else characters[length++] = '='; } return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload