我正在尝试将文件存储在Azure Blob存储中。我甚至仔细设置内容类型,即使记住扩展名,上传的项目仍然无法预览我也无法下载,它们看起来像损坏的文件。
from azure.storage.blob import BlobServiceClient,ContentSettings
import requests
import uuid
import re
import datetime
connection_string = "CONNECTION STRING"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client("test-data")
def getFileType(file_id):
api_key = 'GOOGLE_DRIVE_API_KEY'
# Replace 'file_id' with the file ID from the shareable link
# Construct the API URL for file metadata
url = f'https://www.googleapis.com/drive/v3/files/{file_id}?key={api_key}'
try:
response = requests.get(url)
if response.status_code == 200:
file_info = response.json()
file_type = file_info.get('mimeType')
return file_type
except Exception as e:
print(f"An error occurred: {e}")
def copy_image_to_blob(image_url, container_client):
try:
blob_name = datetime.datetime.now().strftime("%d%m%Y%H%M%S%f")
blob_client = container_client.get_blob_client(blob_name+".jpg")
response = requests.get(image_url, stream=True)
if response.status_code == 200:
if getFileType(re.search(r'/file/d/([^/]+)', image_url).group(1))=="image/jpeg":
blob_client = container_client.get_blob_client(blob_name+".jpg")
else:
blob_client = container_client.get_blob_client(blob_name+".pdf")
content_settings = ContentSettings(content_type=getFileType( re.search(r'/file/d/([^/]+)', image_url).group(1) ) )
blob_client.upload_blob(response.content, content_settings=content_settings)
return blob_client.url
except Exception as e:
return "Invalid urls"
def main(urls):
urlList=[]
for image_url in urls:
urlList.append( copy_image_to_blob(image_url, container_client) )
return urlList
if __name__=="__main__":
urls=['google_drive_public_url','google_drive_public_url','google_drive_public_url','google_drive_public_url']
print(main(urls=urls))
我正在使用正则表达式从文件URL中获取文件ID。我能够获得blob存储中文件的URL,但它们似乎已损坏。(我无法通过下载查看它们)。请提出一些修改意见,这样就可以了。此外,我不想下载的图像本地,然后sotre它的斑点存储,是的,我提供的驱动器链接是公共的,并有所有需要的权限。
1条答案
按热度按时间rm5edbpk1#
我在我的环境中复制,下面是我的预期结果:
如果您使用API Key访问文件,则您将获得文件的元数据,如下面所示,而不是实际数据:
The code which worked for me is :
Output: