-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add Azure Storage WIP * support Azure Storage connection strings with account key * improve azure storage implementation, add AzureStorageClient * update docs * support recursive deletion
- Loading branch information
1 parent
76f30bb
commit 40a764f
Showing
14 changed files
with
396 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
Google Cloud Storage | ||
==================== | ||
|
||
Accessing a Azure Blob Storage (GCS) with the shell tool `azcopy`. | ||
|
||
Installation | ||
------------ | ||
|
||
You need to install `azcopy`. Take a look at `Get started with Azcopy <https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10>`_. | ||
|
||
|
||
Configuration examples | ||
---------------------- | ||
|
||
```{note} | ||
Currently some of the functions require a SAS token, and some of the functions | ||
require a account key. It is recommended to provide both a SAS token | ||
and a account key. | ||
``` | ||
|
||
.. tabs:: | ||
|
||
.. group-tab:: SAS token | ||
|
||
.. code-block:: python | ||
import pathlib | ||
import mara_storage.storages | ||
mara_storage.config.storages = lambda: { | ||
'data': mara_storage.storages.AzureStorage( | ||
account_name='account-name', | ||
container_name='container-name', | ||
sas='sp=racwdlm&st=2022-05-11T10:04:05Z&se=2023-05-11T18:04:05Z&spr=https&sv=2020-08-04&sr=c&sig=u7tqxugyv5MbyrtFdEUp22tnou4wifBoUfIaLDazeRT%3D'), | ||
# optional | ||
storage_type = 'dfs' # use a dfs client instead of 'blob' (default value) | ||
} | ||
.. group-tab:: Account key | ||
|
||
.. code-block:: python | ||
import pathlib | ||
import mara_storage.storages | ||
mara_storage.config.storages = lambda: { | ||
'data': mara_storage.storages.AzureStorage( | ||
account_name='account-name', | ||
container_name='container-name', | ||
account_key='<key>', | ||
# optional | ||
storage_type = 'dfs' # use a dfs client instead of 'blob' (default value) | ||
), | ||
} | ||
| | ||
| | ||
API reference | ||
------------- | ||
|
||
This section contains database specific API in the module. | ||
|
||
|
||
Configuration | ||
~~~~~~~~~~~~~ | ||
|
||
.. module:: mara_storage.storages | ||
:noindex: | ||
|
||
.. autoclass:: AzureStorage | ||
:special-members: __init__ | ||
:inherited-members: | ||
:members: |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
install-azcopy: | ||
# install azcopy in the virtual environment | ||
# see also: https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10 | ||
|
||
# download azcopy | ||
wget https://aka.ms/downloadazcopy-v10-linux | ||
tar -xvf downloadazcopy-v10-linux | ||
|
||
# install | ||
rm -f .venv/bin/azcopy | ||
mv ./azcopy_linux_amd64_*/azcopy .venv/bin/azcopy | ||
|
||
# clean up | ||
rm downloadazcopy-v10-linux | ||
rm -rf ./azcopy_linux_amd64_*/ downloadazcopy-v10-linux |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
|
||
from mara_storage.client import StorageClient | ||
from . import storages | ||
|
||
from azure.storage.blob import BlobClient, BlobServiceClient | ||
|
||
|
||
def init_client(storage: storages.AzureStorage, path: str = None) -> BlobClient: | ||
client = BlobClient.from_blob_url(storage.build_uri(path)) | ||
return client | ||
|
||
def init_service_client(storage: storages.AzureStorage, path: str = None) -> BlobServiceClient: | ||
client = BlobServiceClient.from_connection_string(storage.connection_string()) | ||
return client | ||
|
||
|
||
class AzureStorageClient(StorageClient): | ||
def __init__(self, storage: storages.AzureStorage): | ||
super().__init__(storage) | ||
|
||
self.__blob_service_client: BlobServiceClient = None | ||
self.__container_client = None | ||
|
||
@property | ||
def _blob_service_client(self): | ||
if not self.__blob_service_client: | ||
self.__blob_service_client = init_service_client(self._storage) | ||
|
||
return self.__blob_service_client | ||
|
||
@property | ||
def _container_client(self): | ||
if not self.__container_client: | ||
self.__container_client = self._blob_service_client.get_container_client(self._storage.container_name) | ||
|
||
return self.__container_client | ||
|
||
def creation_timestamp(self, path: str) -> datetime.datetime: | ||
blob_client = self._container_client.get_blob_client(path) | ||
properties = blob_client.get_blob_properties() | ||
|
||
return properties.creation_time | ||
|
||
def last_modification_timestamp(self, path: str) -> datetime.datetime: | ||
blob_client = self._container_client.get_blob_client(path) | ||
properties = blob_client.get_blob_properties() | ||
|
||
return properties.last_modified | ||
|
||
def iterate_files(self, file_pattern: str): | ||
blobs = self._container_client.list_blobs(name_starts_with=file_pattern) | ||
|
||
for blob in blobs: | ||
if blob: | ||
yield blob.name |
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
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
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
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
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
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
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
Oops, something went wrong.