24 #include <aws/s3/S3Client.h> 25 #include <aws/s3/model/PutObjectRequest.h> 26 #include <aws/s3/model/GetObjectRequest.h> 27 #include <aws/s3/model/GetObjectResult.h> 28 #include <aws/s3/model/HeadObjectRequest.h> 29 #include <aws/core/Aws.h> 30 #include <aws/core/client/ClientConfiguration.h> 31 #include <aws/core/http/HttpRequest.h> 32 #include <aws/core/utils/memory/stl/AWSStringStream.h> 48 static ossimTrace traceDebug(
"ossimS3StreamBuffer:debug");
55 m_bufferActualDataSize(0),
56 m_currentBlockPosition(-1),
62 Aws::Client::ClientConfiguration config;
66 preferencesKWL().findKey(std::string(
"ossim.plugins.aws.s3.region"));
69 config.region = region.c_str();
91 blockNumber = byteOffset/m_buffer.size();
102 if(m_buffer.size()>0)
104 blockOffset = byteOffset%m_buffer.size();
118 startRange = blockIndex*m_buffer.size();
119 endRange = startRange + m_buffer.size()-1;
132 <<
"ossim::S3StreamBuffer::loadBlock DEBUG: entered with absolute position: " << absolutePosition <<
"\n";
137 GetObjectRequest getObjectRequest;
140 ossim_int64 blockIndex = getBlockIndex(absolutePosition);
141 if((absolutePosition < 0) || (absolutePosition > (
ossim_int64)m_fileSize))
return false;
143 if(getBlockRangeInBytes(blockIndex, startRange, endRange))
145 stringStream <<
"bytes=" << startRange <<
"-" << endRange;
146 getObjectRequest.WithBucket(m_bucket.c_str())
147 .WithKey(m_key.c_str()).WithRange(stringStream.str().c_str());
148 auto getObjectOutcome = m_client->GetObject(getObjectRequest);
150 if(getObjectOutcome.IsSuccess())
153 Aws::IOStream& bodyStream = getObjectOutcome.GetResult().GetBody();
154 ossim_int64 bufSize = getObjectOutcome.GetResult().GetContentLength();
156 m_bufferActualDataSize = bufSize;
157 bodyStream.read(&m_buffer.front(), bufSize);
158 m_bufferPtr = &m_buffer.front();
161 setg(m_bufferPtr, m_bufferPtr + delta, m_bufferPtr+m_bufferActualDataSize);
162 m_blockInfo.setBytes(startRange,startRange+delta,startRange+m_bufferActualDataSize);
164 m_currentBlockPosition = startRange;
171 m_bufferActualDataSize = 0;
177 <<
"ossim::S3StreamBuffer::loadBlock DEBUG: leaving with absolutePosition " << absolutePosition <<
"\n";
186 std::ios_base::openmode m)
188 std::string temp(connectionString);
189 return open(temp, options, m);
194 std::ios_base::openmode )
199 <<
"ossim::S3StreamBuffer::open DEBUG: entered..... with connection " << connectionString << std::endl;
215 m_fileSize = filesize;
219 m_currentBlockPosition = 0;
224 if(!m_bucket.empty() && !m_key.empty())
226 HeadObjectRequest headObjectRequest;
227 headObjectRequest.WithBucket(m_bucket.c_str())
228 .WithKey(m_key.c_str());
229 auto headObject = m_client->HeadObject(headObjectRequest);
230 if(headObject.IsSuccess())
232 m_fileSize = headObject.GetResult().GetContentLength();
234 m_currentBlockPosition = 0;
242 m_currentBlockPosition = 0;
256 <<
"ossim::S3StreamBuffer::open DEBUG: Took " << delta <<
" seconds to open" << std::endl;
258 <<
"ossim::S3StreamBuffer::open DEBUG: leaving....." << std::endl;
261 if(m_opened)
return this;
274 m_currentBlockPosition = 0;
275 m_blockInfo.setBytes(0,0,0);
285 else if( !m_blockInfo.withinWindow() )
287 ossim_int64 absolutePosition = getAbsoluteByteOffset();
288 if(absolutePosition < 0)
293 if(!loadBlock(absolutePosition))
310 std::ios_base::seekdir dir,
311 std::ios_base::openmode mode)
316 pos_type result = pos_type(off_type(-1));
318 if((mode & std::ios_base::in)&&
319 (mode & std::ios_base::out))
325 case std::ios_base::beg:
327 ossim_int64 absolutePosition = getAbsoluteByteOffset();
342 result = pos_type(offset);
352 if(mode & std::ios_base::in)
354 absolutePosition = getAbsoluteByteOffset();
356 setg(eback(), gptr()+delta, egptr());
357 m_blockInfo.setCurrentByte(offset);
362 case std::ios_base::cur:
376 result = getAbsoluteByteOffset();
381 setg(eback(), gptr()+offset, egptr());
382 m_blockInfo.setCurrentByte(m_blockInfo.getCurrentByte()+offset);
387 case std::ios_base::end:
389 ossim_int64 absolutePosition = m_fileSize + offset;
397 ossim_int64 currentAbsolutePosition = getAbsoluteByteOffset();
398 ossim_int64 delta = absolutePosition-currentAbsolutePosition;
402 if(mode & std::ios_base::in )
404 setg(eback(), gptr()+delta, egptr());
405 m_blockInfo.setCurrentByte(absolutePosition);
406 result = absolutePosition;
423 pos_type result = pos_type(off_type(-1));
433 ossim_int64 absoluteLocation = getAbsoluteByteOffset();
434 if(mode & std::ios_base::in)
438 m_blockInfo.setCurrentByte(tempPos);
442 setg(eback(), gptr()+delta, egptr());
454 if(!is_open())
return EOF;
458 if((!m_blockInfo.withinWindow())&&is_open())
471 if(!loadBlock(m_blockInfo.getCurrentByte()))
479 ossim_int64 currentAbsolutePosition = m_blockInfo.getCurrentByte();
481 if(currentAbsolutePosition >= (
ossim_int64)m_fileSize)
485 else if((currentAbsolutePosition + bytesNeedToRead)>(
ossim_int64)m_fileSize)
487 bytesNeedToRead = (m_fileSize - currentAbsolutePosition);
490 while(bytesNeedToRead > 0)
492 currentAbsolutePosition = m_blockInfo.getCurrentByte();
494 if(!m_blockInfo.withinWindow())
496 if(!loadBlock(m_blockInfo.getCurrentByte()))
500 currentAbsolutePosition = m_blockInfo.getCurrentByte();
504 if(currentAbsolutePosition>=0)
508 ossim_int64 delta = (m_blockInfo.getEndByte()-m_blockInfo.getCurrentByte());
510 if(delta <= bytesNeedToRead)
513 std::memcpy(s+bytesRead, gptr(), delta);
517 bytesNeedToRead-=delta;
519 setg(eback(), gptr()+delta, egptr());
520 m_blockInfo.setCurrentByte(m_blockInfo.getCurrentByte()+delta);
524 std::memcpy(s+bytesRead, gptr(), bytesNeedToRead);
525 setg(eback(), gptr()+bytesNeedToRead, egptr());
526 m_blockInfo.setCurrentByte(m_blockInfo.getCurrentByte()+bytesNeedToRead);
529 bytesRead+=bytesNeedToRead;
538 return std::streamsize(bytesRead);
545 if(m_currentBlockPosition >= 0)
547 result = m_blockInfo.getCurrentByte();
561 if(!gptr())
return false;
562 return ((gptr()>=eback()) && (gptr()<egptr()));
572 return m_buffer.size();
bool getBlockRangeInBytes(ossim_int64 blockIndex, ossim_int64 &startRange, ossim_int64 &endRange) const
ossim_int64 getBlockIndex(ossim_int64 byteOffset) const
virtual std::streamsize xsgetn(char_type *s, std::streamsize n)
std::basic_stringstream< char > stringstream
Class for char mixed input and output memory streams.
virtual pos_type seekpos(pos_type pos, std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out)
unsigned long long Timer_t
Represents serializable keyword/value map.
virtual pos_type seekoff(off_type offset, std::ios_base::seekdir dir, std::ios_base::openmode __mode=std::ios_base::in|std::ios_base::out)
ossim_uint64 getBlockSize() const
static ossimTimer * instance()
os2<< "> n<< " > nendobj n
unsigned long long ossim_uint64
const ossimString & getProtocol() const
unsigned int ossim_uint32
S3StreamBuffer * open(const char *connectionString, const ossimKeywordlist &options, std::ios_base::openmode mode)
const ossimString & getPath() const
virtual ~S3StreamBuffer()
static ossimPreferences * instance()
std::shared_ptr< Aws::S3::S3Client > m_client
bool withinWindow() const
S3StreamBuffer(ossim_int64 blockSize=ossim::S3StreamDefaults::m_readBlocksize)
std::shared_ptr< Aws::S3::S3Client > getSharedS3Client() const
ossim_int64 getAbsoluteByteOffset() const
ossim_int64 getBlockOffset(ossim_int64 byteOffset) const
static AwsStreamFactory * instance()
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string's contents...
const ossimString & getIp() const
bool loadBlock(ossim_int64 absolutePosition)
Timer_t tick() const
Get the timers tick value.
double delta_s(Timer_t t1, Timer_t t2) const
Get the time in seconds between timer ticks t1 and t2.
unsigned char ossim_uint8
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
ossim_uint64 getFileSize() const