【问题标题】:Minio Python Client: Upload Bytes directlyMinio Python 客户端:直接上传字节
【发布时间】:2019-03-18 14:15:04
【问题描述】:

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

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

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

【问题讨论】:

    标签: python minio


    【解决方案1】:

    看看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))
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的情况:尝试将 pandas DataFrame 作为羽毛文件存储到 minio.xml 中。 我需要使用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 能够插入正确数量的字节。

      【讨论】:

      • 如果我想将其保存为 csv 文件。
      • 只需将BytesIO() 替换为StringIO() 并使用to_csv() 代替to_feather()。这应该可以解决问题
      【解决方案3】:

      @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')
      

      【讨论】:

        猜你喜欢
        • 2022-12-17
        • 2015-03-29
        • 2021-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-03
        • 2022-10-14
        • 2012-11-02
        相关资源
        最近更新 更多