|
1 | 1 | package com.qcloud.cos_migrate_tool.task; |
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.BufferedOutputStream; |
4 | 5 | import java.io.File; |
5 | 6 | import java.io.FileNotFoundException; |
@@ -204,24 +205,39 @@ private String uploadBigFile(PutObjectRequest putObjectRequest) throws Interrupt |
204 | 205 |
|
205 | 206 | private String uploadSmallFile(PutObjectRequest putObjectRequest) throws InterruptedException { |
206 | 207 | Upload upload = smallFileTransfer.upload(putObjectRequest); |
207 | | - return showTransferProgressAndGetRequestId(upload, false, putObjectRequest.getKey(), |
208 | | - putObjectRequest.getFile().lastModified()); |
| 208 | + File file = putObjectRequest.getFile(); |
| 209 | + long mtime = 0; |
| 210 | + if(file != null){ |
| 211 | + mtime = file.lastModified(); |
| 212 | + } |
| 213 | + return showTransferProgressAndGetRequestId(upload, false, putObjectRequest.getKey(), mtime); |
209 | 214 | } |
210 | 215 |
|
211 | 216 |
|
212 | 217 | public String uploadFile(String bucketName, String cosPath, File localFile, |
213 | 218 | StorageClass storageClass, boolean entireMd5Attached, ObjectMetadata objectMetadata, |
214 | 219 | AccessControlList acl) throws Exception { |
215 | | - PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, cosPath, localFile); |
| 220 | + PutObjectRequest putObjectRequest; |
| 221 | + if(localFile.isDirectory()) { |
| 222 | + if(!cosPath.endsWith("/")) { |
| 223 | + cosPath += "/"; |
| 224 | + } |
| 225 | + byte[] contentByteArray = new byte[0]; |
| 226 | + objectMetadata.setContentType("application/x-directory"); |
| 227 | + objectMetadata.setContentLength(0); |
| 228 | + putObjectRequest = new PutObjectRequest(bucketName, cosPath, new ByteArrayInputStream(contentByteArray), |
| 229 | + objectMetadata); |
| 230 | + } else { |
| 231 | + putObjectRequest = new PutObjectRequest(bucketName, cosPath, localFile); |
| 232 | + } |
216 | 233 | putObjectRequest.setStorageClass(storageClass); |
217 | 234 |
|
218 | 235 | if (acl != null) { |
219 | 236 | putObjectRequest.setAccessControlList(acl); |
220 | 237 | } |
221 | 238 |
|
222 | | - if (entireMd5Attached) { |
| 239 | + if (entireMd5Attached && !localFile.isDirectory()) { |
223 | 240 | String md5 = Md5Utils.md5Hex(localFile); |
224 | | - |
225 | 241 | String upyunTag = objectMetadata.getUserMetaDataOf("upyun-etag"); |
226 | 242 | if (upyunTag != null) { |
227 | 243 | if (!md5.equalsIgnoreCase(upyunTag)) { |
|
0 commit comments