|
39 | 39 | import java.util.Map; |
40 | 40 | import java.util.Map.Entry; |
41 | 41 | import java.util.Objects; |
| 42 | +import java.util.concurrent.ConcurrentHashMap; |
42 | 43 |
|
43 | 44 | import com.fasterxml.jackson.core.Version; |
44 | 45 | import com.fasterxml.jackson.databind.ObjectMapper; |
@@ -165,6 +166,8 @@ public class COSClient implements COS { |
165 | 166 |
|
166 | 167 | private CosHttpClient cosHttpClient; |
167 | 168 |
|
| 169 | + private ConcurrentHashMap<String, Long> preflightBuckets = new ConcurrentHashMap<>(); |
| 170 | + |
168 | 171 | public COSClient(COSCredentials cred, ClientConfig clientConfig) { |
169 | 172 | this(new COSStaticCredentialsProvider(cred), clientConfig); |
170 | 173 | } |
@@ -840,6 +843,17 @@ ObjectMetadata uploadObjectInternal(UploadMode uploadMode, UploadObjectRequest u |
840 | 843 | rejectNull(bucketName, |
841 | 844 | "The bucket name parameter must be specified when uploading an object"); |
842 | 845 | rejectNull(key, "The key parameter must be specified when uploading an object"); |
| 846 | + |
| 847 | + try { |
| 848 | + preflightObj(bucketName, key); |
| 849 | + } catch (CosServiceException cse) { |
| 850 | + log.warn("fail to do the preflight request due to the service exception, will not do the upload obj request", cse); |
| 851 | + throw cse; |
| 852 | + } catch (CosClientException cce) { |
| 853 | + log.warn("fail to do the preflight request due to the client exception, will not do the upload obj request", cce); |
| 854 | + throw cce; |
| 855 | + } |
| 856 | + |
843 | 857 | // If a file is specified for upload, we need to pull some additional |
844 | 858 | // information from it to auto-configure a few options |
845 | 859 | if (file == null) { |
@@ -979,6 +993,22 @@ ObjectMetadata uploadObjectInternal(UploadMode uploadMode, UploadObjectRequest u |
979 | 993 | CosDataSource.Utils.cleanupDataSource(uploadObjectRequest, file, isOrig, input, log); |
980 | 994 | } |
981 | 995 |
|
| 996 | + if (returnedMetadata.isNeedPreflight()) { |
| 997 | + Long currentTime = System.currentTimeMillis(); |
| 998 | + if ((preflightBuckets.get(bucketName) == null) || ((currentTime - preflightBuckets.get(bucketName)) > clientConfig.getPreflightStatusUpdateInterval())) { |
| 999 | + String reqMsg = String.format("will update preflight status, bucket[%s]",bucketName); |
| 1000 | + log.info(reqMsg); |
| 1001 | + preflightBuckets.put(bucketName, currentTime); |
| 1002 | + } |
| 1003 | + } else { |
| 1004 | + Long currentTime = System.currentTimeMillis(); |
| 1005 | + if ((preflightBuckets.get(bucketName) != null) && ((currentTime - preflightBuckets.get(bucketName)) > clientConfig.getPreflightStatusUpdateInterval())) { |
| 1006 | + String reqMsg = String.format("will remove bucket[%s] from preflight lists",bucketName); |
| 1007 | + log.info(reqMsg); |
| 1008 | + preflightBuckets.remove(bucketName); |
| 1009 | + } |
| 1010 | + } |
| 1011 | + |
982 | 1012 | String contentMd5 = metadata.getContentMD5(); |
983 | 1013 | if (md5DigestStream != null) { |
984 | 1014 | contentMd5 = Base64.encodeAsString(md5DigestStream.getMd5Digest()); |
@@ -5475,5 +5505,22 @@ public BucketGetMetadataResult getBucketMetadata(String bucketName) throws CosCl |
5475 | 5505 |
|
5476 | 5506 | return result; |
5477 | 5507 | } |
| 5508 | + |
| 5509 | + private void preflightObj(String bucketName, String key) throws CosClientException, CosServiceException { |
| 5510 | + rejectEmpty(bucketName, |
| 5511 | + "The bucket name parameter must be specified when doing preflight request"); |
| 5512 | + rejectEmpty(key, |
| 5513 | + "The key parameter must be specified when doing preflight request"); |
| 5514 | + if (clientConfig.isCheckPreflightStatus() && preflightBuckets.containsKey(bucketName)) { |
| 5515 | + String reqMsg = String.format("will do preflight request for put object[%s] to the bucket[%s]", key, bucketName); |
| 5516 | + log.debug(reqMsg); |
| 5517 | + CosServiceRequest serviceRequest = new CosServiceRequest(); |
| 5518 | + CosHttpRequest<CosServiceRequest> request = createRequest(bucketName, key, serviceRequest, HttpMethodName.HEAD); |
| 5519 | + request.addParameter("preflight", null); |
| 5520 | + request.addHeader("x-cos-next-action", "PutObject"); |
| 5521 | + |
| 5522 | + invoke(request, voidCosResponseHandler); |
| 5523 | + } |
| 5524 | + } |
5478 | 5525 | } |
5479 | 5526 |
|
0 commit comments