我正在尝试通过蓝牙从外部附件读取最多 2MB 的数据,并且花费的时间比预期的要长得多。我们正在对事物的外部附件方面进行优化,但我也在寻找一种更有效的方法,在将数据上传到 s3 之前将数据写入临时文件,或者甚至更好地将输入流直接通过管道传输到 s3。
目前我们有一个非常简单的机制,它几乎基于 EADemo 代码和 aws 示例:
// 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 的最佳方法是什么?