OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimAwsStreamFactory.cpp
Go to the documentation of this file.
1 //---
2 //
3 // License: MIT
4 //
5 // Description: OSSIM Amazon Web Services (AWS) plugin initialization
6 // code.
7 //
8 //---
9 // $Id$
10 
11 #include "ossimAwsStreamFactory.h"
12 #include "ossimS3IStream.h"
13 #include "S3HeaderCache.h"
14 
15 #include <ossim/base/ossimCommon.h>
19 #include <ossim/base/ossimTimer.h>
20 #include <ossim/base/ossimTrace.h>
21 #include <ossim/base/ossimUrl.h>
22 
23 #include <aws/core/client/ClientConfiguration.h>
24 #include <aws/s3/S3Client.h>
25 #include <aws/s3/model/HeadObjectRequest.h>
26 
27 
28 static ossimTrace traceDebug("ossimAwsStreamFactory:debug");
29 
31 
33 {
34 }
35 
37 {
38  if( !m_instance )
39  {
40  m_instance = new ossim::AwsStreamFactory();
41  }
42  return m_instance;
43 }
44 
45 std::shared_ptr<ossim::istream> ossim::AwsStreamFactory::createIstream(
46  const std::string& connectionString,
47  const ossimKeywordlist& options,
48  std::ios_base::openmode openMode) const
49 {
50  std::shared_ptr<ossim::S3IStream> result = std::make_shared<ossim::S3IStream>();
51  if(traceDebug())
52  {
53  ossimNotify(ossimNotifyLevel_WARN) << "ossim::AwsStreamFactory::createIstream: Entered...............\n";
54  }
55 
56  //---
57  // Hack for upstream code calling ossimFilename::convertToNative()
58  // wrecking s3 url.
59  //---
60 #if defined(_WIN32)
61  ossimFilename f = connectionString;
62  result->open( f.string(), options, openMode) ;
63 #else
64  result->open( connectionString, options, openMode );
65 #endif
66 
67  if(!result->good())
68  {
69  result.reset();
70  }
71  if(traceDebug())
72  {
73  ossimNotify(ossimNotifyLevel_WARN) << "ossim::AwsStreamFactory::createIstream: Leaving...............\n";
74  }
75 
76  return result;
77 }
78 
79 std::shared_ptr<ossim::ostream> ossim::AwsStreamFactory::createOstream(
80  const std::string& /*connectionString*/,
81  const ossimKeywordlist& /* options */,
82  std::ios_base::openmode /*openMode*/) const
83 {
84  return std::shared_ptr<ossim::ostream>(0);
85 }
86 
87 std::shared_ptr<ossim::iostream> ossim::AwsStreamFactory::createIOstream(
88  const std::string& /*connectionString*/,
89  const ossimKeywordlist& /* options */,
90  std::ios_base::openmode /*openMode*/) const
91 {
92  return std::shared_ptr<ossim::iostream>(0);
93 }
94 
96  const std::string& connectionString, bool& continueFlag) const
97 {
98  if(traceDebug())
99  {
101  << "ossim::AwsStreamFactory::exists() DEBUG: entered.....\n";
102 
103  }
104  bool result = false;
105 
106  // ossimTimer::Timer_t startTimer = ossimTimer::instance()->tick();
107 
108  if ( connectionString.size() )
109  {
110  // Check for url string:
111  std::size_t pos = connectionString.find( "://" );
112  if ( pos != std::string::npos )
113  {
114  ossimUrl url(connectionString);
115  if( (url.getProtocol() == "s3") || (url.getProtocol() == "S3") )
116  {
117  continueFlag = false; // Set to false to stop registry search.
118 
119  ossim_int64 filesize;
120  if(ossim::S3HeaderCache::instance()->getCachedFilesize(connectionString, filesize))
121  {
122  if(filesize >=0)
123  {
124  result = true;
125  }
126  }
127  else
128  {
129  std::string bucket = url.getIp().string();
130  std::string key = url.getPath().string();
131  Aws::S3::Model::HeadObjectRequest headObjectRequest;
132  headObjectRequest.WithBucket( bucket.c_str() )
133  .WithKey( key.c_str() );
134  auto headObject = m_client->HeadObject(headObjectRequest);
135  if(headObject.IsSuccess())
136  {
137  result = true;
138  filesize = headObject.GetResult().GetContentLength();
139  ossim::S3HeaderCache::Node_t nodePtr = std::make_shared<ossim::S3HeaderCacheNode>(filesize);
140  ossim::S3HeaderCache::instance()->addHeader(connectionString, nodePtr);
141  }
142  else
143  {
144  result = false;
145  ossim::S3HeaderCache::Node_t nodePtr = std::make_shared<ossim::S3HeaderCacheNode>(-1);
146  ossim::S3HeaderCache::instance()->addHeader(connectionString, nodePtr);
147  }
148  }
149  }
150  }
151  }
152 
153  // ossimTimer::Timer_t endTimer = ossimTimer::instance()->tick();
154  // cout << "time: " << ossimTimer::instance()->delta_s(startTimer, endTimer) << endl;
155 
156  // std::cout << "aws result: " << result << std::endl;
157  if(traceDebug())
158  {
160  << "ossim::AwsStreamFactory::exists() DEBUG: leaving.....\n";
161 
162  }
163 
164  return result;
165 }
166 
167 // Hidden from use:
169 {
170  // the stream is now shared so we must allocate here.
171  //
172  initClient();
173 }
174 
175 // Hidden from use:
177 {
178 }
179 
181 {
182  Aws::Client::ClientConfiguration config;
183 
184  // Look for AWS S3 region override:
185  std::string region = ossimPreferences::instance()->
186  preferencesKWL().findKey(std::string("ossim.plugins.aws.s3.region"));
187  if ( region.size() )
188  {
189  config.region = region.c_str();
190  }
191 
192  m_client = std::make_shared<Aws::S3::S3Client>(config);
193 //new Aws::S3::S3Client( config );
194 }
bool exists(const std::string &connectionString, bool &continueFlag) const
Methods to test if connection exists.
std::shared_ptr< S3HeaderCacheNode > Node_t
Definition: S3HeaderCache.h:30
Represents serializable keyword/value map.
virtual std::shared_ptr< ossim::istream > createIstream(const std::string &connectionString, const ossimKeywordlist &options, std::ios_base::openmode openMode) const
static AwsStreamFactory * m_instance
const ossimString & getProtocol() const
Definition: ossimUrl.h:17
const ossimString & getPath() const
Definition: ossimUrl.h:20
static ossimPreferences * instance()
static AwsStreamFactory * instance()
static std::shared_ptr< S3HeaderCache > instance()
long long ossim_int64
virtual std::shared_ptr< ossim::iostream > createIOstream(const std::string &connectionString, const ossimKeywordlist &options, std::ios_base::openmode openMode) const
const ossimString & getIp() const
Definition: ossimUrl.h:18
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
virtual std::shared_ptr< ossim::ostream > createOstream(const std::string &connectionString, const ossimKeywordlist &options, std::ios_base::openmode openMode) const
const std::string & string() const
Definition: ossimString.h:414