Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file from URL to Microsoft Azure Blob Storage

It is not a problem to upload file from local path (from my computer). However, I didn't find how to upload from specific URL.

If it possible - solution in Python is needed. There is documentation only for local files https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

How to do this for remote URL?

like image 557
Lewoniewski Avatar asked Nov 06 '18 18:11

Lewoniewski


People also ask

How do I access blob storage from URL?

By default, the URL for accessing the Blob service in a storage account is https://<your account name>. blob.core.windows.net. You can map your own domain or subdomain to the Blob service for your storage account so that users can reach it using the custom domain or subdomain.

How do I upload to Azure blob storage from my frontend?

Run project locally to verify connection to Storage account. Your SAS token and storage account name are set in the src/azure-storage-blob. ts file, so you are ready to run the application. Select an image from the images folder to upload then select the Upload!

How do I upload files to Azure blob storage using API?

Create an access policy with write permission. Create an asset. Create a SAS locator and create the upload URL. Upload a file to blob storage using the upload URL.


2 Answers

You can make use of async copy blob functionality to create a blob from a publicly accessible URL. Please see sample code below:

from azure.storage.blob import BlockBlobService, PublicAccess
from azure.storage.blob.models import Blob

def run_sample():
    block_blob_service = BlockBlobService(account_name='your_name', account_key='your_key')
    container_name ='t1s'

    block_blob_service.copy_blob(container_name,'remoteURL.pdf','https://media.readthedocs.org/pdf/azure-storage/v0.20.3/azure-storage.pdf')


# Main method.
if __name__ == '__main__':
    run_sample()
like image 137
Gaurav Mantri Avatar answered Oct 24 '22 09:10

Gaurav Mantri


You can first download the file as stream, then call the method create_blob_from_stream.

The following is a demo code:

from azure.storage.blob import BlockBlobService, PublicAccess
from azure.storage.blob.models import Blob
import requests

def run_sample():
    block_blob_service = BlockBlobService(account_name='your_name', account_key='your_key')
    container_name ='t1s'

    response = requests.get('https://media.readthedocs.org/pdf/azure-storage/v0.20.3/azure-storage.pdf',stream=True)       
    block_blob_service.create_blob_from_stream(container_name,'remoteURL.pdf',response.raw)


# Main method.
if __name__ == '__main__':
    run_sample()

Test result as below: enter image description here

like image 33
Ivan Yang Avatar answered Oct 24 '22 09:10

Ivan Yang