2016-05-31 9 views
0

Bluemixオブジェクト保存にアップロードするファイルは、requestモジュールです。すべてが良いですが、自動的に追加するいくつかの不要な文字があります。nodejs put要求に不要な文字を自動的に追加します

example: 
--38oi85df-b5d1-4d42-81ce-c547c860b512 //this is unwanted character 
    Email 
[email protected] 
[email protected] 
[email protected] 
--38oi85df-b5d1-4d42-81ce-c547c860b512-- // this is unwanted character 

はここに私のコードです: -

import request from 'request'; 

exports.putObjectStorageFile = function(authToken, file, csv, cb) { 
var s = new stream.Readable(); 
s._read = function noop() {}; 
s.push(csv); //csv is string 
s.push(null); 
var options = { 
    url: 'https://xxxx.objectstorage.open.xxxx.com/v1/AUTH_' + config.objectStorage.projectId + '/xxxx/' + file, 
    method: 'PUT', 
    preambleCRLF: true, 
    postambleCRLF: true, 
    encoding: 'utf-8', 
    headers: { 
     'Content-Type': 'text/html; charset=UTF-8', 
     'Content-Length': 1, 
     'X-Auth-Token': authToken 
    }, 
    multipart: { 
     chunked: false, 
     data: [ 
     { body: s } 
     ] 
    } }; 

    function callback(error, response) { 
    if (error) cb(error); 
    if (!error && response.statusCode == 201) { 
     cb(null); 
    } 
    } 
request(options, callback); 

答えて

0

マルチパートを使用してデータを送信しています。

var options = { 
    url: 'https://dal.objectstorage.open.softlayer.com/v1/AUTH_' + config.objectStorage.projectId + '/nrich-storage/' + file, 
    method: 'PUT', 
    headers: { 
     'Content-Type': 'text/csv', 
     'Content-Length': csv.length, 
     'X-Auth-Token': authToken 
    }, 
    body:csv 
    }; 

    function callback(error, response) { 
    if (error) cb(error); 
    if (!error && response.statusCode == 201) { 
     cb(null); 
    } 
    } 

request(options, callback); 
+0

私たちはzipファイルをアップロードできますか?コンテンツタイプ、コンテンツの長さを追加して本文にデータを送信することで解決策を見つけましたか? –

1

あなたはそれらの行を引き起こしているあなたのリクエストでpreambleCRLFとpostambleCRLFでマルチパートメッセージを送っています。

あなたがストレージをオブジェクトにデータをアップロードするためpkgcloudライブラリを使用する必要があります。以下は

https://github.com/pkgcloud/pkgcloud

はBluemix(VCAPから資格情報)にオブジェクトストレージサービスとpkgcloudを使用してのサンプルです。

(function (module) { 
    var pkgcloud = require('pkgcloud'), 
     fs = require('fs'); 

    function ObjectStorage(container, credentials) { 
     this.container = container; 

     this.config = { 
      provider: 'openstack', 
      useServiceCatalog: true, 
      useInternal: false, 
      keystoneAuthVersion: 'v3', 
      authUrl: credentials.auth_url, 
      tenantId: credentials.projectId, 
      domainId: credentials.domainId, 
      username: credentials.username, 
      password: credentials.password, 
      region: credentials.region 
     }; 

     this.client = pkgcloud.storage.createClient(this.config); 
    } 

    ObjectStorage.prototype.validate = function() { 
     return new Promise(function (resolve, reject) { 
      this.client.auth(function (error) { 
       if (error) { 
        return reject(error); 
       } 

       resolve(); 
      }); 
     }.bind(this)); 
    }; 

    ObjectStorage.prototype.makeContainer = function() { 
     return new Promise(function (resolve, reject) { 

      this.client.createContainer({name: this.container}, function (error) { 
       if (error) { 
        return reject(error); 
       } 

       return resolve(); 
      }); 

     }.bind(this)); 
    }; 

    ObjectStorage.prototype.uploadFile = function (path, name) { 
     return new Promise(function (resolve, reject) { 

      var myPicture = fs.createReadStream(path); 

      var upload = this.client.upload({ 
       container: this.container, 
       remote: name 
      }); 

      upload.on('error', function (error) { 
       reject(error); 
      }); 

      upload.on('success', function (file) { 
       resolve(file); 
      }); 

      myPicture.pipe(upload); 
     }.bind(this)); 
    }; 

    module.exports = ObjectStorage; 
})(module); 
関連する問題