| | |
| | | * @return PutObjectResult |
| | | */ |
| | | public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey, AccessControlList acl) { |
| | | // 创建ObsClient实例 |
| | | ObsClient obsClient = getObsClient(properties); |
| | | try { |
| | | // 创建ObsClient实例 |
| | | ObsClient obsClient = getObsClient(properties); |
| | | PutObjectRequest request = new PutObjectRequest(); |
| | | request.setBucketName(properties.getBucket()); |
| | | request.setObjectKey(objectKey); |
| | |
| | | throw new BadRequestException("上传对象失败" + e.getMessage()); |
| | | } finally { |
| | | FileUtil.del(file); |
| | | CloseUtil.close(obsClient); |
| | | } |
| | | } |
| | | |
| | |
| | | * @return PutObjectResult |
| | | */ |
| | | public static PutObjectResult putObject(ObsProperties properties, File file, String objectKey) { |
| | | // 创建ObsClient实例 |
| | | ObsClient obsClient = getObsClient(properties); |
| | | try { |
| | | // 创建ObsClient实例 |
| | | ObsClient obsClient = getObsClient(properties); |
| | | // 为待上传的本地文件路径,需要指定到具体的文件名 |
| | | return obsClient.putObject(properties.getBucket(), objectKey, file); |
| | | } catch (Exception e) { |
| | | throw new BadRequestException("上传对象失败" + e.getMessage()); |
| | | } finally { |
| | | FileUtil.del(file); |
| | | CloseUtil.close(obsClient); |
| | | } |
| | | } |
| | | |
| | |
| | | } finally { |
| | | CloseUtil.close(in); |
| | | CloseUtil.close(out); |
| | | CloseUtil.close(obsClient); |
| | | } |
| | | } |
| | | return path; |
| | |
| | | ObsClient obsClient = getObsClient(properties); |
| | | // 删除指定的对象 |
| | | obsClient.deleteObject(properties.getBucket(), objectKey); |
| | | CloseUtil.close(obsClient); |
| | | } |
| | | |
| | | public static String getPublicObjectUrl(ObsProperties properties, String objectKey) { |
| | | return getPublicObjectUrl(properties.getBucket(), properties.getEndpoint(), objectKey); |
| | | } |
| | | |
| | | public static String getPublicObjectUrl(String bucketName, String endpoint, String objectKey) { |
| | | // 移除objectKey开头可能存在的文件分割符('/') |
| | | String cleanObjectKey = objectKey.startsWith("/") ? objectKey.substring(1) : objectKey; |
| | | // 标准URL格式 |
| | | return String.format("https://%s.%s/%s", bucketName, endpoint.replaceAll("^https?://", ""), cleanObjectKey); |
| | | } |
| | | } |