Bluemix์˜ Object Storage ๋ฌธ์„œ๋ฅผ ๋จผ์ € ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ์ด ๋งํฌ๊ฐ€ ๊ณต์‹์ ์ธ ๋ฌธ์„œ์ด๋ฉฐ ์ด ํฌ์ŠคํŒ…์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—ฐ๋™๋ฒ•์— ๋Œ€ํ•ด์„œ๋งŒ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

3.1 ์„œ๋น„์Šค ์‹ ์ž„์ •๋ณด ์ƒ์„ฑํ•˜๊ธฐ

1) ์„œ๋น„์Šค ์‹ ์ž„์ •๋ณด ํƒญ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ์ƒˆ ์‹ ์ž„์ •๋ณด ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

2) ์ถ”๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

3) ์ƒ์„ฑ๋œ ์‹ ์ž„์ •๋ณด์˜ ์‹ ์ž„ ์ •๋ณด ๋ณด๊ธฐ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜์—ฌ ์ •๋ณด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

3.2 pkgCloud ์‚ฌ์šฉํ•˜์—ฌ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—ฐ๋™ํ•˜๊ธฐ

๋ธ”๋ฃจ๋ฏน์Šค์˜ ์˜ค๋ธŒ์ ํŠธ ์Šคํ† ๋ฆฌ์ง€๋Š” Openstack API Spec์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Openstack Swift๋ฅผ ์ง€์›ํ•˜๋Š” ๋‹ค์–‘ํ•œ library๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ์ค‘์—์„œ๋„ pkgCloud๋Š” node.js๊ธฐ๋ฐ˜์˜ ๋Œ€ํ‘œ์ ์ธ library์ž…๋‹ˆ๋‹ค.

๋จผ์ € pkgCloud๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฌธ์„œ ์ž‘์„ฑ์‹œ์—๋Š” 1.4.0(latest)์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

npm install pkgcloud --save

ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด cfenv์™€ dotenv๋„ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

"cfenv": "*",
"dotenv": "^2.0.0",

3.2.1 API Client ์ƒ์„ฑ

1) .env ํŒŒ์ผ์— credential ์ •๋ณด ์ž…๋ ฅ

์œ„์—์„œ ์ƒ์„ฑํ•œ ์„œ๋น„์Šค ์‹ ์ž„์ •๋ณด๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ๊ธฐ ๋“ฑ๋ก๋œ OS ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ฉ๋‹ˆ๋‹ค. Bluemix Object Storage๋Š” keystone ์ธ์ฆ์„ ์‚ฌ์šฉํ•˜๋ฉฐ v3์˜ ๋ฒ„์ „์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

# Object Storage Environment Variables
auth_url=https://lon-identity.open.softlayer.com
keystone_version=v3
project=object_storage_d21f009xxxxxxxxxxxxxxxxxxx
project_id=89fxxxxxxxxxxxxxxxxxxxxxx
region=london
user_id=ead08xxxxxxxxxxxxxxxxxxxx
user_name=admin_613759f6xxxxxxxxxxxxxxxxxxxxxxxxxxxx
password=xJH.xxxxxxxxxxxxxxxx
domain_id=41937b584b7344f58xxxxxxxxxxxxxx
domain_name=1111111
role=admin

2) Client ์ƒ์„ฑ
์œ„ ๋‹จ๊ณ„์—์„œ ์„ค์ •ํ•œ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ API Client๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ Console์— ์—๋Ÿฌ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค๋ฉด, credential ์ •๋ณด ์ค‘ ๊ฐ’์ด ์ž˜๋ชป ๋“ค์–ด๊ฐ”์„ ํ™•๋ฅ ์ด ๊ฐ€์žฅ ํฝ๋‹ˆ๋‹ค. pkgcloud๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธ์ฆ ํ† ํฐ ๊ด€๋ฆฌ๋ฅผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•Œ์•„์„œ ํ•ด์ฃผ๋ฏ€๋กœ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

'use strict';

const pkgcloud = require('pkgcloud');

let config = {
    provider: 'openstack',
    useServiceCatalog: true,
    useInternal: false,
    keystoneAuthVersion: process.env.keystone_version,
    authUrl: process.env.auth_url,
    tenantId: process.env.project_id,    //projectId from credentials
    domainId: process.env.domain_id,
    username: process.env.user_name,
    password: process.env.password,
    region: process.env.region   //dallas or london region
};

