Очень читабельный код должен использовать .substring()
с начальным значением, установленным для индекса второго символа (1) (первый символ имеет индекс 0). Второй параметр метода .substring()
на самом деле необязателен, поэтому вам даже не нужно вызывать .length()
...
str = str.substring(1);
... да, это так просто ...
Как и @Shaded, просто зациклите это, в то время как первый символ вашей строки является «нежелательным» символом ...
var yourString = "0000test";
var unwantedCharacter = "0";
//there is really no need for === check, since we use String's charAt()
while( yourString.charAt(0) == unwantedCharacter ) yourString = yourString.substr(1);
//yourString now contains "test"
.slice()
против .substring()
против .substr()
Цитата (и больше об этом в) В чем разница между String.slice и String.substring?
Он также указывает, что если параметры для среза отрицательны, они ссылаются на строку с конца. Substring и substr не являются.
Вот базовый пример использования PUT. Очевидно, в реальном мире вам следует использовать очередь, а не синхронный запрос.
Если вы измените заголовки amz, не забудьте обновить 'canonicalizedAmzHeaders' согласно инструкциям Amazon.
#import "ASIHTTPRequest.h"
#import <CommonCrypto/CommonHMAC.h>
...
- (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(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length);
CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
CCHmacFinal(&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 < [data length])
{
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [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] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}
Существует также Connection Kit Какао-фреймворк, который может для загрузки данных в разные сервисы, включая Amazon S3. Я почти уверен, что в его источнике есть некоторые отправные точки для вас.
In case it's helpful for anyone finding this question - Basic S3 support is now built in to ASIHTTPRequest:
http://allseeing-i.com/ASIHTTPRequest/S3
I'd been thinking about adding S3 support for ages, but your question nudged it to the front of the queue :)