0

I have an api to file upload on Bluemix Object Storage by using request module. All are good but there is some unwanted character which append automatically.

example:  
--38oi85df-b5d1-4d42-81ce-c547c860b512 //this is unwanted character
  Email 
 abc@gmail.com
 hsl@gmsl.com 
 pjeyjle@cse.com
--38oi85df-b5d1-4d42-81ce-c547c860b512-- // this is unwanted character

Here is my code:-

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); 
4

2 回答 2

1

您在请求中发送带有 preambleCRLF 和 postambleCRLF 的多部分消息,这导致了这些行。

您应该使用 pkgcloud 库将数据上传到对象存储:

https://github.com/pkgcloud/pkgcloud

下面是使用 pkgcloud 和 Bluemix 上的对象存储服务的示例(来自 VCAP 的凭证)。

(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);
于 2016-06-02T20:56:05.177 回答
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); 
于 2016-06-03T04:59:41.153 回答