|
@@ -1,5 +1,8 @@
|
|
package org.example.music.controller;
|
|
package org.example.music.controller;
|
|
|
|
|
|
|
|
+import io.minio.MinioClient;
|
|
|
|
+import io.minio.PutObjectArgs;
|
|
|
|
+import io.minio.errors.MinioException;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
@@ -12,135 +15,130 @@ import java.util.Properties;
|
|
@Service
|
|
@Service
|
|
public class MinioUploadController {
|
|
public class MinioUploadController {
|
|
|
|
|
|
-// private static MinioClient minioClient;
|
|
|
|
-// private static String bucketName;
|
|
|
|
-//
|
|
|
|
-// public static void init(){
|
|
|
|
-// Properties properties = new Properties();
|
|
|
|
-// try {
|
|
|
|
-// // 使用类加载器获取资源文件的输入流
|
|
|
|
-// InputStream inputStream = org.example.music.controller.MinioUploadController.class.getClassLoader().getResourceAsStream("application-dev.properties");
|
|
|
|
-// if (inputStream != null) {
|
|
|
|
-// properties.load(inputStream);
|
|
|
|
-// String minioEndpoint = properties.getProperty("minio.endpoint");
|
|
|
|
-// String minioAccessKey = properties.getProperty("minio.access-key");
|
|
|
|
-// String minioSecretKey = properties.getProperty("minio.secret-key");
|
|
|
|
-// String minioBucketName = properties.getProperty("minio.bucket-name");
|
|
|
|
-// bucketName = minioBucketName;
|
|
|
|
-// minioClient = MinioClient.builder()
|
|
|
|
-// .endpoint(minioEndpoint)
|
|
|
|
-// .credentials(minioAccessKey, minioSecretKey)
|
|
|
|
-// .build();
|
|
|
|
-// }
|
|
|
|
-// }catch (Exception e){
|
|
|
|
-// System.out.println(e);
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-//
|
|
|
|
|
|
+ private static MinioClient minioClient;
|
|
|
|
+ private static String bucketName;
|
|
|
|
+
|
|
|
|
+ public static void init(){
|
|
|
|
+ Properties properties = new Properties();
|
|
|
|
+ try {
|
|
|
|
+ // 使用类加载器获取资源文件的输入流
|
|
|
|
+ InputStream inputStream = org.example.music.controller.MinioUploadController.class.getClassLoader().getResourceAsStream("application-dev.properties");
|
|
|
|
+ if (inputStream != null) {
|
|
|
|
+ properties.load(inputStream);
|
|
|
|
+ String minioEndpoint = properties.getProperty("minio.endpoint");
|
|
|
|
+ String minioAccessKey = properties.getProperty("minio.access-key");
|
|
|
|
+ String minioSecretKey = properties.getProperty("minio.secret-key");
|
|
|
|
+ String minioBucketName = properties.getProperty("minio.bucket-name");
|
|
|
|
+ bucketName = minioBucketName;
|
|
|
|
+ minioClient = MinioClient.builder()
|
|
|
|
+ .endpoint(minioEndpoint)
|
|
|
|
+ .credentials(minioAccessKey, minioSecretKey)
|
|
|
|
+ .build();
|
|
|
|
+ }
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ System.out.println(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public static String uploadFile(MultipartFile file) {
|
|
public static String uploadFile(MultipartFile file) {
|
|
-// try {
|
|
|
|
-// init();
|
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
|
-// minioClient.putObject(
|
|
|
|
-// PutObjectArgs.builder()
|
|
|
|
-// .bucket(bucketName)
|
|
|
|
-// .object(file.getOriginalFilename())
|
|
|
|
-// .stream(inputStream, inputStream.available(), -1)
|
|
|
|
-// .contentType(file.getContentType())
|
|
|
|
-// .build()
|
|
|
|
-// );
|
|
|
|
-// return "File uploaded successfully!";
|
|
|
|
-// } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// }
|
|
|
|
- return "File uploaded successfully!";
|
|
|
|
|
|
+ try {
|
|
|
|
+ init();
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
+ minioClient.putObject(
|
|
|
|
+ PutObjectArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .object(file.getOriginalFilename())
|
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
|
+ .contentType(file.getContentType())
|
|
|
|
+ .build()
|
|
|
|
+ );
|
|
|
|
+ return "File uploaded successfully!";
|
|
|
|
+ } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
public static String uploadImgFile(MultipartFile file) {
|
|
public static String uploadImgFile(MultipartFile file) {
|
|
-// try {
|
|
|
|
-// init();
|
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
|
-// minioClient.putObject(
|
|
|
|
-// PutObjectArgs.builder()
|
|
|
|
-// .bucket(bucketName)
|
|
|
|
-// .object("/singer/img/"+file.getOriginalFilename())
|
|
|
|
-// .stream(inputStream, inputStream.available(), -1)
|
|
|
|
-// .contentType(file.getContentType())
|
|
|
|
-// .build()
|
|
|
|
-// );
|
|
|
|
-// return "File uploaded successfully!";
|
|
|
|
-// } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// }
|
|
|
|
- return "File uploaded successfully!";
|
|
|
|
|
|
+ try {
|
|
|
|
+ init();
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
+ minioClient.putObject(
|
|
|
|
+ PutObjectArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .object("/singer/img/"+file.getOriginalFilename())
|
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
|
+ .contentType(file.getContentType())
|
|
|
|
+ .build()
|
|
|
|
+ );
|
|
|
|
+ return "File uploaded successfully!";
|
|
|
|
+ } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
public static String uploadSonglistImgFile(MultipartFile file) {
|
|
public static String uploadSonglistImgFile(MultipartFile file) {
|
|
-// try {
|
|
|
|
-// init();
|
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
|
-// minioClient.putObject(
|
|
|
|
-// PutObjectArgs.builder()
|
|
|
|
-// .bucket(bucketName)
|
|
|
|
-// .object("/songlist/"+file.getOriginalFilename())
|
|
|
|
-// .stream(inputStream, inputStream.available(), -1)
|
|
|
|
-// .contentType(file.getContentType())
|
|
|
|
-// .build()
|
|
|
|
-// );
|
|
|
|
-// return "File uploaded successfully!";
|
|
|
|
-// } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// }
|
|
|
|
- return "File uploaded successfully!";
|
|
|
|
|
|
+ try {
|
|
|
|
+ init();
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
+ minioClient.putObject(
|
|
|
|
+ PutObjectArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .object("/songlist/"+file.getOriginalFilename())
|
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
|
+ .contentType(file.getContentType())
|
|
|
|
+ .build()
|
|
|
|
+ );
|
|
|
|
+ return "File uploaded successfully!";
|
|
|
|
+ } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
public static String uploadSongImgFile(MultipartFile file) {
|
|
public static String uploadSongImgFile(MultipartFile file) {
|
|
-// try {
|
|
|
|
-// init();
|
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
|
-// minioClient.putObject(
|
|
|
|
-// PutObjectArgs.builder()
|
|
|
|
-// .bucket(bucketName)
|
|
|
|
-// .object("/singer/song/"+file.getOriginalFilename())
|
|
|
|
-// .stream(inputStream, inputStream.available(), -1)
|
|
|
|
-// .contentType(file.getContentType())
|
|
|
|
-// .build()
|
|
|
|
-// );
|
|
|
|
-// return "File uploaded successfully!";
|
|
|
|
-// } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// }
|
|
|
|
- return "File uploaded successfully!";
|
|
|
|
|
|
+ try {
|
|
|
|
+ init();
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
+ minioClient.putObject(
|
|
|
|
+ PutObjectArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .object("/singer/song/"+file.getOriginalFilename())
|
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
|
+ .contentType(file.getContentType())
|
|
|
|
+ .build()
|
|
|
|
+ );
|
|
|
|
+ return "File uploaded successfully!";
|
|
|
|
+ } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
-//
|
|
|
|
|
|
+
|
|
public static String uploadAtorImgFile(MultipartFile file) {
|
|
public static String uploadAtorImgFile(MultipartFile file) {
|
|
-// try {
|
|
|
|
-// init();
|
|
|
|
-// InputStream inputStream = file.getInputStream();
|
|
|
|
-// minioClient.putObject(
|
|
|
|
-// PutObjectArgs.builder()
|
|
|
|
-// .bucket(bucketName)
|
|
|
|
-// .object("/img/avatorImages/"+file.getOriginalFilename())
|
|
|
|
-// .stream(inputStream, inputStream.available(), -1)
|
|
|
|
-// .contentType(file.getContentType())
|
|
|
|
-// .build()
|
|
|
|
-// );
|
|
|
|
-// return "File uploaded successfully!";
|
|
|
|
-// } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// throw new RuntimeException(e);
|
|
|
|
-// }
|
|
|
|
- return "File uploaded successfully!";
|
|
|
|
|
|
+ try {
|
|
|
|
+ init();
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
+ minioClient.putObject(
|
|
|
|
+ PutObjectArgs.builder()
|
|
|
|
+ .bucket(bucketName)
|
|
|
|
+ .object("/img/avatorImages/"+file.getOriginalFilename())
|
|
|
|
+ .stream(inputStream, inputStream.available(), -1)
|
|
|
|
+ .contentType(file.getContentType())
|
|
|
|
+ .build()
|
|
|
|
+ );
|
|
|
|
+ return "File uploaded successfully!";
|
|
|
|
+ } catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return "Error uploading file to MinIO: " + e.getMessage();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|