oss增加按【通过文件扩展名获取MIME类型】存储对象
| | |
| | | package com.ard.oss.service; |
| | | |
| | | import com.ard.oss.model.CredentialsDTO; |
| | | |
| | | import java.io.InputStream; |
| | | import java.net.URL; |
| | | |
| | |
| | | |
| | | /** |
| | | * Get temporary credentials. |
| | | * |
| | | * @return |
| | | */ |
| | | CredentialsDTO getCredentials(); |
| | | |
| | | /** |
| | | * Get the address of the object based on the bucket name and the object name. |
| | | * |
| | | * @param bucket bucket name |
| | | * @param objectKey object name |
| | | * @return download link |
| | |
| | | |
| | | /** |
| | | * Deletes the object in the storage bucket. |
| | | * |
| | | * @param bucket |
| | | * @param objectKey |
| | | * @return |
| | |
| | | |
| | | /** |
| | | * Get the contents of an object. |
| | | * |
| | | * @param bucket |
| | | * @param objectKey |
| | | * @return |
| | |
| | | |
| | | void putObject(String bucket, String objectKey, InputStream input); |
| | | |
| | | void putObject(String bucket, String objectKey, InputStream input, String contentType); |
| | | |
| | | void createClient(); |
| | | |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input, String contentType) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void createClient() { |
| | | if (Objects.nonNull(this.ossClient)) { |
| | | return; |
| | |
| | | PutObjectResult objectResult = client.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata())); |
| | | log.info("Upload File: {}", objectResult.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input, String contentType) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void createClient() { |
| | | if (Objects.nonNull(this.client)) { |
| | |
| | | } |
| | | } |
| | | @Override |
| | | public void putObject(String bucket, String objectKey, InputStream input, String contentType) { |
| | | try { |
| | | // 检查文件是否已经存在 |
| | | client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build()); |
| | | throw new RuntimeException("The file already exists: " + objectKey); |
| | | } catch (ErrorResponseException e) { |
| | | // 如果文件不存在,则继续上传 |
| | | log.info("The file does not exist, start uploading."); |
| | | try { |
| | | // 设置 partSize 为 10MB |
| | | long partSize = 10 * 1024 * 1024; // 每块 10MB |
| | | PutObjectArgs.Builder putArgsBuilder = PutObjectArgs.builder() |
| | | .bucket(bucket) |
| | | .object(objectKey) |
| | | .stream(input, -1, partSize); |
| | | |
| | | // 如果提供了 Content-Type,则设置该类型 |
| | | if (contentType != null && !contentType.isEmpty()) { |
| | | putArgsBuilder.contentType(contentType); |
| | | } |
| | | |
| | | // 上传文件 |
| | | ObjectWriteResponse response = client.putObject(putArgsBuilder.build()); |
| | | log.info("Uploaded file successfully: {}, ETag: {}", objectKey, response.etag()); |
| | | |
| | | } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) { |
| | | log.error("Failed to upload file: {}", objectKey, ex); |
| | | throw new RuntimeException("File upload failed: " + objectKey, ex); |
| | | } |
| | | } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) { |
| | | // 如果 statObject 发生错误,说明服务端错误或其他问题 |
| | | log.error("Failed to check if file exists: {}", objectKey, e); |
| | | throw new RuntimeException("Failed to check if file exists: " + objectKey, e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void createClient() { |
| | | if (Objects.nonNull(this.client)) { |
| | | return; |
| | |
| | | */ |
| | | public String upload(MultipartFile file) throws IOException { |
| | | try { |
| | | return upload(OssConfiguration.bucket, null, file); |
| | | return upload(OssConfiguration.bucket, file); |
| | | } catch (Exception e) { |
| | | throw new IOException(e.getMessage(), e); |
| | | } |
| | |
| | | ? extractFilename(file) |
| | | : object; |
| | | |
| | | // 通过文件扩展名获取 MIME 类型 |
| | | String contentType = getMimeTypeByExtension(file.getOriginalFilename()); |
| | | |
| | | @Cleanup |
| | | InputStream inputStream = file.getInputStream(); |
| | | // 上传文件到 Minio |
| | | this.ossService.putObject(bucket, objectName, inputStream); |
| | | this.ossService.putObject(bucket, objectName, inputStream, contentType); |
| | | |
| | | // ✅ 拼接永久 URL(不带 presigned 过期参数) |
| | | String baseUrl = OssConfiguration.endpoint; |
| | |
| | | } |
| | | } |
| | | |
| | | // 获取文件 MIME 类型 |
| | | public String getMimeTypeByExtension(String fileName) { |
| | | // 获取文件扩展名 |
| | | String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase(); |
| | | |
| | | // 根据文件扩展名返回 MIME 类型 |
| | | switch (extension) { |
| | | case "jpg": |
| | | case "jpeg": |
| | | return "image/jpeg"; |
| | | case "png": |
| | | return "image/png"; |
| | | case "gif": |
| | | return "image/gif"; |
| | | case "pdf": |
| | | return "application/pdf"; |
| | | case "txt": |
| | | return "text/plain"; |
| | | case "html": |
| | | return "text/html"; |
| | | case "json": |
| | | return "application/json"; |
| | | case "csv": |
| | | return "text/csv"; |
| | | case "zip": |
| | | return "application/zip"; |
| | | // 添加更多的扩展名映射 |
| | | default: |
| | | return "application/octet-stream"; // 默认 MIME 类型 |
| | | } |
| | | } |
| | | |
| | | } |