我正在使用一个Python应用程序,并在Bluemix上运行了一个烧瓶。我知道如何使用对象存储( Object )来创建容器并在其中保存文件,但是如何转储包含在容器中的joblib或泡菜文件?如何将其加载到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))发布于 2016-05-19 21:28:59
因为file.open和pickle.dumps都像python一样返回字节对象:
pickle.dumps(obj,protocol=None,*,fix_imports=True)将对象的腌制表示形式返回为字节对象,而不是将其写入文件。
打开(名称[,模式,缓冲])打开文件,返回节文件对象中描述的文件类型的对象。如果无法打开文件,则引发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”。因此,对于不感知泡菜的系统来说,流看起来像任何其他八位流,但是对于启用泡菜的系统来说,这是非常重要的信息。
https://stackoverflow.com/questions/37204323
复制相似问题