in WebApp.py [0:0]
def send_head(self):
"""Common code for GET and HEAD commands.
This sends the response code and MIME headers.
Return value is either a file object (which has to be copied
to the outputfile by the caller unless the command was HEAD,
and must be closed by the caller under all circumstances), or
None, in which case the caller has nothing further to do.
"""
path = self.translate_path(self.path)
f = None
if os.path.isdir(path):
if not self.path.endswith('/'):
# redirect browser - doing basically what apache does
self.send_response(301)
self.send_header("Location", self.path + "/")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)
ctype = self.guess_type(path)
# Requesting a file in S3, let's get the file if the instance has the right role
# file will be downloaded from S3 into the local system and server from the local system
if 'S3' in path:
pathsplit = path.split('/')
s3path = pathsplit[-1]
#remake the path without the 'S3' tag
path = pathsplit[0]+ '/' + pathsplit[1]+ '/' + pathsplit[2] + '/' +pathsplit[3]+ '/' + "localfile-"+pathsplit[-1]
KEY = str(s3path)
try:
my_bucket.download_file(KEY, "localfile-"+KEY)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The S3 object does not exist.")
else:
raise
try:
# Always read in binary mode. Opening files in text mode may cause
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
except IOError:
self.send_error(404, "File not found")
return None
print("LOCAL PATH: ",path)
self.send_response(200)
self.send_header("Content-type", ctype)
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return f