简体   繁体   中英

how to download file from a blob and save in folder in python

i'm trying to download a .pdf file from a blob in python, but i can't figure it out how to save to a directory in my project folder. it keeps giving me an error permission denied.

the code:

block_blob_service = BlockBlobService(account_name='hi', account_key='hi') 
tenant = 'hi'
container_name = 'files'
id_ = 1
upload_id = 'upload'+'/'+str(id_)

location = 'local'+'/'+tenant+'/'+upload_id+'/'

for c in block_blob_service.list_containers(): 
   if c.name == container_name:
        for blob in block_blob_service.list_blobs(c.name):
            if location in blob.name:
                print(blob.name)
                block_blob_service.get_blob_to_path(c.name,blob.name,'data')

i can't save the pdf files that have in the "folder" to the folder data.

The third parameter of get_blob_to_path should be the file path and not the directory. if './data' is an existing directory, get_blob_to_path will try to write a file into a directory which explains the Permission denied error. Try with something like this:

import os
block_blob_service = BlockBlobService(account_name='hi',account_key='hi') 
tenant = 'hi'
container_name = 'files'
id_ = 1
upload_id = 'upload'+'/'+str(id_)
location = 'local'+'/'+tenant+'/'+upload_id+'/'

for c in block_blob_service.list_containers(): 
    if location in blob.name:
        print(blob.name)
        path_to_file = "data/" + blob.name
        dir = os.path.dirname(path_to_file)
        os.makedirs(dir, exist_ok=True)
        # path_to_file should change between loop iterations
        block_blob_service.get_blob_to_path(c.name,blob.name,'data')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM