LoginSignup
0
0

More than 5 years have passed since last update.

Bluemix の Cloudant に python3 の requests で画像ファイルをアップロード

Posted at

次のページでは、pycurl を使っていますが、requests を使ってみました。
Bluemix の Cloudant に python3 で画像ファイルをアップロード

to_cloudant.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   to_cloudant.py
#
#                       Jul/30/2017
#
import sys
import requests
# --------------------------------------------------------------------
# [4]:
def file_upload_proc(url,key):
    print(key)
    jpg_in = key + ".jpg"
#
    fp_in = open(jpg_in,'rb')
    image = fp_in.read()
    fp_in.close()
#
    url_jpg_file = url + "/jpg/" + key + "/" + jpg_in
    headers = {'Content-type': 'image/jpeg'}
    try:
        rr=requests.put(url_jpg_file, data=image,headers=headers)
        print(rr)
        print(str(rr.content,'utf-8'))
        print()
        print(rr.headers)
    except Exception as ee:
        sys.stderr.write(str(ee))
#
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="https://44b508dd-f332-4f91-81f2-78369c7d29d9-bluemix:9dacdfdee131c2a5c16596fc371012e08a32e7132d3b21ebaad1f63a835fae64@efc189dc-f332-4f52-81f2-78369c7d17d9-bluemix.cloudant.com"
#
url_jpg = url + "/jpg"
rr=requests.delete(url_jpg)
print(rr)
print(str(rr.content,'utf-8'))
#
rr=requests.put(url_jpg)
print(rr)
print(str(rr.content,'utf-8'))
#
keys= ["DSCF0001","DSCF0002"]
#
for key in keys:
    file_upload_proc(url,key)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0