LoginSignup
0
1

More than 5 years have passed since last update.

Bluemix の Cloudant から、python3 で画像ファイルをダウンロード

Posted at

Cloudant にアップロードしたファイルを、requests を使ってダウンロードする方法です。
データは、jpg というデータベース以下に保存されているとします。
url は、アカウントに合わせて変更して下さい。

from_cloudant.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   from_cloudant.py
#
#                       Jul/26/2016
#
import sys
import json
import requests
# --------------------------------------------------------------------
# [6]:
def download_jpg_proc(url,key):
    file_jpg= key + ".jpg"
    url_jpg = url + "/jpg/" + key + "/" + file_jpg
    rr=requests.get(url_jpg)
#
    outfile = open(file_jpg,'wb')
    outfile.write(rr.content)
    outfile.close()
# --------------------------------------------------------------------
# [2]:
def list_keys_proc(url):
    url_all_docs = url + "/jpg/_all_docs"
    rr=requests.get(url_all_docs)
    print(rr.content)
    print()
    json_str=rr.content.decode('utf-8')
    print(json_str)
    dict_aa = json.loads(json_str)
    nnx = dict_aa['total_rows']
    print ("total_rows = %d" % nnx)
    keys = []
    for it in range(nnx):
        key = dict_aa['rows'][it]['key']
        keys.append(key)
#
    return keys
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="https://44b508dd-f332-4f91-81f2-78369c7d29d9-bluemix:9dacdfdee666c2a5c16596fc371012e08a61e7132d3b21ecaaf1f63a835f3356@efc189dc-f332-4f91-81f2-78369c7d29d9-bluemix.cloudant.com"
#
keys = list_keys_proc(url)
#
for key in keys:
    print(key)
    download_jpg_proc(url,key)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
0
1
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
1