簡體   English   中英

如何在Bluemix對​​象存儲中轉儲joblib或pickle文件?

[英]How do I dump a joblib or pickle file in Bluemix Object Storage?

我正在使用在Bluemix上運行Flask的Python應用程序。 我知道如何使用swiftclient模塊的對象存儲來創建容器並在其中保存文件,但是如何轉儲其中包含的joblib或pickle文件? 如何在我的Python程序中加載它?

這是存儲簡單文本文件的代碼。

import swiftclient

app = Flask(__name__)
CORS(app)


cloudant_service = json.loads(os.environ['VCAP_SERVICES'])['Object-Storage'][0]
objectstorage_creds = cloudant_service['credentials']

if objectstorage_creds:
   auth_url = objectstorage_creds['auth_url'] + '/v3' #authorization URL
   password = objectstorage_creds['password'] #password
   project_id = objectstorage_creds['projectId'] #project id
   user_id = objectstorage_creds['userId'] #user id 
   region_name = objectstorage_creds['region'] #region name 

def predict_joblib():
  print('satart')
  conn = swiftclient.Connection(key=password,authurl=auth_url,auth_version='3',os_options={"project_id": project_id,"user_id": user_id,"region_name": region_name})
  container_name = 'new-container'

  # File name for testing
  file_name = 'requirment.txt'

  # Create a new container
  conn.put_container(container_name)
  print ("nContainer %s created successfully." % container_name)

  # List your containers
  print ("nContainer List:")
  for container in conn.get_account()[1]:
    print (container['name'])

  # Create a file for uploading
  with open(file_name, 'w') as example_file:
    conn.put_object(container_name,file_name,contents= "",content_type='text/plain')

  # List objects in a container, and prints out each object name, the file size, and last modified date
  print ("nObject List:")
  for container in conn.get_account()[1]:
    for data in conn.get_container(container['name'])[1]:
      print ('object: {0}t size: {1}t date: {2}'.format(data['name'], data['bytes'], data['last_modified']))

  # Download an object and save it to ./my_example.txt
  obj = conn.get_object(container_name, file_name)
  with open(file_name, 'w') as my_example:
    my_example.write(obj[1])
  print ("nObject %s downloaded successfully." % file_name)




@app.route('/')
def hello():
    dff = predict_joblib()
    return 'Welcome to Python Flask!'

@app.route('/signUp')
def signUp():
    return 'signUp'


port = os.getenv('PORT', '5000')
if __name__ == "__main__":
    app.debug = True
    app.run(host='0.0.0.0', port=int(port))

由於file.openpickle.dumps返回python docs上的字節對象:

pickle.dumps(obj,protocol = None,*,fix_imports = True)將對象的pickled表示作為bytes對象返回,而不是將其寫入文件。

open(name [,mode [,buffering]])打開一個文件,返回文件對象一節中描述的文件類型的對象。 如果無法打開文件,則引發IOError。 打開文件時,最好使用open()而不是直接調用文件構造函數。

您可以直接處理要存儲為obj

# Create a file for uploading
file = pickle.dumps(obj)
conn.put_object(container_name,file,contents= "",content_type='application/python-pickle')

內容類型的這種變化是由http協議中的標准引起的。 這是我從另一個問題得到的,請檢查。 就像聲明的那樣:

這是事實上的標准。 RFC2046規定:4.5.3。 其他應用程序子類型預計未來將定義“應用程序”的許多其他子類型。 MIME實現必須至少將任何未識別的子類型視為等同於“application / octet-stream”。 因此,對於非pickle-aware系統,流將看起來像任何其他八位字節流,但對於啟用pickle的系統,這是至關重要的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM