-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加了minio,用来保存歌曲,并测试下载和播放功能成功
- Loading branch information
Showing
8 changed files
with
221 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
music-server/src/main/java/com/example/yin/config/MinioConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.yin.config; | ||
|
||
import io.minio.MinioClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class MinioConfig { | ||
|
||
@Value("${minio.endpoint}") | ||
private String minioEndpoint; | ||
|
||
@Value("${minio.access-key}") | ||
private String minioAccessKey; | ||
|
||
@Value("${minio.secret-key}") | ||
private String minioSecretKey; | ||
|
||
@Bean | ||
public MinioClient minioClient() { | ||
return MinioClient.builder() | ||
.endpoint(minioEndpoint) | ||
.credentials(minioAccessKey, minioSecretKey) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
music-server/src/main/java/com/example/yin/controller/MinioController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.yin.controller; | ||
|
||
import io.minio.GetObjectArgs; | ||
import io.minio.MinioClient; | ||
import org.apache.commons.io.IOUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.*; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.InputStream; | ||
|
||
|
||
@Controller | ||
public class MinioController { | ||
@Value("${minio.bucket-name}") | ||
private String bucketName; | ||
@Autowired | ||
private MinioClient minioClient; | ||
@GetMapping("/user01/{fileName:.+}") | ||
public ResponseEntity<byte[]> getMusic(@PathVariable String fileName) { | ||
try { | ||
GetObjectArgs args = GetObjectArgs.builder() | ||
.bucket(bucketName) | ||
.object(fileName) | ||
.build(); | ||
InputStream inputStream = minioClient.getObject(args); | ||
|
||
// 将文件内容读取为字节数组 | ||
byte[] bytes = IOUtils.toByteArray(inputStream); | ||
|
||
// 设置响应头,指示浏览器如何处理响应内容 | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); | ||
headers.setContentDispositionFormData("attachment", fileName); // 如果需要下载文件,可以使用此头部 | ||
|
||
// 返回字节数组作为响应 | ||
return new ResponseEntity<>(bytes, headers, HttpStatus.OK); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
music-server/src/main/java/com/example/yin/controller/MinioUploadController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.example.yin.controller; | ||
|
||
import io.minio.MinioClient; | ||
import io.minio.PutObjectArgs; | ||
import io.minio.errors.MinioException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.InvalidKeyException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Properties; | ||
|
||
@Service | ||
public class MinioUploadController { | ||
|
||
private static MinioClient minioClient; | ||
private static String bucketName; | ||
|
||
public static void init(){ | ||
Properties properties = new Properties(); | ||
try { | ||
// 使用类加载器获取资源文件的输入流 | ||
InputStream inputStream = 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) { | ||
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters