MinIO 设置桶策略

工具类写法

  /**
     *
     * 权限设置为读权限
     * @param bucketName 存储bucket名称
     * @return Boolean
     */
    public void updateBucketPolicy(String bucketName,String policy) throws Exception {
        if (!checkBucket(bucketName)) {
            throw new Exception("bucket不存在");
        }
        if(StringUtils.isEmpty(policy)){
            throw new Exception("策略不能为空");
        }
        try {
            switch (policy){
                case "R":
                    policy= readonlyPolicy().toString();
                    break;
                case "W":
                    policy= writeonlyPolicy().toString();
                    break;
                case "RW":
                    policy= readwritePolicy().toString();
                    break;
                default:
                    throw new Exception("无对应策略");
            }
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("Minio服务未知错误");
        }
    }



    /**
     * 仅读策略
     * @return
     */
    private  StringBuilder readonlyPolicy(){
        StringBuilder builder=new StringBuilder();
        builder.append("{\n" +
                "    \"Version\": \"2012-10-17\",\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Sid\":\"PublicRead\",\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": \"*\",\n" +
                "            \"Action\": [\n" +
                "                \"s3:GetBucketLocation\",\n" +
                "                \"s3:GetObject\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::*\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}");
        return builder;
    }


    /**
     * 仅写策略
     * @return
     */
    private  StringBuilder writeonlyPolicy(){
        StringBuilder builder=new StringBuilder();
        builder.append("{\n" +
                "    \"Version\": \"2012-10-17\",\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Sid\":\"PublicWrite\",\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": \"*\",\n" +
                "            \"Action\": [\n" +
                "                  \"s3:PutObject\"" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::*\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}");
        return builder;
    }

    /**
     *  默认权限:读写策略
     * @return
     */
    private  StringBuilder readwritePolicy(){
        StringBuilder builder=new StringBuilder();
        builder.append("{\n" +
                "    \"Version\": \"2012-10-17\",\n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Sid\":\"PublicReadWrite\",\n" +
                "            \"Effect\": \"Allow\",\n" +
                "            \"Principal\": \"*\",\n" +
                "            \"Action\": [\n" +
                "                \"s3:ListAllMyBuckets\",\n" +
                "                \"s3:ListBucket\",\n" +
                "                \"s3:GetBucketLocation\",\n" +
                "                \"s3:GetObject\",\n" +
                "                \"s3:PutObject\",\n" +
                "                \"s3:DeleteObject\"\n" +
                "            ],\n" +
                "            \"Resource\": [\n" +
                "                \"arn:aws:s3:::*\"\n" +
                "            ]\n" +
                "        }\n" +
                "    ]\n" +
                "}");
        return builder;
    }

你可能感兴趣的:(java,前端,服务器)