久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python - 在 FTP 中按塊上傳內存文件(由 API 調用生成

Python - Upload a in-memory file (generated by API calls) in FTP by chunks(Python - 在 FTP 中按塊上傳內存文件(由 API 調用生成))
本文介紹了Python - 在 FTP 中按塊上傳內存文件(由 API 調用生成)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要能夠在 Python 中通過 FTP 和 SFTP 上傳文件,但有一些不太常見的限制.

I need to be able to upload a file through FTP and SFTP in Python but with some not so usual constraints.

  1. 文件不得寫入磁盤.

  1. File MUST NOT be written in disk.

文件的生成方式是調用 API 并將 JSON 格式的響應寫入文件.

The file how it is generated is by calling an API and writing the response which is in JSON to the file.

API 有多次調用.無法通過一次 API 調用來檢索整個結果.

There are multiple calls to the API. It is not possible to retrieve the whole result in one single call of the API.

我無法將完整的結果存儲在字符串變量中,方法是執行所需的多次調用并附加到每個調用中,直到我將整個文件存儲在內存中.文件可能很大并且存在內存資源限制.應該發送每個塊并釋放內存.

I can not store in a string variable the full result by doing the multiple calls needed and appending in each call until I have the whole file in memory. File could be huge and there is a memory resource constraint. Each chunk should be sent and memory deallocated.

所以這里有一些我想要的示例代碼:

So here some sample code of what I would like to:

def chunks_generator():
    range_list = range(0, 4000, 100)
    for i in range_list:
        data_chunk = requests.get(url=someurl, url_parameters={'offset':i, 'limit':100})
        yield str(data_chunk)

def upload_file():
    chunks_generator = chunks_generator()
    for chunk in chunks_generator:
        data_chunk= chunk
        chunk_io = io.BytesIO(data_chunk)
        ftp = FTP(self.host)
        ftp.login(user=self.username, passwd=self.password)
        ftp.cwd(self.remote_path)
        ftp.storbinary("STOR " + "myfilename.json", chunk_io)

我只想要一個附加了所有塊的文件.我已經擁有并且正在工作的是,如果我將整個文件保存在內存中并像這樣立即發送它:

I want only one file with all the chunks appended. What I have already and works is if I have the whole file in memory and send it at once like this:

string_io = io.BytesIO(all_chunks_together_in_one_string)
ftp = FTP(self.host)
ftp.login(user=self.username, passwd=self.password)
ftp.cwd(self.remote_path)
ftp.storbinary("STOR " + "myfilename.json", string_io )

獎金

我在 ftplib 中需要它,但在 Paramiko 中也需要它用于 SFTP.如果有任何其他庫可以更好地工作,我是開放的.

I need this in ftplib but will need it in Paramiko as well for SFTP. If there are any other libraries that this would work better I am open.

如果我需要壓縮文件怎么辦?我可以壓縮每個塊并一次發送 zip-chunked 塊嗎?

How about if I need to zip the file? Can I zip each chunk and send the zip-chunked chunk at a time?

推薦答案

可以實現類文件類,調用.read(blocksize)方法從requests 對象.

You can implement file-like class that upon calling .read(blocksize) method retrieves data from requests object.

類似這樣的東西(未經測試):

Something like this (untested):

class ChunksGenerator:
    i = 0
    requests = None

    def __init__(self, requests)
        self.requests = requests

    def read(self, blocksize):
        # TODO: somehow detect end-of-file and return false in that case
        buf = requests.get(
                  url=someurl, url_parameters={'offset':self.i, 'limit':blocksize})
        self.i += blocksize
        return buf

generator = ChunksGenerator(requests)
ftp.storbinary("STOR " + "myfilename.json", generator)

<小時>

使用 Paramiko,您可以使用與 SFTPClient.putfo 方法.

這篇關于Python - 在 FTP 中按塊上傳內存文件(由 API 調用生成)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 91欧美| 日本a∨视频 | 久久久精品一区 | 少妇淫片aaaaa毛片叫床爽 | 欧产日产国产精品国产 | 日韩精品一区二区三区视频播放 | 在线观看免费毛片 | 黄色国产大片 | 日韩亚洲视频在线 | 亚洲国产乱码 | 精品熟人一区二区三区四区 | 日韩一级免费观看 | 婷婷不卡 | 国产精品大片在线观看 | 欧美日韩国产精品一区 | 一区二区三区精品视频 | 欧美色综合网 | 在线精品亚洲欧美日韩国产 | 欧美视频在线播放 | 色爱综合网 | 国内精品视频一区二区三区 | www.婷婷 | 羞视频在线观看 | 女同久久另类99精品国产 | 另类一区 | 精品亚洲一区二区三区四区五区高 | 成人中文字幕在线 | 日韩欧美精品在线 | 精品中文字幕久久 | 围产精品久久久久久久 | 久久i | 国产98色在线 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 亚洲一区免费 | 国产福利二区 | 国产午夜精品久久久 | 亚洲精品不卡 | av网站在线免费观看 | 国产中文区二幕区2012 | a级黄色片在线观看 | 国产视频精品免费 |