| | |
| | | /** |
| | | * 上传文件到 Amazon S3,并返回上传结果 |
| | | * |
| | | * @param filePath 本地文件路径 |
| | | * @param key 在 Amazon S3 中的对象键 |
| | | * @param md5Digest 本地文件的 MD5 哈希值(可选) |
| | | * @param filePath 本地文件路径 |
| | | * @param key 在 Amazon S3 中的对象键 |
| | | * @param md5Digest 本地文件的 MD5 哈希值(可选) |
| | | * @param contentType 文件内容类型 |
| | | * @return UploadResult 包含上传后的文件信息 |
| | | * @throws OssException 如果上传失败,抛出自定义异常 |
| | | */ |
| | | public UploadResult upload(Path filePath, String key, String md5Digest) { |
| | | public UploadResult upload(Path filePath, String key, String md5Digest, String contentType) { |
| | | try { |
| | | // 构建上传请求对象 |
| | | FileUpload fileUpload = transferManager.uploadFile( |
| | |
| | | y -> y.bucket(properties.getBucketName()) |
| | | .key(key) |
| | | .contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null) |
| | | .contentType(contentType) |
| | | .acl(getAccessPolicy().getObjectCannedACL()) |
| | | .build()) |
| | | .addTransferListener(LoggingTransferListener.create()) |
| | | .source(filePath).build()); |
| | |
| | | * @param inputStream 要上传的输入流 |
| | | * @param key 在 Amazon S3 中的对象键 |
| | | * @param length 输入流的长度 |
| | | * @param contentType 文件内容类型 |
| | | * @return UploadResult 包含上传后的文件信息 |
| | | * @throws OssException 如果上传失败,抛出自定义异常 |
| | | */ |
| | | public UploadResult upload(InputStream inputStream, String key, Long length) { |
| | | public UploadResult upload(InputStream inputStream, String key, Long length, String contentType) { |
| | | // 如果输入流不是 ByteArrayInputStream,则将其读取为字节数组再创建 ByteArrayInputStream |
| | | if (!(inputStream instanceof ByteArrayInputStream)) { |
| | | inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream)); |
| | |
| | | .putObjectRequest( |
| | | y -> y.bucket(properties.getBucketName()) |
| | | .key(key) |
| | | .contentType(contentType) |
| | | .acl(getAccessPolicy().getObjectCannedACL()) |
| | | .build()) |
| | | .build()); |
| | | |
| | |
| | | * @throws OssException 如果上传失败,抛出自定义异常 |
| | | */ |
| | | public UploadResult uploadSuffix(byte[] data, String suffix) { |
| | | return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length)); |
| | | return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length), FileUtils.getMimeType(suffix)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws OssException 如果上传失败,抛出自定义异常 |
| | | */ |
| | | public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length) { |
| | | return upload(inputStream, getPath(properties.getPrefix(), suffix), length); |
| | | return upload(inputStream, getPath(properties.getPrefix(), suffix), length, FileUtils.getMimeType(suffix)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @throws OssException 如果上传失败,抛出自定义异常 |
| | | */ |
| | | public UploadResult uploadSuffix(File file, String suffix) { |
| | | return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null); |
| | | return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null, FileUtils.getMimeType(suffix)); |
| | | } |
| | | |
| | | /** |