LoginSignup
0
0

More than 5 years have passed since last update.

Bluemix の Cloudant に Node.js の cradle で画像ファイルをアップロード

Last updated at Posted at 2017-07-26

次のページで行うことと同じことを、Node.js の cradle を使って行いました。

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

cradle についての情報は、

flatiron/cradle

ダウンロードのサンプルは、こちら。

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

to_cloudant.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  to_cloudant.js
//
//                  Jul/27/2017
//
// ---------------------------------------------------------------
var cradle = require ('cradle')
var fs = require('fs')
// ---------------------------------------------------------------
console.log ("*** 開始 ***")
//
const keys = ["DSCF0001","DSCF0002"]
//
const service_username = '44b508dd-f332-4f91-81f2-78369c7d29d9-bluemix'
const service_password = 'de44b508dde666c2a5c16596fc3713762e08a61e7132d3b21ecaaf1f63a835fae64'
const service_host = 'efc189dc-f332-4f91-81f2-785129c7d29d9-bluemix.cloudant.com'
const service_port = '443'

const options = {
    cache : true,
    raw : false,
    secure : true,
    auth : {
        username : service_username,
        password : service_password
        }
}

var cc = new (cradle.Connection)(service_host, service_port, options)

const db_name = 'jpg'
var db = cc.database (db_name)

db.exists (function (err, res_exists)
    {
    if (err)
        {
        console.log('error', err)
        }
    else
        {
        if (res_exists)
            {
            db.destroy (function (err,res)
                {
                console.log ("*** db is destroyed ***")
                db_create_proc (db,keys)
                })
            }
        else
            {
            db_create_proc (db,keys)
            }
        }
    })

    console.log ("*** 終了 ***")
// ---------------------------------------------------------------
// [4]:
function db_create_proc (db,keys)
{
    db.create(function (err,doc)
        {
        for (var it in keys)
            {
            const key = keys[it]
            data_input_proc (db,key)
            }
        })
}

// ---------------------------------------------------------------
// [4-4]:
function data_input_proc (db,key)
{
    const file_jpg = key + '.jpg'

    console.log (key)

    const image = fs.readFileSync (file_jpg)

    const attachmentData = {
        name: file_jpg,
        'Content-Type': 'image/jpeg',
        body: image
        }

    db.saveAttachment (key,attachmentData,function (err,res)
        {
        console.log (err)
        console.log (res)
        })
}

// ---------------------------------------------------------------
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