@@ -166,7 +166,7 @@ public class COSClient implements COS {
166
166
167
167
private CosHttpClient cosHttpClient ;
168
168
169
- private ConcurrentHashMap <String , Long > preflightBuckets = new ConcurrentHashMap <>();
169
+ private static ConcurrentHashMap <String , Long > preflightBuckets = new ConcurrentHashMap <>();
170
170
171
171
public COSClient (COSCredentials cred , ClientConfig clientConfig ) {
172
172
this (new COSStaticCredentialsProvider (cred ), clientConfig );
@@ -1699,6 +1699,18 @@ public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest)
1699
1699
rejectNull (partSize , "The part size parameter must be specified when uploading a part" );
1700
1700
rejectNull (clientConfig .getRegion (),
1701
1701
"region is null, region in clientConfig must be specified when uploading a part" );
1702
+
1703
+ try {
1704
+ preflightObj (uploadPartRequest );
1705
+ } catch (CosServiceException cse ) {
1706
+ String msg = String .format ("fail to do the preflight request due to the service exception[statusCode:%s, requestId:%s], will not do the upload part request" , cse .getStatusCode (), cse .getRequestId ());
1707
+ log .warn (msg );
1708
+ throw cse ;
1709
+ } catch (CosClientException cce ) {
1710
+ log .warn ("fail to do the preflight request due to the client exception, will not do the upload part request" , cce );
1711
+ throw cce ;
1712
+ }
1713
+
1702
1714
CosHttpRequest <UploadPartRequest > request =
1703
1715
createRequest (bucketName , key , uploadPartRequest , HttpMethodName .PUT );
1704
1716
request .addParameter ("uploadId" , uploadId );
@@ -1762,6 +1774,21 @@ private UploadPartResult doUploadPart(final String bucketName, final String key,
1762
1774
ObjectMetadata metadata = invoke (request , new CosMetadataResponseHandler ());
1763
1775
final String etag = metadata .getETag ();
1764
1776
1777
+ if (metadata .isNeedPreflight ()) {
1778
+ Long currentTime = System .currentTimeMillis ();
1779
+ if ((preflightBuckets .get (bucketName ) == null ) || ((currentTime - preflightBuckets .get (bucketName )) > clientConfig .getPreflightStatusUpdateInterval ())) {
1780
+ String reqMsg = String .format ("will update preflight status, bucket[%s]" , bucketName );
1781
+ log .info (reqMsg );
1782
+ preflightBuckets .put (bucketName , currentTime );
1783
+ }
1784
+ } else {
1785
+ Long currentTime = System .currentTimeMillis ();
1786
+ if ((preflightBuckets .get (bucketName ) != null ) && ((currentTime - preflightBuckets .get (bucketName )) > clientConfig .getPreflightStatusUpdateInterval ())) {
1787
+ String reqMsg = String .format ("will remove bucket[%s] from preflight lists" , bucketName );
1788
+ log .info (reqMsg );
1789
+ preflightBuckets .remove (bucketName );
1790
+ }
1791
+ }
1765
1792
1766
1793
if (md5DigestStream != null && !skipMd5CheckStrategy
1767
1794
.skipClientSideValidationPerUploadPartResponse (metadata )) {
@@ -5576,5 +5603,37 @@ private void preflightObj(PutObjectRequest putObjectRequest) throws CosClientExc
5576
5603
invoke (request , voidCosResponseHandler );
5577
5604
}
5578
5605
}
5606
+
5607
+ private void preflightObj (UploadPartRequest uploadPartRequest ) throws CosClientException , CosServiceException {
5608
+ String bucketName = uploadPartRequest .getBucketName ();
5609
+ String key = uploadPartRequest .getKey ();
5610
+ rejectEmpty (bucketName ,
5611
+ "The bucket name parameter must be specified when doing preflight request" );
5612
+ rejectEmpty (key ,
5613
+ "The key parameter must be specified when doing preflight request" );
5614
+ if (clientConfig .isCheckPreflightStatus () && preflightBuckets .containsKey (bucketName )) {
5615
+ String reqMsg = String .format ("will do preflight request for upload part object[%s] to the bucket[%s]" , key , bucketName );
5616
+ log .debug (reqMsg );
5617
+ uploadPartRequest .setHasDonePreflight (true );
5618
+ CosServiceRequest serviceRequest = new CosServiceRequest ();
5619
+ Map <String , String > customHeaders = uploadPartRequest .getCustomRequestHeaders ();
5620
+ if (customHeaders != null ) {
5621
+ for (Map .Entry <String , String > e : customHeaders .entrySet ()) {
5622
+ serviceRequest .putCustomRequestHeader (e .getKey (), e .getValue ());
5623
+ }
5624
+ }
5625
+ CosHttpRequest <CosServiceRequest > request = createRequest (bucketName , key , serviceRequest , HttpMethodName .HEAD );
5626
+ if (uploadPartRequest .getFixedEndpointAddr () != null ) {
5627
+ request .setEndpoint (uploadPartRequest .getFixedEndpointAddr ());
5628
+ }
5629
+ request .addParameter ("preflight" , null );
5630
+ ObjectMetadata metadata = uploadPartRequest .getObjectMetadata ();
5631
+ if (metadata != null ) {
5632
+ populateRequestMetadata (request , metadata );
5633
+ }
5634
+ request .addHeader ("x-cos-next-action" , "UploadPart" );
5635
+ invoke (request , voidCosResponseHandler );
5636
+ }
5637
+ }
5579
5638
}
5580
5639
0 commit comments