tinify / tinify-nodejs

Node.js client for the Tinify API.
https://tinypng.com/developers
MIT License
414 stars 74 forks source link

Making running Tinify with AWS S3 at buffer level #2

Closed afaucogney closed 8 years ago

afaucogney commented 8 years ago

Here is the code I use without succes.

The store does work, but I do not want save every file on my computer to them uploading them again.... I will deploy this on a Heroku dyno when it will work.

var tinify = require("tinify");
var s3 = require('s3');

if (!process.env.TINIFY_KEY) {
    var env = require('../env.js')
}

tinify.key = process.env.TINIFY_KEY;

var client = s3.createClient({
    s3Options: {
        accessKeyId    : process.env.AWS_KEY,
        secretAccessKey: process.env.AWS_SECRET,
        region         : process.env.AWS_REGION
    }
});

var listingParams = {
    s3Params: {
        Bucket : process.env.BUCKET,
        Prefix : "bottles/labelPictures/",
        MaxKeys: 1
    }
};

var lister = client.listObjects(listingParams);

lister.on('error', function (err) {
    console.error("unable to list:", err.stack);
});

//lister.on('progress', function () {
//    console.log("progress", lister.progressAmount, lister.progressTotal, lister.dirsFound, lister.objectsFound);
//});

lister.on('end', function () {
    console.log("done listing");
});

lister.on('data', function (data) {
    console.log("list data: ");

    var filename = getKeyFromPath(data.Contents[0].Key);
    var remotePath = data.Contents[0].Key;
    //var localPath = "local/" + filename;

    console.log("FileName:" + filename);
    console.log("RemotePath:" + remotePath);
    //console.log("LocalPath:" + localPath);

    var downloadBufferParams = {
        Bucket: process.env.BUCKET,
        Key   : remotePath
    };

    var downloader = client.downloadBuffer(downloadBufferParams);

    downloader.on('error', function (err) {
        console.error("unable to download:", err.stack);
    });

    //downloader.on('progress', function () {
    //    console.log("buffer download progress for", filename, downloader.progressAmount, downloader.progressTotal);
    //});
    downloader.on('end', function (buffer) {
        console.log("done buffer downloading for", filename);

        tinify.fromBuffer(buffer).store({
            service              : "s3",
            aws_access_key_id    : process.env.AWS_KEY,
            aws_secret_access_key: process.env.AWS_SECRET,
            region               : process.env.AWS_REGION,
            path                 : process.env.BUCKET + "/essai/" + filename
        });
    });

});

var getKeyFromPath = function (key) {
    return key.replace(/^.*[\\\/]/, '');
};

I get this error about file aspect.

Unhandled rejection ClientError: File is empty (HTTP 400/InputMissing) at Error.ClientError (/Users/anthonyfaucogney/git/ImageOptimizer/node_modules/tinify/lib/tinify/Error.js:23:9) at Function.Error.create (/Users/anthonyfaucogney/git/ImageOptimizer/node_modules/tinify/lib/tinify/Error.js:59:10) at IncomingMessage. (/Users/anthonyfaucogney/git/ImageOptimizer/node_modules/tinify/lib/tinify/Client.js:77:31) at IncomingMessage.emit (events.js:129:20) at _stream_readable.js:908:16 at process._tickDomainCallback (node.js:381:11)

Feel free to add my code to improve my debug visibility.

rolftimmermans commented 8 years ago

Thanks for reporting this! The code looks correct, this should totally work. Could you please check the following?

What is the version of the Tinify client you are using? Try npm list | grep tinify. The current version is 1.1.1.

Is the buffer actually empty? Maybe try the code with the following line:

downloader.on('end', function (buffer) {
    console.log(buffer);
    console.log("done buffer downloading for", filename);
    // ...
afaucogney commented 8 years ago

I have the good tinnily version. The buffer is not empty.

Nothing has been changed, but it is now working. Maybe some workload on your side or on was…

Is there a way to know progress when we use store :

With this, we may track issues in the workflow.

Le 22 janv. 2016 à 20:08, Rolf Timmermans notifications@github.com a écrit :

Thanks for reporting this! The code looks correct, this should totally work. Could you please check the following?

What is the version of the Tinify client you are using? Try npm list | grep tinify. The current version is 1.1.1.

Is the buffer actually empty? Maybe try the code with the following line:

downloader.on('end', function (buffer) { console.log(buffer); console.log("done buffer downloading for", filename); // ... — Reply to this email directly or view it on GitHub https://github.com/tinify/tinify-nodejs/issues/2#issuecomment-174014519.

rolftimmermans commented 8 years ago

Thanks for checking. This is an interesting result. I'm happy that it works now, but we will investigate to see if there is any issue on our side that may cause 400 (InputMissing) errors instead of 500 errors.

Upload & compression happens directly after each other. You can use the core Node HTTP library to report upload progress events, which will tell you when the upload has arrived completely at our servers.

We don't offer that as a feature in the client, though. But feel free to copy and reuse code from our client to implement it if you want.

We don't report any other progress events back yet, but we generally try to make failures as clear as possible through our use of HTTP status codes. This is reflected in the exception classes in the client, which are of type AccountError/ClientError/ServerError/ConnectionError.