You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Blob存储上传文件损坏问题排查与修复建议咨询

问题原因与解决方案

你的核心问题是Google Drive公开文件的默认分享链接返回的是网页内容而非原始文件字节,导致上传到Azure Blob的是HTML代码而非目标文件,最终出现损坏无法打开的情况。以下是无需本地下载的代码修改方案:

关键修改点

  1. 构造Google Drive直接下载URL:公开文件需使用https://drive.google.com/uc?export=download&id={file_id}格式,而非普通分享链接。
  2. 流式传输文件内容:通过请求流直接将文件数据传递给Azure Blob,避免内存占用和本地存储。

修改后的Python代码示例

import requests
from azure.storage.blob import BlobServiceClient

def upload_gdrive_to_azure(gdrive_file_id, azure_connection_string, container_name, blob_name, content_type):
    # 构造Google Drive直接下载URL
    gdrive_download_url = f"https://drive.google.com/uc?export=download&id={gdrive_file_id}"
    
    # 流式请求获取文件内容
    with requests.get(gdrive_download_url, stream=True) as response:
        response.raise_for_status()  # 检查请求是否成功
        
        # 处理大文件的下载确认令牌(若有)
        for key, value in response.cookies.items():
            if key.startswith('download_warning'):
                gdrive_download_url = f"{gdrive_download_url}&confirm={value}"
                response = requests.get(gdrive_download_url, stream=True)
                break
        
        # 初始化Azure Blob客户端并上传流
        blob_service_client = BlobServiceClient.from_connection_string(azure_connection_string)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
        blob_client.upload_blob(data=response.raw, content_type=content_type, overwrite=True)

# 调用示例
gdrive_file_id = "你的Google Drive文件ID"
azure_conn_str = "你的Azure存储连接字符串"
container = "目标容器名"
blob_name = "上传后的文件名(带正确扩展名)"
content_type = "文件对应的MIME类型,比如image/jpeg、application/pdf"

upload_gdrive_to_azure(gdrive_file_id, azure_conn_str, container, blob_name, content_type)

额外注意事项

  • 确保content_type设置准确,比如.pdf对应application/pdf,.png对应image/png,这会直接影响Azure Blob的预览和下载行为。
  • 若遇到权限相关错误,需确认Google Drive文件确实设置为公开可访问(任何人可查看/下载)。

内容的提问来源于stack exchange,提问作者Gaurav Bahuguna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 10:25:08