将tarfile读入BytesI

2024-09-28 20:56:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Docker-pyAPI来处理和操作Docker容器。在API中,put_archive()函数期望data字段以字节为单位。 因此,使用tarfile库,我有:

import tarfile
import io

container = client.create_container(image="myimage", command="/bin/bash")
source = "~/.ssh/id_rsa.pub"
tarfile = create_tar_file(path=source, name="keys.tar")
# tarfile = "keys.tar"
# How can I read the tar file was a BytesIO() object?
data = io.BytesIO()
client.put_archive(container=container, path="/tmp", data=data)

API说:

put_archive(container, path, data)

    Insert a file or folder in an existing container using a tar archive as source.

Parameters:

container (str) – The container where the file(s) will be extracted.
path (str) – Path inside the container where the file(s) will be extracted. Must exist.
data (bytes) – tar data to be extracted

Returns: True if the call succeeds.

Return type: (bool)

Raises: docker.errors.APIError – If the server returns an error.


我的问题是:

如何将tar文件作为BytesIO()读取,以便将其传递给put_archive()函数?


Tags: thepathdockerapisourcedataputcontainer