java.lang.NoSuchMethodError is reported when I run the SDK?NoSuchMethodError.
Solutions:
Solution 1: Change the version of the package in your project that has caused NoSuchMethodError to the version of the corresponding library in pom.xml in the SDK.
Solution 2: Replace cos-java-sdk with cos_api-bundle. In this case, all dependencies of cos-java-sdk are installed independently, so more space will be used.<groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>5.6.35</version>
SetConnectionTimeoutMs and setSocketTimeout methods in the SDK to adjust them respectively.IOException is frequently printed in the log?IOException is printed in WARN logs, it can be ignored, and the SDK will retry. IOException may be caused by a slow network connection. For solutions, see 1 and 2 above.<groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version>
cos-java-sdk with cos_api-bundle. In this case, all dependencies of cos-java-sdk are installed independently, so more space will be used.<groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>5.6.35</version>
cos-java-sdk with cos_api-bundle. In this case, all dependencies of cos-java-sdk are installed independently, so more space will be used.<groupId>com.qcloud</groupId><artifactId>cos_api-bundle</artifactId><version>5.6.35</version>
/. When creating a file, you don't need to create a directory. For example, when you create a file with the object key xxx/yyy/zzz.txt, set the key to xxx/yyy/zzz.txt instead of creating the object xxx/yyy/. Directories are separated by / to show the hierarchy when displayed in the console. To create a directory object, use the following sample code:String bucketName = "examplebucket-1250000000";String key = "folder/images/";// A directory object is an empty file that ends with `/`, where a byte stream with a length of 0 is uploaded.InputStream input = new ByteArrayInputStream(new byte[0]);ObjectMetadata objectMetadata = new ObjectMetadata();objectMetadata.setContentLength(0);PutObjectRequest putObjectRequest =new PutObjectRequest(bucketName, key, input, objectMetadata);PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
ClientConfig class. Below is the sample code:// Initialize the user authentication information (`secretId` and `secretKey`).String secretId = "COS_SECRETID";String secretKey = "COS_SECRETKEY";COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// Set the bucket region. For the abbreviations for COS regions, visit https://www.tencentcloud.com/document/product/436/6224.ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));// Configure HTTPS.clientConfig.setHttpProtocol(HttpProtocol.https);// Generate a COS client.COSClient cosClient = new COSClient(cred, clientConfig);
ClientConfig class. Below is the sample code:// Initialize the user authentication information (`secretId` and `secretKey`).String secretId = "COS_SECRETID";String secretKey = "COS_SECRETKEY";COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);// Set the bucket region. For the abbreviations for COS regions, visit https://www.tencentcloud.com/document/product/436/6224.ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));// Configure a proxy (both the IP and port need to be set).// Set the proxy IP (or pass in a domain name).clientConfig.setHttpProxyIp("192.168.2.3");// Set the proxy port.clientConfig.setHttpProxyPort(8080);// Generate a COS client.COSClient cosClient = new COSClient(cred, clientConfig);
EndpointBuilder?buildGeneralApiEndpoint and buildGetServiceApiEndpoint functions in the EndpointBuilder API to specify the remote endpoints for general API requests and GETService requests respectively. Below is the sample code:// Step 1. Implement the two functions in the `EndpointBuilder` API.class SelfDefinedEndpointBuilder implements EndpointBuilder {@Overridepublic String buildGeneralApiEndpoint(String bucketName) {return String.format("%s.%s", bucketName, "mytest.com");}@Overridepublic String buildGetServiceApiEndpoint() {return "service.mytest.com";}}// Step 2. Initialize the client.String secretId = "COS_SECRETID";String secretKey = "COS_SECRETKEY";COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);SelfDefinedEndpointBuilder selfDefinedEndpointBuilder = new SelfDefinedEndpointBuilder();ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));clientConfig .setEndpointBuilder(selfDefinedEndpointBuilder);COSClient cosClient = new COSClient(cred, clientConfig);
/ prefix to key values used in SDK operations such as upload, download, and batch deletion?/ for COS objects. For example, if you set the key value of a COS object to exampleobject, you can access it at the http://cos.ap-guangzhou.myqcloud.com/exampleobject URL./ in a batch deletion request; otherwise, object deletion will fail.getProgress() method of Upload to get the TransferProgress class.CRC64 localCRC = new CRC64();// Calculate the local CRC64 value.localCRC.update();//...// Convert the CRC64 returned by COS into Long:cosCRC = crc64ToLong(strCOSCRC);// Perform comparison.if (cosCRC == localCRC.getValue()) {xxx}// Convert the CRC64 returned by COS into a Long in Java.long crc64ToLong(String crc64) {if (crc64.charAt(0) == '-') {return negativeCrc64ToLong(crc64);} else {return positiveCrc64ToLong(crc64);}}long positiveCrc64ToLong(String strCrc64) {BigInteger crc64 = new BigInteger(strCrc64);BigInteger maxLong = new BigInteger(Long.toString(Long.MAX_VALUE));int maxCnt = 0;while (crc64.compareTo(maxLong) > 0) {crc64 = crc64.subtract(maxLong);maxCnt++;}return crc64.longValue() + Long.MAX_VALUE * maxCnt;}long negativeCrc64ToLong(String strCrc64) {BigInteger crc64 = new BigInteger(strCrc64);BigInteger minLong = new BigInteger(Long.toString(Long.MIN_VALUE));int minCnt = 0;while (crc64.compareTo(minLong) < 0) {crc64 = crc64.subtract(minLong);minCnt++;}return crc64.longValue() + Long.MIN_VALUE * minCnt;}
prefix parameter to specify the directory prefix.COSClient.shutdown() method to shut down the instance when it is no longer needed.
If you need to create a new instance, shut down the previous one first. We recommend you use only one COSClient in a thread and call COSClient.shutdown() after all programs end and exit.TransferManager.shutdownNow(false) after all programs end and exit.
In addition, if another COSClient is reused when a TransferManager instance is created, add the parameter false (i.e., TransferManager.shutdownNow(false)) when closing the instance. This avoids closing the reused COSClients and thus causing errors in other places where it is used.Feedback