- Linux安装MINIO
https://blog.csdn.net/tongxin_tongmeng/article/details/133934115
- MINIO创建桶
- MINIO创建秘钥
- MINIO的API路径
http://your-server-ip:9000 注意:API路径在日志文件中/opt/minio/minio.log
- pom.xml
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 --> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.12.566</version> </dependency>
- application.yml
aws: s3: accessKey: minio_access_key secretKey: minio_secret_key url: http://your-server-ip:9000 bucket: minio-bucket spring: servlet: multipart: max-file-size: 100MB max-request-size: 100MB
- 配置类
@Data @Configuration @ConfigurationProperties(prefix = "aws.s3") public class AwzS3Config { private String accessKey; private String secretKey; private String url; private String bucket; }
-
工具类
@Component public class AwzS3Util { @Resource private AwzS3Config injectAwzS3Config; private static AwzS3Config awzS3Config; private static AmazonS3 amazonS3; @PostConstruct public void init() { awzS3Config = this.injectAwzS3Config; AWSCredentials credentials = new BasicAWSCredentials(awzS3Config.getAccessKey(), awzS3Config.getSecretKey()); AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials); AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(awzS3Config.getUrl(), Regions.CN_NORTH_1.getName()); ClientConfiguration config = new ClientConfiguration(); config.setSignerOverride("S3SignerType"); config.setProtocol(Protocol.HTTP); config.withUseExpectContinue(false); config.disableSocketProxy(); amazonS3 = AmazonS3Client.builder() .withEndpointConfiguration(endpointConfig) .withClientConfiguration(config) .withCredentials(credentialsProvider) .disableChunkedEncoding() .withPathStyleAccessEnabled(true) .withForceGlobalBucketAccessEnabled(true) .build(); } public static boolean createBucket(String bucket) { if (StringUtils.isNullOrEmpty(bucket)) { throw new IllegalArgumentException("桶名称不能为空!"); } try { amazonS3.createBucket(bucket); } catch (SdkClientException e) { e.printStackTrace(); return false; } return true; } public static boolean deleteBucket(String bucket) { if (StringUtils.isNullOrEmpty(bucket)) { throw new IllegalArgumentException("桶名称不能为空!"); } try { amazonS3.deleteBucket(bucket); } catch (SdkClientException e) { e.printStackTrace(); return false; } return true; } public static boolean upload(MultipartFile file, String bucket) { if (Objects.isNull(file)) { throw new IllegalArgumentException("文件不能为空!"); } String fileName = file.getOriginalFilename(); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType(file.getContentType()); objectMetadata.setContentLength(file.getSize()); bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3Config.getBucket() : bucket; try { PutObjectRequest request = new PutObjectRequest(bucket, fileName, file.getInputStream(), objectMetadata) // 设置文件访问权限 .withCannedAcl(CannedAccessControlList.BucketOwnerFullControl); PutObjectResult result = amazonS3.putObject(request); } catch (IOException e) { e.printStackTrace(); return false; } return true; } public static ResponseEntity<byte[]> downloadByName(String bucket, String fileName) throws IOException { if (StringUtils.isNullOrEmpty(bucket)) { throw new IllegalArgumentException("文件名称不能为空!"); } bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3Config.getBucket() : bucket; GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, fileName); S3Object s3Object = amazonS3.getObject(getObjectRequest); S3ObjectInputStream objectInputStream = s3Object.getObjectContent(); byte[] bytes = IOUtils.toByteArray(objectInputStream); // 替换中文 String showFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); httpHeaders.setContentLength(bytes.length); httpHeaders.setContentDispositionFormData("attachment", showFileName); return new ResponseEntity<>(bytes, httpHeaders, HttpStatus.OK); } public static boolean deleteFile(String bucket, String fileName) { if (StringUtils.isNullOrEmpty(fileName)) { throw new IllegalArgumentException("文件名称不能为空!"); } bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3Config.getBucket() : bucket; try { amazonS3.deleteObject(bucket, fileName); } catch (SdkClientException e) { e.printStackTrace(); return false; } return true; } public static ObjectMetadata getFileInfo(String bucket, String fileName) { if (StringUtils.isNullOrEmpty(fileName)) { throw new IllegalArgumentException("文件名称不能为空!"); } bucket = StringUtils.isNullOrEmpty(bucket) ? awzS3Config.getBucket() : bucket; ObjectMetadata objectMetadata = null; try { objectMetadata = amazonS3.getObjectMetadata(bucket, fileName); } catch (SdkClientException e) { e.printStackTrace(); } return objectMetadata; } }
- 源码地址
https://gitee.com/xyzissj/minio.git