Lewoniewski
Lewoniewski

Reputation: 106

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://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

How to do this for remote URL?

Upvotes: 8

Views: 11890

Answers (4)

Ronald Das
Ronald Das

Reputation: 1277

You can also use the upload_blob_from_url function from the BlobClient class https://learn.microsoft.com/en-gb/python/api/azure-storage-blob/azure.storage.blob.blobclient?view=azure-python#azure-storage-blob-blobclient-upload-blob-from-url

Example

from azure.storage.blob import BlobServiceClient

blob_service_client = BlobServiceClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING)

def upload_blob_from_url(container_name,blob_name,source_url):
    blob = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    blob.upload_blob_from_url(source_url)
 

Upvotes: 1

Ognjen Stanisavljevic
Ognjen Stanisavljevic

Reputation: 51

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

Upvotes: 0

Gaurav Mantri
Gaurav Mantri

Reputation: 136386

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

Upvotes: 9

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30025

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

Upvotes: 2

Related Questions