前提・実現したいこと
node.jsでHTTPメソッドを使って、conohaオブジェクトストレージに画像をアップロードさせたいです。
今はクライアントからmulterで画像データを受け取り、HTTPメソッドであるrequestでアップロードする流れにしています。
以下参考にしたサイト
1.オブジェクトストレージを使う(RESTful API [curl] 編)
https://support.conoha.jp/v/objectstoragecurl/?btn_id=v-objectstoragecyberduck-sidebar_v-objectstoragecurl
2.Conohaのオブジェクトストレージを使ってみた(REST API Ver)
https://dai-yamashita.hatenablog.jp/entry/20141103/1415038231
発生している問題・エラーメッセージ
オブジェクトストレージの認証トークンを取得するところまでできましたが、画像のPOSTがうまくいきません。
ステータスコードが204で、リクエスト自体は通ってそうです。
解決策がもしあればご教授いただきたいです。
他に必要な情報があれば、ぜひ提示させていただきます。
{"statusCode":204,"body":"","headers":{"content-length":"0","content-type":"text/html; charset=UTF-8","x-trans-id":"xxxxxxxxxxxxxx","date":"Fri, 15 Oct 2021 15:11:59 GMT","connection":"close"}
該当のソースコード
node
1const multer = require('multer'); 2const upload = multer(); 3 4// ここからtoken取得のためのコード 5const _ = require('lodash'); 6const httpRequest = require('request'); 7 8const tenantId = 'xxxxxxxxxxx'; 9const username = 'xxxxxxxxxxx'; 10const password = 'xxxxxxx'; 11 12const api_url = 'https://identity.tyo2.conoha.io/v2.0'; 13 14const params = { 15 "url": api_url+'/tokens', 16 "headers": { 17 "Accept": 'application/json' 18 }, 19 "form": JSON.stringify({ 20 "auth": { 21 "passwordCredentials": { 22 "username": username, 23 "password": password 24 }, 25 "tenantId": tenantId 26 } 27 }) 28 29}; 30 31 32app.post('/api/update/communityimage', upload.single('file'), 33 (req, res) => { 34 const file = req.file; 35 36 // tokenの取得 37 httpRequest.post(params, function(err, result, body){ 38 if(_.isString(body)){ 39 const res = JSON.parse(body); 40 const token = res.access.token.id; 41 const token_expire = res.access.token.expires; 42 console.log('token: ' + token); 43 console.log('expire: ' + token_expire); 44 45 const headers = { 46 'X-Auth-Token': token, 47 'content-type': 'multipart/form-data' 48 }; 49 50 // アップロード 51 const options = { 52 url: 'https://object-storage.tyo2.conoha.io/v1/xxxxxxxxxxx/コンテナ', 53 method: 'POST', 54 headers: headers, 55 formData: file 56 } 57 58 httpRequest(options, function (error, result, body) { 59 console.log(`body: ${body}`); 60 console.log(`result: ${JSON.stringify(result)}`) 61 console.log(`resultStatus: ${result.statusCode}`) 62 console.log(`resultHeaders: ${JSON.stringify(result.headers)}`) 63 console.log(`error: ${error}`); 64 }) 65 } else{ 66 const res = body; 67 console.log(res); 68 } 69 }) 70}); 71
試したこと
requestのgetメソッドでオブジェクトストレージへの接続は確認できました。
node
1 // オブジェクトストレージへの接続 2 const headers = { 3 'X-Auth-Token': token, 4 'Accept': 'application/json' 5 }; 6 7 const options = { 8 url: 'https://object-storage.tyo2.conoha.io/v1/xxxxxxxxxx/コンテナ', 9 headers: headers 10 } 11 12 httpRequest.get(options, function (error, result, body) { 13 console.log(`resultStatus: ${result.statusCode}`) 14 })
resultStatus: 200

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。