久久久久久久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:“浮動對象不可下標)
主站蜘蛛池模板: 国产一区二区三区久久久久久久久 | 久久久精品久 | 日本小视频网站 | 日韩和的一区二区 | 高清国产午夜精品久久久久久 | 国产成人av电影 | 欧美αv | 伊人网综合在线观看 | 狠狠操av| 天天天天操 | 日日操操 | 99在线免费观看视频 | 综合激情av | 国产91久久久久久久免费 | 国产成人精品午夜 | 国产精品国产三级国产aⅴ中文 | 日韩精品在线网站 | 国产精品久久久久久久久图文区 | 欧美日韩在线播放 | 福利视频二区 | 国产乱码高清区二区三区在线 | 久久99精品久久 | 羞羞视频免费观看入口 | 日韩欧美精品在线 | 亚洲一区二区av在线 | 欧美一级久久 | 国产成人精品综合 | 精品欧美一区二区久久久伦 | 欧美一区二区二区 | 992tv人人草 久久精品超碰 | 日韩资源 | 国产精品区二区三区日本 | 亚洲成人免费视频在线 | 亚洲国产网站 | 一区二区高清不卡 | 日本特黄特色aaa大片免费 | 亚洲精品久久久久久一区二区 | 亚洲精品国产偷自在线观看 | 黄免费观看视频 | 欧美综合自拍 | 日韩久久综合 |