Minio Python Client:直接上传字节

gue*_*tli 3 python minio

我阅读了 minio docs,我看到了两种上传数据的方法:

我想测试 minio 并上传我刚刚用numpy.random.bytes()创建的一些数据。

如何上传存储在python解释器中的变量中的数据?

gch*_*bon 7

我遇到了类似的情况:尝试将 pandas DataFrame 作为羽毛文件存储到 minio 中。我需要使用客户端直接存储字节Minio。最后代码看起来像这样:

from io import BytesIO
from pandas import df
from numpy import random
import minio

# Create the client
client = minio.Minio(
    endpoint="localhost:9000",
    access_key="access_key",
    secret_key="secret_key",
    secure=False
)

# Create sample dataset
df = pd.DataFrame({
    "a": numpy.random.random(size=1000),
})

# Create a BytesIO instance that will behave like a file opended in binary mode 
feather_output = BytesIO()
# Write feather file
df.to_feather(feather_output)
# Get numver of bytes
nb_bytes = feather_output.tell()
# Go back to the start of the opened file
feather_output.seek(0)

# Put the object into minio
client.put_object(
    bucket_name="datasets",
    object_name="demo.feather", 
    length=nb_bytes,
    data=feather_output
)
Run Code Online (Sandbox Code Playgroud)

我必须使用.seek(0)minio 才能插入正确数量的字节。

  • 只需将 `BytesIO()` 替换为 `StringIO()` 并使用 `to_csv()` 而不是 `to_feather()`。这应该可以解决问题 (2认同)

Ala*_*lan 5

看看io.BytesIO。这些允许您将字节数组包装在可以提供给 minio 的流中。

例如:

import io
from minio import Minio

value = "Some text I want to upload"
value_as_bytes = value.encode('utf-8')
value_as_a_stream = io.BytesIO(value_as_bytes)

client = Minio("my-url-here", ...) # Edit this bit to connect to your Minio server
client.put_object("my_bucket", "my_key", value_as_a_stream , length=len(value_as_bytes))
Run Code Online (Sandbox Code Playgroud)