Java initialization
prerequisite
New OSSClient
OSSClient is thread safe, allowing multiple threads to access the same instance. You can reuse the same OSSClient instance according to business requirements, or create multiple OSSClient instances for separate use. The OSS Client instance maintains a connection pool internally. When the OSSClient instance is no longer in use, please call the shutdown method to close it, so as to avoid creating too many OSSClient instances and causing resource exhaustion.
V4 signature (recommended)
//YourEndpoint fills in the endpoint corresponding to the bucket's region. Taking East China 1 (Hangzhou) as an example, the Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com 。 String endpoint = " https://oss-cn-hangzhou.aliyuncs.com "; //Fill in the region information corresponding to the Endpoint, such as cn hangzhou. String region = "cn-hangzhou"; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create an OSSClient instance. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion. V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); //Close the OSSClient. ossClient.shutdown();
V1 signature (not recommended)
Create an OSSClient using the OSS domain name
//YourEndpoint fills in the endpoint corresponding to the bucket's region. Taking East China 1 (Hangzhou) as an example, the Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com 。 String endpoint = "yourEndpoint"; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider); //Close the OSSClient. ossClient.shutdown();
Create a new OSSClient using a custom domain name
//YourEndpoint fills in the custom domain name. String endpoint = "yourEndpoint"; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create a ClientBuilderConfiguration instance, and you can modify the default parameters according to the actual situation. ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); //Set whether CNAME is supported. CNAME is used to bind a custom domain name to the target bucket. conf.setSupportCname(true); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider, conf); //Close the OSSClient. ossClient.shutdown();
Create an OSSClient using STS
Please refer to Use STS temporary access credentials to access OSS 。 You can call the STS service's AssumeRole Interface or use STS SDK in all languages To obtain temporary access credentials. Temporary access credentials include temporary access keys (AccessKeyId and AccessKeySecret) and security tokens (SecurityToken). The valid time unit of temporary access credentials is seconds, the minimum value is 900, and the maximum value is subject to the maximum session time set by the current role. For more information, see Set the maximum session time of RAM role 。 For a complete code example, see Authorized access 。
//YourEndpoint fills in the endpoint corresponding to the bucket's region. Taking East China 1 (Hangzhou) as an example, the Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com 。 String endpoint = "yourEndpoint"; //Obtain the access key (AccessKey ID and AccessKey Secret) and security token (SecurityToken) of STS from the environment variable as the access credential. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider); //Close the OSSClient. ossClient.shutdown();
Use STSAssumeRole to create a new OSSClient
//The region that STSAssumeRole is authorized to access. Take East China 1 (Hangzhou) as an example. Please fill in other regions according to the actual situation. String region = "cn-hangzhou"; //Obtain the access key (AccessKeyId and AccessKeySecret) of the RAM user from the environment variable. Please configure the environment variables before running this code example. String accessKeyId = System.getenv("ALIYUN_ACCESS_KEY_ID"); String accessKeySecret = System.getenv("ALIYUN_ACCESS_KEY_SECRET"); //Get RamRoleArn of RAM role from environment variable. Please configure the environment variables before running this code example. String roleArn = System.getenv("ALIYUN_STS_ROLE_ARN"); //Create an instance of STSAssumeRoleSessionCredentialsProvider. STSAssumeRoleSessionCredentialsProvider credentialsProvider = CredentialsProviderFactory.newSTSAssumeRoleSessionCredentialsProvider( region, accessKeyId, accessKeySecret, roleArn); //YourEndpoint fills in the endpoint corresponding to the bucket's region. Taking East China 1 (Hangzhou) as an example, the Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com 。 String endpoint = "yourEndpoint"; //Create a ClientBuilderConfiguration instance. ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider, conf); //Close the OSSClient. ossClient.shutdown();
Use EcsRamRole to create a new OSSClient
//Take East China 1 (Hangzhou) as an example. Please fill in other endpoints according to the actual situation. String endpoint = " https://oss-cn-hangzhou.aliyuncs.com "; //Access credentials are obtained through ECS RAM roles. For example, take the role name (ecs ram role) access as an example. InstanceProfileCredentialsProvider credentialsProvider = CredentialsProviderFactory.newInstanceProfileCredentialsProvider("ecs-ram-role"); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider, new ClientBuilderConfiguration()); //Close the OSSClient. ossClient.shutdown();
Create a new OSSClient in a VPC or VPC domain environment
//YourEndpoint fills in the endpoint corresponding to the bucket's region. String endpoint = "yourEndpoint"; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create a ClientBuilderConfiguration instance, and you can modify the default parameters according to the actual situation. ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); //Turn off the CNAME option. conf.setSupportCname(false); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider, conf); //Close the OSSClient. ossClient.shutdown();
Create a new OSSClient using IP
//In some special cases (such as private domain), you need to use the IP address as an endpoint. Please fill in according to the actual IP address. String endpoint = " https://10.10.10.10 "; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create ClientBuilderConfiguration. ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); //Enable the secondary domain name to access OSS. It is not enabled by default. The OSS Java SDK 2.1.2 and earlier versions need to set this value. The OSS Java SDK 2.1.2 and later versions will automatically detect the IP address, and do not need to set this value again. conf.setSLDEnabled(true); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider, conf); //Close the OSSClient. ossClient.shutdown();
Configure OSSClient
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
//YourEndpoint fills in the endpoint corresponding to the bucket's region. Taking East China 1 (Hangzhou) as an example, the Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com 。 String endpoint = "yourEndpoint"; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create ClientBuilderConfiguration. //ClientBuilderConfiguration is the OSSClient configuration class, which can configure parameters such as proxy, connection timeout, and maximum number of connections. ClientBuilderConfiguration conf = new ClientBuilderConfiguration(); //Set the maximum number of HTTP connections that OSSClient allows to open. The default number is 1024. conf.setMaxConnections(200); //Set the timeout for data transmission in the Socket layer, which is 50000 milliseconds by default. conf.setSocketTimeout(10000); //Set the timeout for establishing a connection. The default is 50000 milliseconds. conf.setConnectionTimeout(10000); //Set the timeout time (in milliseconds) for getting connections from the connection pool. By default, it does not timeout. conf.setConnectionRequestTimeout(1000); //Set the connection idle timeout. If timeout occurs, the connection will be closed. The default is 60000 milliseconds. conf.setIdleConnectionTime(10000); //Set the number of failed request retries, which is 3 by default. conf.setMaxErrorRetry(5); //Set whether to support the custom domain name as the endpoint, which is supported by default. conf.setSupportCname(true); //Set whether to enable the access mode of the secondary domain name. It is not enabled by default. conf.setSLDEnabled(true); //Set the protocol (HTTP or HTTPS) used to connect to OSS. The default is HTTP. conf.setProtocol(Protocol. HTTP); //Set the user agent, which refers to the HTTP User Agent header. The default is aliyun sdk java. conf.setUserAgent("aliyun-sdk-java"); //To set the proxy server IP, please replace "<yourProxyHost>" with the IP address of the proxy server (such as "196.128. xxx. xxx"). conf.setProxyHost("<yourProxyHost>"); //To set the user name for proxy server authentication, please replace "<yourProxyUserName>" with the user name of the proxy server (such as "root"). conf.setProxyUsername("<yourProxyUserName>"); //To set the password for proxy server authentication, please replace "<yourProxyPassword>" with the user's authentication password. conf.setProxyPassword("<yourProxyPassword>"); //Set whether to enable HTTP redirection. It is enabled by default. conf.setRedirectEnable(true); //Set whether to enable SSL certificate verification. It is enabled by default. conf.setVerifySSLEnable(true); //Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider, conf); //Close the OSSClient. ossClient.shutdown();
Retry Policy
When the request is of POST type, it is not retried by default. When the request is of non POST type and meets any of the following conditions, OSS will retry according to the default retry policy. The maximum number of retries is 3. When the exception is ClientException, the error code (errorCode) is ConnectionTimeout, SocketTimeout, ConnectionRefused, Unknown Host, and SocketException. When the exception is OSSException, an error code other than InvalidResponse is returned. The returned status codes are 500, 502, and 503.
//YourEndpoint fills in the endpoint corresponding to the bucket's region. Taking East China 1 (Hangzhou) as an example, the Endpoint is filled in as https://oss-cn-hangzhou.aliyuncs.com 。 String endpoint = "yourEndpoint"; //Get access credentials from environment variables. Please configure the environment variables before running this code example. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); //Create ClientBuilderConfiguration. //ClientBuilderConfiguration is the configuration class of OSSClient, which can be used to configure parameters such as retry times, user-defined retry policies, and connection timeouts. ClientBuilderConfiguration conf= new ClientBuilderConfiguration(); //Set the number of failed request retries. The default value is 3. conf.setMaxErrorRetry(5); //Custom retry policy, generally not recommended. //Assuming that the TestRetryStrategy class is your customized retry policy, the TestRetryStrategy class needs to inherit RetryStrategy. conf.setRetryStrategy(new TestRetryStrategy()); //Create an OSSClient instance. OSS ossClient=new OSSClientBuilder().build(endpoint, credentialsProvider, conf); //Close the OSSClient. ossClient.shutdown();
Next Steps
-
Introduction to this page (1)