Skip to content

Commit e7f052e

Browse files
authored
Merge pull request #752 from airbnb/mtl-update_repo_save
[kp] add html file into .kp folder
2 parents e835c73 + bdd81df commit e7f052e

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

knowledge_repo/post.py

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import uuid
1515
import yaml
1616
import PIL.Image
17+
import markdown
1718

1819
logger = logging.getLogger(__name__)
1920

@@ -334,6 +335,10 @@ def write(self, md, headers=None, images={}, interactive=False):
334335
md
335336
)
336337

338+
# convert md to html
339+
html = markdown.markdown(md)
340+
self._write_ref('knowledge.html', encode(html))
341+
337342
self._write_ref('knowledge.md', encode(md))
338343

339344
for image, data in list(images.items()):

knowledge_repo/repositories/s3repositorty.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ def _save(self, file, file_path, src_paths=[]):
9898
# upload files to S3
9999
for dirpath, dirnames, filenames in os.walk(os.path.join(self._path, file_path)):
100100
for filename in filenames:
101+
content_type = "text/html" if filename.endswith(".html") else "binary/octet-stream"
101102
upload_file_to_s3(self._s3_client, os.path.join(
102-
dirpath, filename), self._s3_bucket, os.path.join(remove_prefix(dirpath, self._path), filename))
103+
dirpath, filename), self._s3_bucket, os.path.join(remove_prefix(dirpath, self._path), filename), content_type)
103104

104105
# delete raw file after post processing and upload
105106
if os.path.exists(file):

knowledge_repo/utils/s3.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,25 @@
33
import logging
44
import os
55
import json
6-
from s3path import S3Path
6+
import re
77

88
logger = logging.getLogger(__name__)
99
AWS_S3_AUTH_PATH = '.configs/aws_s3_auth.json'
1010

1111

12-
def parse_s3_path(s3_url):
13-
"""Get s3_path for S3 Object URL
14-
15-
:param s3_url: url of s3 object
16-
:return: bucket and key name
17-
"""
18-
path = S3Path.from_uri(s3_url)
19-
return path.bucket, path.key
20-
21-
2212
def parse_s3_uri(s3_uri):
2313
"""Get s3_path for S3 Object URL
2414
2515
:param s3_url: url of s3 object
2616
:return: s3_bucket, s3_client
2717
"""
28-
path = S3Path.from_uri(s3_uri)
29-
uri_splt = path.key.split('/')
30-
return path.bucket, get_s3_client(uri_splt[1], uri_splt[2], uri_splt[0]), uri_splt[3] if len(uri_splt) > 3 else ''
18+
19+
matches = re.match("s3://(.*?)/(.*)/(.*)/", s3_uri)
20+
if matches:
21+
bucket, _, key_name = matches.groups()
22+
else:
23+
raise ValueError(f'Cannot interpret {s3_uri}')
24+
return bucket, get_s3_client(), key_name
3125

3226

3327
def get_s3_client():
@@ -55,13 +49,15 @@ def upload_file_to_s3(
5549
file_name,
5650
bucket,
5751
object_name=None,
52+
content_type="binary/octet-stream",
5853
):
5954
"""Upload a file to an object in an S3 bucket
6055
6156
:param s3_client: a boto3 S3 client
6257
:param file_name: File to upload
6358
:param bucket: Bucket to upload to
6459
:param object_name: S3 object name. If not specified, file_name is used
60+
:param content_type: AWS S3 Content Type, default to "binary/octet-stream"
6561
:return: True if file was uploaded, else False
6662
"""
6763

@@ -75,6 +71,7 @@ def upload_file_to_s3(
7571
file_name,
7672
bucket,
7773
object_name,
74+
ExtraArgs={'ContentType': content_type}
7875
)
7976
logger.info(response)
8077
except ClientError as client_error:

0 commit comments

Comments
 (0)