久久久久久久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:“浮動對象不可下標)
主站蜘蛛池模板: 日本污视频 | 中文字幕一区二区三区在线观看 | 欧美一级黄色网 | 成人免费视频一区二区 | 国产精品久久国产精品久久 | 在线欧美a | 亚洲国产成人av好男人在线观看 | 国产精品性做久久久久久 | 毛片一级网站 | 免费二区| 青青草这里只有精品 | 综合国产在线 | 91亚洲精华国产 | 蜜桃精品视频在线 | 欧美成人a| 一a一片一级一片啪啪 | 91精品一区二区 | 国内成人免费视频 | 一级片在线视频 | 亚洲最大的成人网 | 久久久精品久久久 | 国产一区二区三区免费观看在线 | 久久久网 | 精品中文字幕视频 | 九九热这里 | 久久久亚洲 | 福利视频网站 | 91久久综合 | 日日摸天天添天天添破 | 成人毛片一区二区三区 | 一级一片在线观看 | 国产成人午夜高潮毛片 | 久久视频精品 | 欧美一级视频在线观看 | 国产视频精品在线观看 | 国产成人在线免费 | 成人av高清| 狠狠的干狠狠的操 | 国产精品区二区三区日本 | 高清一区二区三区 | 久久国产欧美日韩精品 |