-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.py
More file actions
51 lines (37 loc) · 2.01 KB
/
process.py
File metadata and controls
51 lines (37 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import requests
import pdf2image
import tempfile
import os
import sys
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
def upload_file_to_blob(file_path, container_name, blob_name, connection_string):
# Create a BlobServiceClient using the connection string
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Get a reference to the container and create it if it doesn't exist
container_client = blob_service_client.get_container_client(container_name)
if not container_client.exists():
container_client.create_container()
# Create a BlobClient to interact with the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
# Upload the file
with open(file_path, "rb") as data:
blob_client.upload_blob(data, overwrite=True)
print(f"File {file_path} uploaded to {container_name}/{blob_name}")
def upload_images_to_blob(images, container_name, connection_string):
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
for index, image in enumerate(images):
# Create a temporary file for the image
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_image:
image.save(temp_image, format="JPEG")
temp_image_path = temp_image.name
# Define a unique blob name
blob_name = f"uploaded_image_{index}.jpg"
# Upload the temporary image file to Azure Blob Storage
upload_file_to_blob(temp_image_path, container_name, blob_name, connection_string)
# Delete the temporary file
os.remove(temp_image_path)
print(f"Image {index} uploaded to {container_name}/{blob_name}")
def process_file(url, image_container_name, image_connection_string):
response = requests.get(url)
images = pdf2image.convert_from_bytes(response.content)
upload_images_to_blob(images, image_container_name, image_connection_string)