大连网站建设与维护题库,网站建设目标是,餐厅设计公司餐厅设计,wordpress离线发布需求#xff1a;工作中#xff0c;在微信小程序播放时#xff0c;返回文件流并不能有效的使用#xff0c;前端需要一个可以访问的地址#xff0c;springboot默认是有资源拦截器的#xff0c;但是不适合生产环境的使用 可以提供使用的有例如fastdfs或者minio#xff0c;这… 需求工作中在微信小程序播放时返回文件流并不能有效的使用前端需要一个可以访问的地址springboot默认是有资源拦截器的但是不适合生产环境的使用 可以提供使用的有例如fastdfs或者minio这里以minio为例 环境
软件版本docker24.0.4minioRELEASE.2023-10-24T05-18-28Z (commit-id97cc12fdc539361cf175ffc2f00480eec0836d82)
minio安装
docker命令
docker run -d \
-p 9000:9000 \
-p 9001:9001 \
--name minio
--restartalways
--privilegedtrue \
-v /home/minio/data:/data \
-e MINIO_ROOT_USERuser \
-e MINIO_ROOT_PASSWORDpassword \
minio/minio server /data
--console-address :9001开启linux防火墙
centos开启防火墙
打开浏览器访问 ip:9001 看到此页面即为成功
springboot整合minio
pom.xml
dependencygroupIdcom.squareup.okhttp3/groupIdartifactIdokhttp/artifactIdversion4.9.0/version
/dependencydependencygroupIdio.minio/groupIdartifactIdminio/artifactIdversion8.5.6/versionexclusionsexclusionartifactIdokhttp/artifactIdgroupIdcom.squareup.okhttp3/groupId/exclusion/exclusions
/dependency 配置类
Configuration
public class MinioConfig {Value(${minio.endpoint})private String endpoint;Value(${minio.accessKey})private String accessKey;Value(${minio.secretKey})private String secretKey;Beanpublic MinioClient minioClient(){returnMinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();}
}
文件上传的工具类
Slf4j
public class MinioUtils {public static String uploadFile(MinioClient minioClient, InputStream inputStream, String bucket, String filename) {try {boolean found minioClient.bucketExists(BucketExistsArgs.builder().bucket(public).build());if (!found) {minioClient.makeBucket(MakeBucketArgs.builder().bucket(public).build());}ObjectWriteResponse response minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(filename).stream(inputStream, inputStream.available(), -1).contentType(InferStatusConstant.WAV_CONTENT_TYPE).build());String url minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucket).expiry(7 * 24 * 60 * 60).object(filename).method(Method.GET).build());log.info(分享地址 url);return url;} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException |InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException |XmlParserException e) {throw new RuntimeException(e);}}
}测试类
Test
public void uploadFileToMinio() {try (FileInputStream stream new FileInputStream(/path/to/file)) {String url MinioUtils.uploadFile(minioClient, stream, test, /test/test1.wav);System.out.println(url);} catch (Exception e) {}
}
遇到的一些问题
运行springboot的测试类没有上传debug之后显示s3 api requests must be made to api port.
解决方案
进入docker
docker exec -it minio bash进入后,查看信息
mc config host ls
找到自己的服务我的为localhost查看下方的url等信息均不对移除当前服务
mc config host remove 服务名
添加新的服务注意url信息注意端口
mc config host add 服务名 http://IP:9000 user password --api S3v4
不需要重启重新运行测试代码发现运行成功
在使用的过程中生成分享连接为127.0.0.1/XXXXXXX
解决方案同上修改自己的ip