Я пытаюсь прочитать до 2 МБ от внешнего аксессуара через Bluetooth и его принятие намного дольше, чем ожидалось. Мы оптимизируем внешнюю сторону аксессуаров, но я также ищу более эффективный способ записи данных во временный файл, прежде чем загружать его на s3 или даже лучше всего подключить входной поток прямо к s3.Самый эффективный способ чтения с внешнего входного потока аксессуаров и нажатия на s3?
В настоящее время у нас есть очень простой механизм, который в значительной степени на основе кода и AWS примеров EADemo:
// low level read method - read data while there is data and space available in the input buffer
- (void)_readData {
#define INPUT_BUFFER_SIZE 1024
uint8_t buf[INPUT_BUFFER_SIZE];
while ([[_session inputStream] hasBytesAvailable])
{
NSInteger bytesRead = [[_session inputStream] read:buf maxLength:INPUT_BUFFER_SIZE];
if (_readData == nil) {
_readData = [[NSMutableData alloc] init];
}
[_readData appendBytes:(void *)buf length:bytesRead];
}
[[NSNotificationCenter defaultCenter] postNotificationName:SessionDataReceivedNotification object:self userInfo:nil];
}
И наш _sessionDataReceived:
- (void)_sessionDataReceived:(NSNotification *)notification
{
SessionController *sessionController = (SessionController *)[notification object];
uint32_t bytesAvailable = 0;
NSData *streamData;
while ((bytesAvailable = [sessionController readBytesAvailable]) > 0)
{
streamData = [sessionController readData:bytesAvailable];
}
if (![_fileMgr fileExistsAtPath:_deviceFile]) {
[_fileMgr createFileAtPath:_deviceFile contents:nil attributes:nil];
}
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:_deviceFile];
[fileHandle seekToEndOfFile];
[fileHandle writeData:streamData];
[fileHandle closeFile];
NSDictionary *attributes = [_fileMgr attributesOfItemAtPath:_deviceFile error:NULL];
unsigned long long fileSize = [attributes fileSize];
if (fileSize == (ourExpectedBytesSize)) {
[self sendToS3];
}
}
И наш sendToS3:
- (void)sendToS3
{
_s3filekey = [NSString stringWithFormat:@"%@/files/%@", user, _deviceFilename];
AmazonS3Client *s3 = [self s3Client];
S3TransferManager *tm = [S3TransferManager new];
tm.delegate = self;
tm.s3 = s3;
S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:_s3filekey inBucket:_uploadBucket];
por.requestTag = @"sendToS3";
por.filename = _deviceFile;
[tm upload:por];
}
Что было бы лучшим способом получить и направить эти данные на s3 так быстро/эффективно y насколько возможно?