【问题标题】:python azure blob readinto with requests stream uploadpython azure blob readinto 请求流上传
【发布时间】:2020-11-09 18:06:00
【问题描述】:

我将视频保存在 azure blob 存储中,我想将它们上传到 Facebook。 Facebook 视频上传是一个多部分/表单数据发布请求。执行此操作的普通方法是使用 azure python sdk 中的 readall() 方法将 azure blob 下载为字节,并将其设置在 requests post data 中,如下所示。

# download video from azure blob
video = BlobClient.from_connection_string(AZURE_STORAGE_CONNECTION_STRING, 
                                          AZURE_CONTAINER_NAME,
                                          f"{folder_id}/{file_name}")
video = video.download_blob().readall()

# upload video to facebook
url = f"{API_VIDEO_URL}/{page_id}/videos"
params = {
    "upload_phase": "transfer",
    "upload_session_id": session_id,
    "start_offset": start_offset,
    "access_token": access_token
}

response = requests.post(url, params=params, files={"video_file_chunk": video})

文件的字节数被加载到内存中,这对较大的文件不利。 azure sdk readinto(stream) 中有一种方法可以将文件下载到流中。有没有办法连接请求streaming uploadreadinto() 方法。还是有其他方法可以直接从 Blob 存储上传文件?

【问题讨论】:

  • 你介意把文件保存在本地然后上传文件吗?
  • @JimXu No. 既不是本地文件也不是内存。我需要直接从 azure blob 传递到 facebook
  • 好的。我知道了。据我所知,我们可以在参数中使用file_url 和blob sas url。但它有一个限制,视频应该在 5 分钟内下载,并且它的大小不能超过 1GB。所以你的视频不能很大,网络应该很好:developers.facebook.com/docs/graph-api/reference/page/videos/…
  • 如果对你没有用,我认为我们需要将内容写入本地或内存流
  • @JimXu 因为我使用带有块的可恢复视频上传file_url 对我不起作用。所以我认为更好的方法是在内存中延迟加载。一次下载和上传一个块。

标签: python-3.x file-upload python-requests azure-blob-storage


【解决方案1】:

关于如何通过流分块上传视频,请参考以下代码

from azure.storage.blob import BlobClient
import io
from requests_toolbelt import MultipartEncoder
import requests

blob_poperties=blob.get_blob_properties()
blob_size=blob_poperties.size # the blob size
access_token=''
session_id='675711696358783'
chunk_size= 1024*1024 #the chunk size
bytesRemaining = blob_size
params = {
    "upload_phase": "transfer",
    "upload_session_id": session_id,
    "start_offset": 0,
    "access_token": access_token
}
url="https://graph-video.facebook.com/v7.0/101073631699517/videos"
bytesToFetch=0  
start=0 # where to start downlaoding 
while bytesRemaining>0 :
    
    with io.BytesIO() as f:
        if bytesRemaining < chunk_size:
           bytesToFetch= bytesRemaining
        else:
            bytesToFetch=chunk_size
        print(bytesToFetch)
        print(start)
        downloader =blob.download_blob(start,bytesToFetch)
        b=downloader.readinto(f)
        print(b)
        m = MultipartEncoder(
            fields={'video_file_chunk':('file',f) }
        )
        r =requests.post(url, params=params, headers={'Content-Type': m.content_type}, data=m)
        s=r.json()
        print(s)
        start =int(s['start_offset'])
        bytesRemaining -=int(s['start_offset'])
    params['start_offset']=start
    print(params)

# end uplaod
params['upload_phase']= 'finish'
r=requests.post(url, params=params)
print(r)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 2019-05-26
    • 2013-03-11
    • 1970-01-01
    • 2018-05-26
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多