| | |
| | | } |
| | | |
| | | /** |
| | | * 直接上传本地文件到COS |
| | | */ |
| | | public String uploadLocalFile(java.io.File file, String fileName) throws Exception { |
| | | // 生成文件路径:按日期分目录 |
| | | String dateDir = new java.text.SimpleDateFormat("yyyyMMdd").format(new Date()); |
| | | String key = dateDir + "/" + fileName; |
| | | |
| | | System.out.println("=== COS本地文件上传调试信息 ==="); |
| | | System.out.println("文件Key: " + key); |
| | | System.out.println("文件大小: " + file.length()); |
| | | System.out.println("文件路径: " + file.getAbsolutePath()); |
| | | |
| | | // 创建COS客户端 |
| | | COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); |
| | | ClientConfig clientConfig = new ClientConfig(new Region(region)); |
| | | COSClient cosClient = new COSClient(cred, clientConfig); |
| | | |
| | | try { |
| | | // 创建上传请求 |
| | | com.qcloud.cos.model.PutObjectRequest putObjectRequest = |
| | | new com.qcloud.cos.model.PutObjectRequest(bucket, key, file); |
| | | |
| | | // 执行上传 |
| | | com.qcloud.cos.model.PutObjectResult result = cosClient.putObject(putObjectRequest); |
| | | System.out.println("上传成功,ETag: " + result.getETag()); |
| | | |
| | | return key; // 返回相对路径 |
| | | } finally { |
| | | cosClient.shutdown(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取文件访问 URL |
| | | */ |
| | | public String getFileUrl(String key) { |