3

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

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

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

4

3 回答 3

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))
于 2019-03-20T00:55:26.270 回答
4

我遇到了类似的情况:尝试将 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
)

.seek(0)为了让 minio 能够插入正确数量的字节,我必须使用它。

于 2019-09-21T23:30:55.487 回答
2

@gcharbon:这个解决方案对我不起作用。client.put_object()只接受对象之类的字节。

这是我的解决方案:

from minio import Minio 
import pandas as pd
import io  

#Can use a string with csv data here as well
csv_bytes = df.to_csv().encode('utf-8')

csv_buffer = io.BytesIO(csv_bytes)

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

client.put_object("bucketname", 
                  "objectname",  
                  data=csv_buffer, 
                  length=len(csv_bytes), 
                  content_type='application/csv')
于 2020-03-09T08:28:36.867 回答