This topic describes how to use Object Storage Service (OSS) SDK for Java to perform routine operations, such as creating buckets, uploading objects, and downloading objects.

Sample projects

OSS SDK for Java provides sample projects for Maven and Ant. You can compile and run the sample projects on local devices or develop your own applications based on the sample projects. For more information about how to compile and run a project, see README.md in the project directory.

Create buckets

A bucket is a global namespace in OSS. A bucket is a container for objects stored in OSS.

Note For more information about endpoints, see Regions and endpoints. For more information about the naming conventions for buckets, see the "bucket" section in Terms.

The following code provides an example on how to create a bucket named examplebucket:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket. 
String bucketName = "examplebucket";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Create the bucket. 
ossClient.createBucket(bucketName);

// Shut down the OSSClient instance. 
ossClient.shutdown();            

For more information about how to create buckets, see Create buckets.

Upload objects

The following code provides an example on how to upload a local file to OSS:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket to which you want to upload the object. Example: examplebucket. 
String bucketName = "examplebucket";
// Specify the name of the object. The name of the object contains the object path, but not the bucket name. Example: exampledir/exampleobject.txt. 
String objectName = "exampledir/exampleobject.txt";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

String content = "Hello OSS";
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));

// Shut down the OSSClient instance. 
ossClient.shutdown();            

For more information about how to upload objects to OSS, see Overview.

Download objects

The following code provides an example on how to download an object from OSS:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket from which you want to download an object. Example: examplebucket. 
String bucketName = "examplebucket";
// Specify the name of the object. The name of the object contains the object path, but not the bucket name. Example: exampledir/exampleobject.txt. 
String objectName = "exampledir/exampleobject.txt";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Call ossClient.getObject to query an OSSObject instance. The OSSObject instance contains the content and metadata of the object. 
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
// Call the ossObject.getObjectContent method to obtain an input stream of the object. The input stream can be read to obtain the content of the object. 
InputStream content = ossObject.getObjectContent();
if (content != null) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    while (true) {
        String line = reader.readLine();
        if (line == null) break;
        System.out.println("\n" + line);
    }
    // You must close the connection after the object is read to prevent connection leaks. Otherwise, request connections may become unavailable and the program cannot run properly. 
    content.close();
}

// Shut down the OSSClient instance. 
ossClient.shutdown();            

For more information about how to download objects from OSS, see Overview.

List objects

The following code provides an example on how to list objects stored in a bucket named examplebucket. By default, up to 100 objects can be listed.

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket in which the objects you want to list are stored. Example: examplebucket. 
String bucketName = "examplebucket";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Call the ossClient.listObjects method to obtain an ObjectListing instance. The ObjectListing instance contains the response to the listObject request. 
ObjectListing objectListing = ossClient.listObjects(bucketName);
// Call objectListing.getObjectSummaries to query the descriptions of all objects. 
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
    System.out.println(" - " + objectSummary.getKey() + "  " +
            "(size = " + objectSummary.getSize() + ")");
}

// Shut down the OSSClient instance. 
ossClient.shutdown();            

For more information about how to list objects, see List objects.

Delete objects

Note For the complete code used to delete objects, visit GitHub.

The following code provides an example on how to delete an object:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket in which the object you want to delete is stored. Example: examplebucket. 
String bucketName = "examplebucket";
// Specify the name of the object. The name of the object contains the object path, but not the bucket name. Example: exampledir/exampleobject.txt. 
String objectName = "exampledir/exampleobject.txt";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Delete the object. 
ossClient.deleteObject(bucketName, objectName);

// Shut down the OSSClient instance. 
ossClient.shutdown();            

For more information about how to delete objects, see Delete objects.