7
votes

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?

3

3 Answers

7
votes

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()
2
votes

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

0
votes

Oke, one additional information because I was confused when I saw this script.

Information for account_name and account_key you can find on portal.azure.com:

Resource Groups --> Select Resource Group where is Blob Storage --> Select Storage Account --> Click on Access Key from left panel.

account_name=Storage account name (like: mybackupstorage)

account_key=Key (like: ihwIKU@Hsniq87dbki*&qlos8ejuwa3ox7w4rykwij7ryx83deozd)

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='', account_key='')
    container_name ='container-name'

    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()