Quick start

Last Updated: Aug 08, 2017

The basic object upload and download processes are demonstrated below. For details, refer to the following directories of this project:

Step 1. Initialize the OSSClient

The initialization process mainly includes the following steps: endpoint settings, authentication mode settings, and client parameter settings. Two authentication modes are available: self-signed mode, and STS authentication mode. For details about authentication, refer to the “Access Control” section.

  1. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  2. // We recommend that you initialize OSSClient using STS on the mobile terminal. For more authentication modes, refer to the "Access Control" section.
  3. OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");
  4. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);

Initialization of upload and download requests through the OSSClient is thread-safe. You can execute multiple tasks in concurrency.

Step 2. Upload a file

Suppose you already have a bucket in the OSS console. You can use the following code to upload a local file to OSS:

  1. // Construct an upload request
  2. PutObjectRequest put = new PutObjectRequest("<bucketName>", "<objectKey>", "<uploadFilePath>");
  3. // You can set progress callback during asynchronous upload
  4. put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
  5. @Override
  6. public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
  7. Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
  8. }
  9. });
  10. OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
  11. @Override
  12. public void onSuccess(PutObjectRequest request, PutObjectResult result) {
  13. Log.d("PutObject", "UploadSuccess");
  14. }
  15. @Override
  16. public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  17. // Request exception
  18. if (clientExcepion != null) {
  19. // Local exception, such as a network exception
  20. clientExcepion.printStackTrace();
  21. }
  22. if (serviceException != null) {
  23. // Service exception
  24. Log.e("ErrorCode", serviceException.getErrorCode());
  25. Log.e("RequestId", serviceException.getRequestId());
  26. Log.e("HostId", serviceException.getHostId());
  27. Log.e("RawMessage", serviceException.getRawMessage());
  28. }
  29. }
  30. });
  31. // task.cancel(); // You can cancel the task
  32. // task.waitUntilFinished(); // You can wait until the task is completed

Step 3. Download a specified object

The following code downloads the specified ‘object’ (you need to handle the input stream of the returned data):

  1. // Construct an object download request
  2. GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>");
  3. OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
  4. @Override
  5. public void onSuccess(GetObjectRequest request, GetObjectResult result) {
  6. // Request succeeds
  7. Log.d("Content-Length", "" + getResult.getContentLength());
  8. InputStream inputStream = result.getObjectContent();
  9. byte[] buffer = new byte[2048];
  10. int len;
  11. try {
  12. while ((len = inputStream.read(buffer)) != -1) {
  13. // Process the downloaded data
  14. }
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. @Override
  20. public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  21. // Request exception
  22. if (clientExcepion != null) {
  23. // Local exception, such as a network exception
  24. clientExcepion.printStackTrace();
  25. }
  26. if (serviceException != null) {
  27. // Service exception
  28. Log.e("ErrorCode", serviceException.getErrorCode());
  29. Log.e("RequestId", serviceException.getRequestId());
  30. Log.e("HostId", serviceException.getHostId());
  31. Log.e("RawMessage", serviceException.getRawMessage());
  32. }
  33. }
  34. });
  35. // task.cancel(); // You can cancel the task
  36. // task.waitUntilFinished(); // Wait till the task is finished as needed
Thank you! We've received your feedback.