Last active
September 16, 2020 19:17
-
-
Save nescobar/4a014af350613656ae5f8e86a17edf84 to your computer and use it in GitHub Desktop.
Saves file in DBFS and then uploads it to S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AWS | |
ACCESS_KEY = dbutils.secrets.get(scope="aws-s3", key="access_key") | |
SECRET_KEY = dbutils.secrets.get(scope="aws-s3", key="secret_key") | |
ENCODED_SECRET_KEY = dbutils.secrets.get(scope="aws-s3", key="encoded_secret_key") | |
AWS_BUCKET_NAME = "bucket-static-webpages" | |
def upload_to_s3(file_name, file_content): | |
# Check if file_name is a key in dashboards dictionary | |
if file_name not in dashboards: | |
print("{} is not a key in the dictionary".format(file_name)) | |
return | |
# Create file in DBFS Filestore | |
try: | |
dbutils.fs.rm("/FileStore/graph_file_static/{}.html".format(dashboards[file_name])) | |
dbutils.fs.put("/FileStore/graph_file_static/{}.html".format(dashboards[file_name]), file_content) | |
except: | |
dbutils.fs.put("/FileStore/graph_file_static/{}.html".format(dashboards[file_name]), file_content) | |
# Upload file from Filestore to S3 | |
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) | |
with open("/dbfs/FileStore/graph_file_static/{}.html".format(dashboards[file_name]), "rb") as f: | |
s3.upload_fileobj(f, AWS_BUCKET_NAME, "{}.html".format(dashboards[file_name]), ExtraArgs={'ACL': 'public-read', 'ContentType':'text/html'}) | |
print("File {} uploaded to S3".format(file_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment