edit-icon download-icon

Quick start

Last Updated: Aug 25, 2018

This topic describes how to use OSS Java SDK to complete routine operations such as bucket creation, objects uploads, and object downloads.

Create a bucket

A bucket is a global namespace in OSS. It is similar to a data container that stores files. Use the following code to create a bucket:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. // Create a bucket.
  10. ossClient.createBucket(bucketName);
  11. // Close your OSSClient instance.
  12. ossClient.shutdown();

For more information about bucket naming rules, see naming conventions in Basic concepts. For more information about how to create a bucket, see Manage a bucket.

Upload objects

Use the following code to upload a file to OSS:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Upload a file.
  11. String content = "Hello OSS";
  12. ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
  13. // Close your OSSClient instance.
  14. ossClient.shutdown();

For more information, see Upload objects.

Download objects

Use the following code to obtain object content:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Call ossClient.getObject to return an OSSObject instance. The OSSObject instance contains the object content and Object Meta.
  11. OSSObject ossObject = ossClient.getObject(bucketName, objectName);
  12. // Call ossObject.getObjectContent to obtain object InputStream. Read the InputStream to obtain object content.
  13. InputStream content = ossObject.getObjectContent();
  14. if (content != null) {
  15. BufferedReader reader = new BufferedReader(new InputStreamReader(content));
  16. while (true) {
  17. String line = reader.readLine();
  18. if (line == null) break;
  19. System.out.println("\n" + line);
  20. }
  21. // If you do not close the reader after the data is read, connection leaks may occur. Consequently, no available connections are left and an exception occurs.
  22. content.close();
  23. }
  24. // Close your OSSClient instance.
  25. ossClient.shutdown();

For more information, see Download objects.

List objects

Use the following code to list objects in a bucket. You can list a maximum of 100 objects by default.

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. // Create an OSSClient instance.
  8. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  9. // Use ossClient.listObjects to return an ObjectListing instance. The ObjectListing instance contains the response from listObject.
  10. ObjectListing objectListing = ossClient.listObjects(bucketName);
  11. // Use objectListing.getObjectSummaries to obtain the descriptions of all objects.
  12. for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
  13. System.out.println(" - " + objectSummary.getKey() + " " +
  14. "(size = " + objectSummary.getSize() + ")");
  15. }
  16. // Close your OSSClient instance.
  17. ossClient.shutdown();

For more information, see List objects.

Delete objects

Use the following code to delete a specified object:

  1. // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";
  8. // Create an OSSClient instance.
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
  10. // Delete objects.
  11. ossClient.deleteObject(bucketName, objectName);
  12. // Close your OSSClient instance.
  13. ossClient.shutdown();

For more information, see Delete objects. For the complete object deletion code, see GitHub.

Thank you! We've received your feedback.