let client = pkgcloud.storage.createClient(config);

client.auth(err => {
	console.log(err);
})

3.2.2 Container ๋ฆฌ์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

pkgcloud์˜ Container api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Container ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

client.getContainers((err, containers) => {
		//implement code
	});

3.2.3 Container ์ƒ์„ฑํ•˜๊ธฐ

pkgcloud์˜ Container api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Container๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

client.createContainer("New Container Name", (err, container) => {
		//implement code
	});

3.2.4 Object ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ

pkagecloud์˜ File api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Object ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

client.getFiles("Container Name", (err, container) => {
		//implement code
	});

3.2.5 ํŒŒ์ผ ์—…๋กœ๋“œ ํ•˜๊ธฐ

multiparty๋Š” ์›น ํด๋ผ์ด์–ธํŠธ์—์„œ ์˜ฌ๋ผ์˜ค๋Š” multipart ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ multiparty๋ฅผ ํ™œ์šฉํ•˜์—ฌ Cloud๋กœ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

multiparty๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

  npm install multiparty --save

๋จผ์ € Client ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์ฐธ์กฐํ•˜์—ฌ file input์„ ๋ฐ›๋Š” html ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

<form encType="multipart/form-data">
	<div>
	      <label for="container_name">Container Name:<span class="ibm-required">*</span></label>
	      <span>
	        <input type="text" value="" size="40" id="container_name" name="container_name">
	      </span>
	 </div>
	 <div>
		<label for="object">File attachment:</label>
		<span>
		        <input id="object" type="file" data-widget="fileinput" data-multiple="false" />
		</span>
		<span class="ibm-item-note">Upload size limit (5 GB)</span>
	</div>
	<button class="ibm-btn-small ibm-btn-blue-50 ibm-btn-pri ibm-margin-top-1" type="button">OK</button>
</form>

Form ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•ฉ๋‹ˆ๋‹ค.

var input = document.querySelector(selector.overlay_object.input); // ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์„ ์œ„ํ•œ text input
var file = document.querySelector(selector.overlay_object.file); // ์—…๋กœ๋“œํ•  file input

var formData = new FormData();
formData.append("object", file.files[0]);

var qs = "container=" + input.value; 

var promise = new Promise(function(resolve, reject){
    var http = new XMLHttpRequest();
    http.open('POST', uri.object + "?" + qs, true);
    http.onreadystatechange = function() {
	if (http.readyState === 4 && http.status === 200 && http.responseText) {
	    resolve(JSON.parse(http.responseText));
	}
    };
    http.send(formData);
});

return promise;

์„œ๋ฒ„ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํŒŒ์ผ์„ multiparty๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํด๋ผ์šฐ๋“œ๋กœ ๋ฐ”๋กœ ์—…๋กœ๋“œ ํ•ฉ๋‹ˆ๋‹ค. multiparty๊ฐ€ ์ œ๊ณตํ•˜๋Š” part ๋Š” ReadableStram ํƒ€์ž…์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ pipeํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ ์—…๋กœ๋“œ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

const url = require('url');
const multiparty = require('multiparty');

let uploadObject = (req, res) => {
	let form = new multiparty.Form();
	let queryObject = url.parse(req.url,true).query; // query parameter๋กœ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•  container๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. 

	form.on('part', part => {
	    var upload = client.upload({
               container: queryObject.container,
               remote: part.filename
            });

            upload.on('error', function(err) {
               return res.status(err.code || 500).json(err.message);
            });
            upload.on('success', function(file) {
               return res.json({
                  status' : 'success'
               })
            });

	    part.pipe(upload);
	});

	form.parse(req);
}

3.3 ์ƒ˜ํ”Œ ์ฝ”๋“œ ๋ฐ ํ…Œ์ŠคํŠธ

์œ„ ๋‹จ๊ณ„์—์„œ ์„ค๋ช…ํ•œ ์ƒ˜ํ”Œ ์ฝ”๋“œ๋Š” ๋Œ์•„๊ฐ€๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Bluemix Object Storage Sample ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ๋ฅผ folkํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•˜์‹ญ์‹œ์˜ค.

pkgcloud ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๋” ๋‹ค์–‘ํ•œ api๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.