0
votes

I'm trying to upload a PDF to an Azure blob store, and then download and read it. The upload works fine, and when I open it in the Azure Storage Explorer, the file opens fine. However, when I try to download it, I get an Octet stream and I cant figure out how to convert it back into a PDF. Im doing this all through a Function App so I'm not sure if writing everything to a temporary file will help. I tried it and I got a corrupted pdf as my output. My code is as follows.

Upload:

blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = 'testblobstore123'

file = req.files['file']
try:
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=file.filename)
    blob_client.upload_blob(file)
except Exception as e:
    print(e)
    return "Unspecified Error"

Download:

blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = 'testblobstore123'
file = req.form['file']
blob_client = blob_service_client.get_blob_client(container=container_name, blob=file)
# data = blob_service_client.get_data_to_text(container_name, file)

data = blob_client.download_blob().readall()
1

1 Answers

0
votes

Firstly in my test, I open the file link or download it to local, the content-type is Octet stream, however they are able to read.

Below is my download code.

blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = 'test'
blob_name='nodejschinese.pdf'
download_file_path = os.path.join('D:', blob_name)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

with open(download_file_path, "wb") as download_file:
    download_file.write(blob_client.download_blob().readall())

Secondly if you want to change the content-type when y ou upload it, you could add ContentSettings in the upload_blob method.

Below is my upload code with ContentSettings.

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient,ContentSettings

connect_str='connection string'
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_name = 'test'
local_file_name='nodejs.pdf'
upload_file_path='E:\\nodejs.pdf'

blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
with open(upload_file_path, "rb") as data:
    blob_client.upload_blob(data,content_settings=ContentSettings(content_type='application/pdf'))

enter image description here