2

I am trying to handle a image upload request from an iOS/Android app. In the request I am getting a buffer of the image and want to upload that to Rackspace files without having to download the image to re-upload it. I could write the file to file system and read from that but I want to know if it is possible to create a readableStream with the buffer in order to pipe that to cloud.

        var options = {
            container: _this3._container,
            remote: filename,
            contentType: contentType
        }
        var readStream = new Readable(data);
        readStream._read();
        readStream.on('data', function() {
            console.log("Has data!");
        });

    function upload() {
        return new Promise(function (resolve, reject) {
            var writeStream = _this3._RackClient.upload(options);

            writeStream.on('success', function() {
                resolve();
            });
            writeStream.on('error', function(err) {
                if (err !== null) {
                    return reject(err);
                }
            });
            readStream.pipe(writeStream);
        });
    }
    return upload();

Is how I am currently trying to do it but I continue to get not implemented errors.

mcclaskiem
  • 394
  • 1
  • 15

1 Answers1

0

I was actually able to achieve this using a PassThrough stream.

var options = {
    container: _this3._container,
    remote: filename,
    contentType: contentType
};
function upload() {
    return new Promise(function (resolve, reject) {
        var writeStream = _this3._RackClient.upload(options);
        var bufferStream = new stream.PassThrough();
        bufferStream.end(new Buffer(data));
        writeStream.on('success', function(file) {
            resolve(file);
        });
        writeStream.on('error', function(err) {
            if (err !== null) {
                console.log(err);
                return reject(err);
            }
        });
        bufferStream.pipe(writeStream);
    });
}
return upload();
mcclaskiem
  • 394
  • 1
  • 15