10分钟 把 minio 对象存储部署到阿里云-附nodejs示例

585 阅读3分钟

Minio是一个简单易用的云存储服务,就像是一个放在网络上的大文件柜。想象一下,你有一间放满了各种文件的房间,有时候你需要把这些文件分享给朋友或者在不同地方访问它们。Minio就是帮你做到这一点的工具,它让你可以轻松地把文件上传到互联网上,这样无论你在哪里,只要有网络,就能访问或分享这些文件。那我们我这里呢,就是拿来当博客的文件服务器使用。

准备工作

# 下载可执行文件
wget https://dl.min.io/server/minio/release/linux-amd64/minio

chmod +x ./minio

1. 生成免费证书

image.png

image.png

image.png 这里将证书下载下来后保存到服务器上,比如 /root/certificate/ 这个位置

2. 启动 minio

# 9011 是自定义 minio 的端口(默认9000),33933 是自定义控制台端口(不指定随机)
./minio server /root/minio-data/ --address :9011 --console-address :33933

试过用证书直接放到 ~/.minio/cert/ 中,始终没办法正确上传文件,知道的大佬帮忙解答一下

3. nginx 配置

官方地址 min.io/docs/minio/…

新建 /etc/nginx/sites-available/minio.conf, 将下面内容贴进去

替换 /root/certificate/xxxx.pem, /root/certificate/xxxx.key 到刚刚保存的证书地址

测试 nginx 配置文件正确与否 nginx -t

upstream minio {
    server localhost:9011;
}

upstream console {
    ip_hash;
    server localhost:33933;
}

server {
    listen       9001 ssl;
    listen  [::]:9001;
    server_name  localhost;

    ssl_certificate /root/certificate/xxxx.pem;
    ssl_certificate_key /root/certificate/xxxx.key;

    # To allow special characters in headers
    ignore_invalid_headers off;
    # Allow any size file to be uploaded.
    # Set to a value such as 1000m; to restrict file size to a specific value
    client_max_body_size 0;
    # To disable buffering
    proxy_buffering off;
    proxy_request_buffering off;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_connect_timeout 300;
        # Default is HTTP/1, keepalive is only enabled in HTTP/1.1
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        chunked_transfer_encoding off;

        proxy_pass http://minio;
    }
}

server {
    listen       33923 ssl;
    listen  [::]:33923;
    server_name  localhost;

    ssl_certificate /root/certificate/xxxx.pem;
    ssl_certificate_key /root/certificate/xxxx.key;

    # To allow special characters in headers
    ignore_invalid_headers off;
    # Allow any size file to be uploaded.
    # Set to a value such as 1000m; to restrict file size to a specific value
    client_max_body_size 0;
    # To disable buffering
    proxy_buffering off;
    proxy_request_buffering off;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-NginX-Proxy true;

        # This is necessary to pass the correct IP to be hashed
        real_ip_header X-Real-IP;

        proxy_connect_timeout 300;

        # To support websocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        chunked_transfer_encoding off;

        proxy_pass http://console;
    }
}

4 开放阿里云对外服务端口

阿里云网络安全组

image.png

5. 获取 accessKey, secretKey

image.png

6. 本地测试上传

替换 your_hostname your_accessKey your_secretKey

提前新建一个 /tmp/test-file.txt 文件

提前新建一个 bucket,也可以判断没有就新建

const Minio = require('minio')

// Instantiate the MinIO client with the object store service
// endpoint and an authorized user's credentials
// play.min.io is the MinIO public test cluster
const minioClient = new Minio.Client({
  endPoint: 'your_hostname',
  port: 9001,
  useSSL: true,
  accessKey: 'your_accessKey',
  secretKey: 'your_secretKey',
})

// File to upload
const sourceFile = '/tmp/test-file.txt'

// Destination bucket
const bucket = 'image-bucket'

// Destination object name
const destinationObject = 'my-test-file.txt'

// Check if the bucket exists
// If it doesn't, create it
// const exists = await minioClient.bucketExists(bucket)
// if (exists) {
//   console.log('Bucket ' + bucket + ' exists.')
// } else {
//   await minioClient.makeBucket(bucket, 'us-east-1')
//   console.log('Bucket ' + bucket + ' created in "us-east-1".')
// }

// Set the object metadata
var metaData = {
  'Content-Type': 'text/plain',
  'X-Amz-Meta-Testing': 1234,
  example: 5678,
}

// Upload the file with fPutObject
// If an object with the same name exists,
// it is updated with new data
minioClient.fPutObject(bucket, destinationObject, sourceFile, metaData).then(() => {
  console.log('File ' + sourceFile + ' uploaded as object ' + destinationObject + ' in bucket ' + bucket)
})

7. 总结

目前刚开始用,踩了不少坑,目前配置了半天,还是 nginx 方案最方便快捷,另外一种配置方案的,始终没有成功,还是有点遗憾的,好在,能用起来。