中国机加工订单网郑州网站优化顾问
minio使用shell上传文件
- 前言
- 1. 编写调用脚本
- 2.测试脚本上传
- 3.候选脚本
前言
业务场景需要实现,服务器文件上传至存储服务。一种方式是安装minio的linux客户端,另一种方式是通过调用minio的api接口实现文件上传。后一种方式不需要依赖minio的客户端使用起来又一定便利性。本文介绍通过minio api接口的上传文件的脚本写法及用法
1. 编写调用脚本
#!/bin/bash# 检查参数是否正确
if [ "$#" -ne 2 ]; thenecho "usage: $0 <bucket-name> <file-path>"exit 1
fi# 设置MinIO信息
minio_url="http://192.168.100.100:7000"
minio_access_key="minio"
minio_secret_key="minio@admin"
minio_bucket="$1"# 设置文件名和路径
file_path="$2"
file_name="$(basename $file_path)"# 生成 HTTP 头
http_date=$(date -u "+%a, %d %h %Y %H:%M:%S GMT")
http_content_md5=$(openssl md5 -binary < $file_path | base64)
http_content_type=$(file -b --mime-type $file_path)
http_request_path="/$minio_bucket/$file_name"
http_request_body=""http_request="PUT\n$http_content_md5\n$http_content_type\n$http_date\n$http_request_path"
http_signature=$(echo -en "${http_request}" | openssl sha1 -binary -hmac "${minio_secret_key}" | base64)# 使用 cURL 和 MinIO API 上传文件
curl -X PUT \-H "Date: $http_date" \-H "Content-Type: $http_content_type" \-H "Content-MD5: $http_content_md5" \-H "Authorization: AWS $minio_access_key:$http_signature" \--upload-file "$file_path" \"$minio_url/$minio_bucket/$file_name"
2.测试脚本上传
./upload_to_minio_new.sh aaaa upms_install.sh
3.候选脚本
#!/bin/bash# usage: ./minio-upload my-bucket my-file.zipbucket=$1
file=$2
host=192.168.100.100:7000
s3_key='minio'
s3_secret='minio@admin'
resource="/${bucket}/${file}"
content_type='application/zip'
date=$(date -R)
filesize=$(stat -c%s "$file")_signature="PUT\n\n${content_type}\n${date}\n${resource}"
signature=$(echo -en "${_signature}" | openssl sha1 -hmac "${s3_secret}" -binary | base64)curl -v --fail \-X PUT \--data-binary "@$file" \--header "Host: ${host}" \--header "Date: ${date}" \--header "Content-Type: ${content_type}" \--header "Content-Length: ${filesize}" \--header "Authorization: AWS ${s3_key}:${signature}" \http://${host}${resource}