Skip to content

Commit

Permalink
Update azure_blob_remover.py
Browse files Browse the repository at this point in the history
  • Loading branch information
parkermac committed Sep 23, 2022
1 parent 4dd89cb commit 4a9d16a
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions misc/azure_blob_remover.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Code to delete selected blobs or containers
"""

import os, sys
sys.path.append(os.path.abspath('../../LiveOcean/alpha'))
import Lfun
Ldir = Lfun.Lstart()
Code to delete a range of containers.
from datetime import datetime, timedelta
This code has to be customized by hand to delete specific containers.
# ****************** CASE-SPECIFIC CODE *****************
RESULT: this is fast and effective for things that are tedious to do from the portal.
"""

from datetime import datetime
start_time = datetime.now()
from azure.storage.blob import BlobServiceClient
from azure.storage.blob import PublicAccess
from lo_tools import Lfun
from time import time
from datetime import datetime, timedelta

dt0 = datetime(2019,7,1) # start time
dt1 = datetime(2020,9,1) # end time
Ldir = Lfun.Lstart()

# # account name and key
# azu_dict = Lfun.csv_to_dict(Ldir['data'] + 'accounts/azure_pm_2015.05.25.csv')
# account = azu_dict['account']
# key = azu_dict['key']
# # get a handle to the account
# blob_service = BlockBlobService(account_name=account, account_key=key)
# screen output
print(' azure_container_remover.py '.center(60,'='))
print(datetime.now().strftime('%Y.%m.%d %H:%M:%S'))
print('Deleting user-specified containers')

tt0 = time()
# get the blob_service
from azure.storage.blob import BlobServiceClient
from azure.storage.blob import PublicAccess
azu_dict = Lfun.csv_to_dict(Ldir['data'] + 'accounts/azure_pm_2015.05.25.csv')
account = azu_dict['account']
key = azu_dict['key']
account_fn = Ldir['data'] / 'accounts' / 'azure_pm_2015.05.25.txt'
with account_fn.open() as aa:
# make sure to strip newline characters
account = aa.readline().strip()
key = aa.readline().strip()
connection_string = ('DefaultEndpointsProtocol=https' +
';AccountName=' + account +
';AccountKey=' + key +
';EndpointSuffix=core.windows.net')
blob_service = BlobServiceClient.from_connection_string(conn_str=connection_string)


dt0 = datetime(2020,9,15)
# dt1 = datetime(2020,9,16)
dt1 = datetime.now()

dt = dt0
while dt <= dt1:
Ldir['date_string'] = dt.strftime('%Y.%m.%d')
print('Deleting Azure files for ' + Ldir['date_string'])
f_string = 'f' + Ldir['date_string']
ff_string = f_string.replace('.','') # azure does not like dots in container names
container_name = ff_string
container_name = 'f' + dt.strftime('%Y%m%d')
print(container_name)

# create the container if needed
try:
blob_service.create_container(container_name, public_access=PublicAccess.Container)

except:
# assume error is because container exists
pass
result = blob_service.delete_container(container_name)
print(' - result = ' + str(result))
dt = dt + timedelta(days=1)
result = blob_service.delete_container(container_name)
except Exception as e:
print(' no action: assume container did not exist')
# print(e)

dt = dt + timedelta(days=1)

print('DONE')
print('Total time %0.2f sec' % (time()-tt0))

0 comments on commit 4a9d16a

Please sign in to comment.