启动控制台,在minio文件目录下执行(复制以下代码)。
CMDをスタットして、minio目録の中には実行する。(コッピ)
minio.exe --compat --config-dir config server data/data/.minio.sys/config/config.json
//导包
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;
@Autowired
private S3Client s3Client;
File file = new File("c:\\test.csv");
try (InputStream inputStream = new FileInputStream(zipFile)) {
this.fileSave(fileName, inputStream,bucketName);
}
/**
*fileName:文件名 如果桶里面有文件夹,文件名前面带上文件夹 input/fileName
*inputStream:输入流
*bucketName:桶名 例:bucket
*/
public void fileSave(String fileName, InputStream inputStream, String bucketName) throws IOException {
// S3アップロード
PutObjectRequest build = PutObjectRequest.builder().bucket(bucketName).key(fileName).build();
RequestBody fromInputStream = RequestBody.fromBytes(inputStream.readAllBytes());
s3Client.putObject(build, fromInputStream);
}
s3Client.deleteObject(DeleteObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.build());
/**
* encode:指定的编码格式
* bucketName:桶名
* fileName:文件夹名/文件名
* coypToBucketName:复制到的桶名
* copyTofile:复制的位置 文件夹名/文件夹
*/
String encode = URLEncoder.encode(bucketName + "/" + fileName,
StandardCharsets.UTF_8.toString());
CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder().copySource(encode)
.destinationBucket(coypToBucketName)
.destinationKey(copyTofile).build();
s3Client.copyObject(copyObjectRequest);
//Controller側
public String downloadFile(Model model、HttpServletResponse response,
@AuthenticationPrincipal LoginUserDetails userDetails) {
fileDownload(fileName, response,bucketName);
return null;
}
private void fileDownload(String fileName, HttpServletResponse response, String bucketName) throws IOException {
// ファイルチェック
Boolean checkObject = S3Utils.s3FileCheck(fileName, bucketName, s3Client);
// S3側 ファイルがあるの場合は、Downloadする
if (fileName != null && checkObject) {
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="
+URLEncoder.encode(fileName, "UTF-8"));
// ファイル出力
ResponseInputStream<GetObjectResponse> object =s3Client
.getObject(GetObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.build());
ServletOutputStream out = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = object.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.close();
object.close();
log.info("file Download: " + fileName + ",bucket: " + bucketName);
return;
}
// 取得できない場合は、メッセージID:MSKWOI026を設定し、加盟店情報登録画面を表示
throw new FileNotFoundException("S3 File Not Found. Bucket: "
+ bucketName + " ,FileName: " + fileName);
}
import java.util.List;
import java.util.Objects;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Object;
public class S3Utils {
/**
* S3 File保存Util
*/
public static void putObjectForReader(String fileName, String bucketName, S3Client s3Client, byte[] readAllBytes) {
Boolean checkBucket = true;
// Bucket存在チェック
List<Bucket> buckets = s3Client.listBuckets()
.buckets();
for (Bucket bucket : buckets) {
if (Objects.equals(bucket.name(), bucketName)) {
checkBucket = false;
break;
}
}
if (Boolean.TRUE.equals(checkBucket)) {
s3Client.createBucket(CreateBucketRequest.builder()
.bucket(bucketName)
.build());
}
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.build();
RequestBody fromInputStream = RequestBody.fromBytes(readAllBytes);
s3Client.putObject(putObjectRequest, fromInputStream);
}
/**
* S3ファイルチェッククラス
*
* @param fileName ファイル名
* @param bucketName Bucket名
* @param s3Client S3
* @return ファイルある:True なし:False
*/
public static Boolean s3FileCheck(String fileName, String bucketName, S3Client s3Client) {
Boolean checkBucket = false;
Boolean checkObject = false;
String bucketTempName = null;
// Bucket存在チェック
List<Bucket> buckets = s3Client.listBuckets()
.buckets();
for (Bucket bucket : buckets) {
if (Objects.equals(bucket.name(), bucketName)) {
bucketTempName = bucket.name();
checkBucket = true;
break;
}
}
// ファイル存在チェック
// BucketNameがあるの場合、このBucketの中には相関のファイルを探す
if (Boolean.TRUE.equals(checkBucket)) {
ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder()
.bucket(bucketTempName)
.build();
ListObjectsResponse listObjects = s3Client.listObjects(listObjectsRequest);
List<S3Object> fileList = listObjects.contents();
for (S3Object s3Object : fileList) {
if (Objects.equals(s3Object.key(), fileName)) {
checkObject = true;
break;
}
}
}
return checkObject;
}
private S3Utils() {
}
}