Last active
February 10, 2020 08:37
-
-
Save zeddee/d995a27a5f7271e4b6720fdb5b41b20b to your computer and use it in GitHub Desktop.
Me trying to grok how to make a basic REST client in Python for Azure Blob Storage services
This file contains hidden or 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
#!/usr/bin/env python3 | |
import requests | |
import os | |
from dotenv import load_dotenv | |
from datetime import datetime | |
from azure.storage.common import CloudStorageAccount | |
load_dotenv('.env') | |
containername = os.environ.get('AZURE_CONTAINER_NAME') | |
project_id = os.environ.get('PROJECT_ID') | |
target_blob = 'genindex.html' | |
# snapshot_date_time = '' | |
azure_storage_account = os.environ.get('AZURE_STORAGE_ACCOUNT') | |
if (azure_storage_account is '' or azure_storage_account is None): | |
print(f'AZURE_STORAGE_ACCOUNT not found in .env: {azure_storage_account}') | |
raise KeyError | |
azure_sas_token = os.environ.get('AZURE_SAS_TOKEN') | |
if (azure_sas_token is '' or azure_sas_token is None): | |
print('AZURE_SAS_TOKEN not found in .env') | |
raise KeyError | |
if azure_sas_token.startswith('?'): | |
# portal.azure.com creates shared access tokens that | |
# start with a '?', which we strip off for consistency | |
azure_sas_token = azure_sas_token[1:] | |
get_this_url = f"https://{azure_storage_account}.blob.core.windows.net/{containername}/{project_id}/{target_blob}?{azure_sas_token}" | |
# if (snapshot_date_time != '' or snapshot_date_time is None): | |
# get_this_url = get_this_url + f"/?snapshot={snapshot_date_time}" | |
print(get_this_url) | |
with requests.Session() as r: | |
r = requests.get( | |
url=get_this_url, | |
headers={ | |
#'Host': azure_storage_account, | |
'Date': print(datetime.now()) | |
} | |
) | |
from pprint import pprint | |
r.raise_for_status() | |
print(r.status_code) | |
pprint(r.content.decode('utf-8')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment