【问题标题】:Kuzzle / Minio example usageKuzzle / Minio 示例用法
【发布时间】:2021-06-14 18:56:25
【问题描述】:

Kuzzle 或 Minio 开发团队有没有为 Minio 使用 Kuzzle S3 插件的工作示例?我有以下内容,但我的文件没有上传,预签名的 url 指的是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();
                    });
                });
            });
        });
    });
}

我希望上传到 Minio 存储桶并获取预签名 URL,以便稍后将其存储在文档中。

【问题讨论】:

    标签: node.js minio kuzzle


    【解决方案1】:

    您可以更改 endpoint 配置以设置不同的 s3 兼容端点,该端点可以是 Minio 端点。

    这个configuration 可以在plugins.s3.endpoint 键下更换。您还应该禁用默认 s3 路径。

    例子:

    app.config.set('plugins.s3.endpoint', 'https://minio.local');
    app.config.set('plugins.s3.s3ClientOptions.s3ForcePathStyle', false);
    

    【讨论】:

    • 如果我添加代码 kuzzle.config.set('plugins.s3.endpoint', 'minio.local');我收到 TypeError 错误:无法读取未定义的属性“设置”。
    • 什么是“应用程序”?和上面定义的 kuzzle 一样吗?
    • 看着docs.kuzzle.io/official-plugins/s3/2/essentials/installation 说要编辑 .kuzzlerc 的插件部分。但是 app.config 是什么?文档中没有详细说明。
    • 我在 .kuzzlerc app.config.set('plugins.s3.s3ClientOptions.s3ForcePathStyle', false); - - 为真
    • 您需要使用应用程序实例或 .kuzzlerc 文件来设置此配置。此处列出了修改 Kuzzle 配置的机制:docs.kuzzle.io/core/2/guides/advanced/configuration
    猜你喜欢
    • 1970-01-01
    • 2023-01-04
    • 2020-09-13
    • 1970-01-01
    • 2019-07-26
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-21
    相关资源
    最近更新 更多