Skip to content

Instantly share code, notes, and snippets.

@jspinella
Created September 9, 2021 04:30
Show Gist options
  • Save jspinella/d27f135a913fc5dda834424e306c4faf to your computer and use it in GitHub Desktop.
Save jspinella/d27f135a913fc5dda834424e306c4faf to your computer and use it in GitHub Desktop.
# convert a table from an accdb file to CSV and upload to an AWS S3 bucket
import os, subprocess, urllib.request, requests, zipfile, boto3
from bs4 import BeautifulSoup
from lxml import etree
def handler(event, context): # we aren't using event or context here, but you probably will in your real-world implementation
# cd into Lambda's writable directory (allows up to 512MB of files)
os.chdir('/tmp')
#todo: download the accdb file from S3 or the Internet
# convert the accdb table to CSV
DATABASE = "yourFile.accdb" # yourFile.mdb should work as well
TABLE = "tableInAccdbToConvert" # e.g. "MyAccessTable"
# based on code here: http://okfnlabs.org/handbook/data/patterns/liberating-access-databases/ which loops through all tables in accdb file
# here I am just converting a single table to CSV as I only needed one table
filename = TABLE.replace(' ','_') + '.csv'
print(f'Converting {TABLE} to CSV format...')
with open(filename, 'wb') as f:
subprocess.call(['mdb-export', DATABASE, TABLE], stdout=f)
# upload CSV file to S3
s3 = boto3.client(
's3',
region_name='us-east-1',
aws_access_key_id='yourAccessKeyId',
aws_secret_access_key='yourAccessKeyValue'
)
S3_BUCKET = "yourS3BucketName"
S3_FILE_NAME = "export.csv" # override file name of CSV in S3 here
print(f"Uploading {S3_FILE_NAME} to S3 bucket {S3_BUCKET}")
response = s3.upload_file(f"{TABLE}.csv", S3_BUCKET, S3_FILE_NAME)
print(f"S3 response: {response}")
print("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment