基本请求流程: 1. 获取授权 web->服务端->ceph服务端=》授权的url 2. 上传文件 web-> 授权的url执行PUT请求 一: java代码 minio客户端版本: dependencies { implementation io.minio:minio:8.2.2 }
import io.minio.BucketExistsArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.http.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyMinIoClient {
private static final Logger LOG = LoggerFactory.getLogger(MyMinIoClient.class);
private static MinioClient minioClient;
private static String endpoint = "http://abc";
private static int port = 90;
private static Boolean secure = false;
private static String accessKey = "accessKeyaccessKeyaccessKey";
private static String secretKey = "secretKeysecretKeysecretKeysecre";
private static String bucket = "bucketTest001";
private static String policy = "{\"Statement\":[{\"Action\":\"s3:GetObject\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::%s/*\"},{\"Action\":\"s3:putObject\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::%s/*\"}],\"Version\":\"2012-10-17\"}'";
private static int policyExpireSecond = 60 * 10;
public static void main(String[] args) {
minioClient = MinioClient.builder()
.endpoint(endpoint, port, secure)
.credentials(accessKey, secretKey)
.build();
if (!exitsBucket(bucket)) {
makeBucket(bucket);
setBucketPolicy(bucket);
}
}
public static String getPolicy(String fileName) {
try {
fileName = "abc" + "/" + fileName;
return minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.bucket(bucket)
.expiry(policyExpireSecond)
.object(fileName)
.method(Method.PUT)
.build());
} catch (Exception e) {
LOG.error("getPolicy failed, msg = {}", e.getMessage(), e);
throw new RuntimeException("资源服务暂不可用,请稍后重试");
}
}
public static void makeBucket(String bucket) {
try {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
} catch (
Exception e) {
LOG.error("makeBucket failed, msg = {}", e.getMessage(), e);
throw new RuntimeException("啊偶,问题了,请稍后重试");
}
}
public static void setBucketPolicy(String bucket) {
try {
minioClient.setBucketPolicy(
SetBucketPolicyArgs.builder().bucket(bucket)
.config(String.format(policy, bucket, bucket))
.build());
} catch (Exception e) {
LOG.error("setBucketPolicy failed, msg = {}", e.getMessage(), e);
throw new RuntimeException("啊偶,问题了,请稍后重试");
}
}
public static boolean exitsBucket(String bucketName) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
LOG.error("exitsBucket request failed, msg = {}", e.getMessage(), e);
throw new RuntimeException("啊偶,问题了,请稍后重试");
}
}
}
二: nginx配置
注意点: nginx代理ceph时,可能会导致发生403错误,或者跨域请求错误,需要nginx做特殊配置
server {
listen 90;
server_name 127.0.0.1;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST,PUT, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
location / {
if ($request_method = 'OPTIONS') {
return 204;
}
proxy_pass http://CEPH_IP:CEPH_PORT$1; ## CEPH_IP和CEPH_PORT分别对应代理的ceph服务端ip和端口
proxy_set_header host $http_host;
}
}