2

Dose the Kuzzle or Minio development teams have a working example of using the Kuzzle S3 plugin for Minio? I have the following but my file isnt being uploaded and the pre-signed url is referring to https://your-s3-bucket.s3.eu-west-3.amazonaws.com/

const fs = require("fs");
const fsPromises = require('fs').promises;

// Create a JS File object instance from a local path using Node.js
const fileObject = require("get-file-object-from-local-path");

// Promise based HTTP client for the browser and node.js
const axios = require('axios');

// Loads the Kuzzle SDK modules
const {
    Kuzzle,
    WebSocket
} = require('kuzzle-sdk');

var start = new Date();

const webSocketOptionsObject = {
    "autoReconnect": true,
    "ssl": true,
    "port": 443
};
const kuzzle = new Kuzzle(new WebSocket('myurl.com', webSocketOptionsObject));
const credentials = { username: 'xyz123', password: 'fithenmgjtkj' };


const path = __dirname + "\\" + "yellow_taxi_data.csv"; // the "\\" is for Windows path
var fileData = {};

// check file exists
fs.access(path, fs.F_OK, (err) => {
    if (err) {
        console.error(err)
        return
    }
    fileData = new fileObject.LocalFileData(path);

    // Adds a listener to detect connection problems
    kuzzle.on('networkError', error => {
        console.error('Network Error:', error);
    });
});

const connectToKuzzle = async () => {
    // Connects to the Kuzzle server
    await kuzzle.connect();
    return await kuzzle.auth.login('local', credentials);
    // console.log('jwt auth token: ', jwt);
}

const disConnectFromKuzzle = async () => {
    console.log('Disconnected from Kuzzle');
    kuzzle.disconnect();
    var time = new Date() - start;
    // sec = Math.floor((time/1000) % 60);
    console.log('Execution time in milliseconds: ', time);
}

const presignedURL = async () => {
    // Get a Presigned URL
    const result = await kuzzle.query({
        controller: 's3/upload',
        action: 'getUrl',
        uploadDir: 'proxybucket', // directory name inside the Bucket specified in the s3 plugin bucket name
        filename: fileData.name
    });

    console.log("result: ", result);
    return result;
}

const loadFileStream = async () => {
    console.log('getting file: ', path);
    targetFile = null;
    await fs.promises.readFile(path)
        .then(function (result) {
            console.log("file loaded------", result.length);
            targetFile = result;
        })
        .catch(function (error) {
            console.log(error);
            return;
        });

    return targetFile;
}

const kuzzleValidate = async (kuzzleResource) => {
    // console.log("kuzzleResource: ", kuzzleResource.result.fileKey);
    // validate
    // Validate and persist a previsously uploaded file.
    // https://docs.kuzzle.io/official-plugins/s3/2/controllers/upload/validate/
    const Presult = await kuzzle.query({
        // Kuzzle API params
        "controller": "s3/upload",
        "action": "validate",
        // File key in S3 bucket
        "fileKey": kuzzleResource.result.fileKey
    });
    console.log('validate: ', Presult.result.fileUrl);
}

const uploadFile = async (fileBuffer, kuzzleResource, jwt) => {
    // options at https://github.com/axios/axios
    const axiosOptions = {
        headers: {
            'Content-Type': fileData.type
        },
        maxBodyLength: 200000000 // 200,000,000 bytes 200 Mb
    };
    // PUT the fileBuffer to the Kuzzle S3 endpoint
    // https://github.com/axios/axios
    axios.defaults.headers.common['Authorization'] = jwt;
    const response = await axios.put(kuzzleResource.result.uploadUrl, fileBuffer, axiosOptions)
        .then((response) => {
            console.log('file uploaded......');
        })
        .catch(function (error) {
            console.log("File upload error: ", error);
            return;
        });

    return "Upload successful";
}



if (fileData) {
    connectToKuzzle().then((jwt) => {
        console.log(jwt);
        // upload(jwt);
        presignedURL().then((kuzzleResource) => {
            loadFileStream().then((fileBuffer) => {
                uploadFile(fileBuffer, kuzzleResource, jwt).then((doneMessage) => {
                    console.log("doneMessage: ", doneMessage);
                }).then(() => {
                    kuzzleValidate(kuzzleResource).then(() => {
                        disConnectFromKuzzle();
                    });
                });
            });
        });
    });
}

I'm looking to upload to a Minio bucket and obtain a pre-signedURL so I can store it in a document later.

Chris Jackson
  • 718
  • 1
  • 6
  • 14

1 Answers1

1

You can change the endpoint configuration to set a different s3-compatible endpoint who can be a Minio one.

This configuration can be changer under the plugins.s3.endpoint key. You should also disable the usage of default s3 path.

Example:

app.config.set('plugins.s3.endpoint', 'https://minio.local');
app.config.set('plugins.s3.s3ClientOptions.s3ForcePathStyle', false);
Aschen
  • 1,691
  • 11
  • 15
  • If I add the code kuzzle.config.set('plugins.s3.endpoint', 'https://minio.local'); I get an error of TypeError: Cannot read property 'set' of undefined. – Chris Jackson Jun 15 '21 at 17:10
  • What is "app"? Is it the same as kuzzle as defined above? – Chris Jackson Jun 15 '21 at 17:12
  • Looking at https://docs.kuzzle.io/official-plugins/s3/2/essentials/installation/ it dose say to edit the plugin section of .kuzzlerc . But what is app.config? Its not detailed out in the documentation. – Chris Jackson Jun 15 '21 at 19:33
  • i had a little success when setting in .kuzzlerc app.config.set('plugins.s3.s3ClientOptions.s3ForcePathStyle', false); - - to true – Chris Jackson Jun 15 '21 at 20:31
  • You need to set this config either using the application instance or the .kuzzlerc file. Mecanisms to modify Kuzzle configuration are listed here: https://docs.kuzzle.io/core/2/guides/advanced/configuration/ – Aschen Jun 17 '21 at 08:05