title: Most efficient way to read from external accessory input stream and push to s3?
body: <p>I am trying to read up to max 2MB from an external accessory via bluetooth and its taking a lot longer than expected. We are optimising on the external accessory side of things but I am looking also for a more efficient way to write the data to a temporary file before uploading it to s3 or even better just piping the inputstream straight to s3.</p>
  
  <p>At the moment we have a very simple mechanism which is pretty much based on the EADemo code and aws examples:</p>
  
  <pre><code>// 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];
  }
  </code></pre>
  
  <p>And our _sessionDataReceived:</p>
  
  <pre><code>- (void)_sessionDataReceived:(NSNotification *)notification
  {
      SessionController *sessionController = (SessionController *)[notification object];
      uint32_t bytesAvailable = 0;
      NSData *streamData;
  
      while ((bytesAvailable = [sessionController readBytesAvailable]) &gt; 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];
      }
  }
  </code></pre>
  
  <p>And our sendToS3:</p>
  
  <pre><code>- (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];
  }
  </code></pre>
  
  <p>What would be the best way to get and push this data to s3 as fast/efficiently as possible?</p>