Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
production
geocannabis--drone-analytics-2
azure-blob-lib
Commits
46812582
Commit
46812582
authored
Feb 15, 2021
by
Louis Baetens
Browse files
Add the retrieval of the SAS for a blob
parent
6b2b8089
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
2 deletions
+42
-2
azure_blob_client/__init__.py
azure_blob_client/__init__.py
+42
-2
No files found.
azure_blob_client/__init__.py
View file @
46812582
...
@@ -17,6 +17,8 @@ from azure.storage.blob import (
...
@@ -17,6 +17,8 @@ from azure.storage.blob import (
AccountSasPermissions
,
AccountSasPermissions
,
ContainerClient
,
ContainerClient
,
BlobType
,
BlobType
,
generate_blob_sas
,
BlobSasPermissions
,
)
)
from
azure.core.exceptions
import
ClientAuthenticationError
from
azure.core.exceptions
import
ClientAuthenticationError
...
@@ -118,7 +120,7 @@ class AzureBlobClient:
...
@@ -118,7 +120,7 @@ class AzureBlobClient:
return
self
.
blob_service_client
().
delete_container
(
container
=
container_name
)
return
self
.
blob_service_client
().
delete_container
(
container
=
container_name
)
def
upload_blob_in_container
(
def
upload_blob_in_container
(
self
,
container_name
:
str
,
blob_name
:
str
,
blob_filename
:
str
,
blob_type
:
BlobType
=
BlobType
.
BlockBlob
self
,
container_name
:
str
,
blob_name
:
str
,
blob_filename
:
str
,
blob_type
:
BlobType
=
BlobType
.
BlockBlob
):
):
"""Upload a new blob in the container
"""Upload a new blob in the container
...
@@ -145,4 +147,42 @@ class AzureBlobClient:
...
@@ -145,4 +147,42 @@ class AzureBlobClient:
:param blob_name: Name of the blob to delete
:param blob_name: Name of the blob to delete
:type blob_name: str
:type blob_name: str
"""
"""
self
.
blob_container_client
(
container_name
=
container_name
).
delete_blob
(
blob
=
blob_name
)
self
.
blob_container_client
(
container_name
=
container_name
).
delete_blob
(
blob
=
blob_name
)
\ No newline at end of file
def
get_blob_sas_url
(
self
,
container_name
,
blob_name
,
time_to_expiry
=
1
):
"""
Get the URL of a blob, with the necessary read access
Args:
container_name (str): name of the caontainer
blob_name (str): name (path) of the blob in the container
time_to_expiry (int): in hours, the time of validity for this access
Returns:
(str): the authorized URL
"""
sas_blob
=
self
.
get_blob_sas
(
container_name
=
container_name
,
blob_name
=
blob_name
,
time_to_expiry
=
time_to_expiry
)
authorized_url
=
self
.
account_url
+
'/'
+
container_name
+
'/'
+
blob_name
+
'?'
+
sas_blob
return
authorized_url
def
get_blob_sas
(
self
,
container_name
,
blob_name
,
time_to_expiry
=
1
):
"""
Generate a blob SAS, to be able to access it from a standard URL
Args:
container_name (str): name of the caontainer
blob_name (str): name (path) of the blob in the container
time_to_expiry (int): in hours, the time of validity for this access
Returns:
(str): the SAS of the blob
"""
sas_blob
=
generate_blob_sas
(
account_name
=
self
.
storage_account
,
account_key
=
self
.
credentials
,
container_name
=
container_name
,
blob_name
=
blob_name
,
permission
=
BlobSasPermissions
(
read
=
True
),
expiry
=
datetime
.
utcnow
()
+
timedelta
(
hours
=
time_to_expiry
))
return
sas_blob
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment