首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kuzzle / Minio示例用法

Kuzzle / Minio示例用法
EN

Stack Overflow用户
提问于 2021-06-15 02:56:26
回答 1查看 92关注 0票数 2

Kuzzle开发团队有没有使用Kuzzle的S3插件的工作示例?我有以下内容,但我的文件未被上传,并且预签名的url指向https://your-s3-bucket.s3.eu-west-3.amazonaws.com/

代码语言:javascript
复制
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存储桶,并获得一个预先签名的pre,这样我就可以稍后将其存储在文档中。

EN

回答 1

Stack Overflow用户

发布于 2021-06-15 16:34:18

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

可以在plugins.s3.endpoint密钥下更改此configuration。您还应禁用默认s3路径的使用。

示例:

代码语言:javascript
复制
app.config.set('plugins.s3.endpoint', 'https://minio.local');
app.config.set('plugins.s3.s3ClientOptions.s3ForcePathStyle', false);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67975772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档