我试图通过蓝牙从外部附件读取最多2MB,并且比预期花费的时间更长。我们正在对外部附件方面进行优化,但我还希望能够在将数据上传到s3之前将数据写入临时文件的更有效方法,甚至更好地将输入流直接输入到s3。
目前我们有一个非常简单的机制,它几乎基于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的最佳方式是什么?
答案 0 :(得分:0)
您可能想尝试在后台线程中将数据发送到S3。