### Maven Dependency for Ufile-Client-Java Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This snippet shows how to include the ufile-client-java dependency in your Maven project's pom.xml file to start using the SDK. ```xml cn.ucloud.ufile ufile-client-java latest-release-version ``` -------------------------------- ### Object Authorization with Local and Remote Options Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This snippet illustrates setting up authorization for object-related operations, showing both local private key usage and a more secure remote authorization approach. ```java // 对象相关API的授权器 ObjectAuthorization OBJECT_AUTHORIZER = new UfileObjectLocalAuthorization( "Your PublicKey", "Your PrivateKey"); /** * 您也可以创建远程对象相关API的授权器,远程授权器将签名私钥放于签名服务器上,更为安全 * 远程签名服务端示例代码在 (https://github.com/ucloud/ufile-sdk-auth-server) * 您也可以自行继承ObjectRemoteAuthorization来重写远程签名逻辑 */ ObjectAuthorization OBJECT_AUTHORIZER = new UfileObjectRemoteAuthorization( 您的公钥, new ObjectRemoteAuthorization.ApiConfig( "http://your_domain/applyAuth", "http://your_domain/applyPrivateUrlAuth" )); ``` -------------------------------- ### Create Bucket - Asynchronous Execution Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This Java code demonstrates the asynchronous execution of the createBucket API, utilizing a UfileCallback to handle responses and errors. ```java UfileClient.bucket(BUCKET_AUTHORIZER) .createBucket(bucketName, region, bucketType) .executeAsync(new UfileCallback() { @Override public void onResponse(BucketResponse response) { } @Override public void onError(Request request, ApiError error, UfileErrorBean response) { } }); ``` -------------------------------- ### Synchronous File Upload with Ufile SDK Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md Demonstrates how to upload a file synchronously using the Ufile SDK for Java. It includes setting up the file, specifying MIME type, assigning a key name, selecting a bucket, and executing the upload. Optional configurations for MD5 verification and progress callbacks are commented out. ```java File file = new File("your file path"); try { PutObjectResultBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config) .putObject(file, "mimeType") .nameAs("save as keyName") .toBucket("upload to which bucket") /** * 是否上传校验MD5, Default = true */ // .withVerifyMd5(false) /** * 指定progress callback的间隔, Default = 每秒回调 */ // .withProgressConfig(ProgressConfig.callbackWithPercent(10)) /** * 配置进度监听 */ .setOnProgressListener(new OnProgressListener() { @Override public void onProgress(long bytesWritten, long contentLength) { } }) .execute(); } catch (UfileClientException e) { e.printStackTrace(); } catch (UfileServerException e) { e.printStackTrace(); } ``` -------------------------------- ### Loading ObjectConfig from Local Profile Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This snippet demonstrates how to load the ObjectConfig settings from a local JSON profile file, which can contain region/proxy suffix or custom domain information. ```java /** * ObjectConfig同时支持从本地文件来导入 * 配置文件内容必须是含有以下参数的json字符串: * {"Region":"","ProxySuffix":""} * 或 * {"CustomDomain":""} */ try { ObjectConfig.loadProfile(new File("your config profile path")); } catch (UfileFileException e) { e.printStackTrace(); } ``` -------------------------------- ### Bucket Authorization and API Execution Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This snippet illustrates how to set up authorization for bucket-related operations using a local authorization mechanism and then execute bucket APIs. ```java // Bucket相关API的授权器 BucketAuthorization BUCKET_AUTHORIZER = new UfileBucketLocalAuthorization( "Your PublicKey", "Your PrivateKey"); UfileClient.bucket(BUCKET_AUTHORIZER) .APIs // Bucket相关操作API .execute() or executeAsync(UfileCallback) ``` -------------------------------- ### Create Bucket - Synchronous Execution Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This Java code shows the synchronous execution of the createBucket API, including basic error handling for client and server exceptions. ```java try { BucketResponse res = UfileClient.bucket(BUCKET_AUTHORIZER) .createBucket(bucketName, region, bucketType) .execute(); } catch (UfileClientException e) { e.printStackTrace(); } catch (UfileServerException e) { e.printStackTrace(); } ``` -------------------------------- ### Object API Execution Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This code shows how to execute object-related APIs after setting up authorization and configuration, supporting both synchronous and asynchronous calls. ```java UfileClient.object(OBJECT_AUTHORIZER, config) .APIs // 对象存储相关API .execute() or executeAsync(UfileCallback) ``` -------------------------------- ### Gradle Dependency for Ufile-Client-Java Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This snippet demonstrates how to add the ufile-client-java dependency to your Gradle project for integration. ```java dependencies { /* * your other dependencies */ implementation 'cn.ucloud.ufile:ufile-client-java:latest-release-version' } ``` -------------------------------- ### ObjectConfig for Region and Domain Suffix Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This code shows how to configure object operations by specifying the bucket's region and the Ufile domain suffix, or by using a custom domain. ```java // 对象操作需要ObjectConfig来配置您的地区和域名后缀 ObjectConfig config = new ObjectConfig("your bucket region", "ufileos.com"); /** * 您也可以使用已登记的自定义域名 * 注意'http://www.your_domain.com'指向的是某个特定的bucket+region+域名后缀, * eg:http://www.your_domain.com -> www.your_bucket.bucket_region.ufileos.com */ ObjectConfig config = new ObjectConfig("http://www.your_domain.com"); ``` -------------------------------- ### Asynchronous File Upload with Ufile SDK Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md Illustrates how to upload a file asynchronously using the Ufile SDK for Java. This method allows for background uploads and provides callbacks for progress, successful responses, and errors. It includes placeholders for file path, MIME type, key name, and bucket. ```java File file = new File("your file path"); UfileClient.object(OBJECT_AUTHORIZER, config) .putObject(file, "mimeType") .nameAs("save as keyName") .toBucket("upload to which bucket") /** * 是否上传校验MD5, Default = true */ // .withVerifyMd5(false) /** *指定progress callback的间隔, Default = 每秒回调 */ // .withProgressConfig(ProgressConfig.callbackWithPercent(10)) .executeAsync(new UfileCallback() { @Override public void onProgress(long bytesWritten, long contentLength) { } @Override public void onResponse(PutObjectResultBean response) { } @Override public void onError(Request request, ApiError error, UfileErrorBean response) { } }); ``` -------------------------------- ### Configure UfileClient with Custom HttpClient Config Source: https://github.com/ucloud/ufile-sdk-java/blob/master/README.md This code demonstrates how to configure the UfileClient with custom HTTP client settings, including connection pooling, timeouts, and interceptors. This configuration must be called before any other UfileClient method. ```java UfileClient.configure(new UfileClient.Config( new HttpClient.Config(int maxIdleConnections, long keepAliveDuration, TimeUnit keepAliveTimeUnit) .setTimeout(连接超时ms,读取超时ms,写入超时ms) .setExecutorService(线程池) .addInterceptor(okhttp3拦截器) .addNetInterceptor(okhttp3网络拦截器))); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.