To implement simple upload, you can call PutObject to upload a single object. You can use simple upload to upload local files and byte arrays in binary.

Prerequisites

Upload a local file

You can upload a local file to Object Storage Service (OSS) in synchronous or asynchronous mode.

  • Upload a local file in synchronous mode

    The following code provides an example on how to upload a local file named examplefile.txt to the exampleobject.txt object in the exampledir directory of the examplebucket bucket in synchronous mode:

    // Construct a request to upload the local file. 
    // Specify the name of the bucket such as examplebucket, the full path of the object such as exampledir/exampleobject.txt, and the full path of the local file such as /storage/emulated/0/oss/examplefile.txt. 
    // The full path of the object cannot contain the bucket name. 
    PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", "/storage/emulated/0/oss/examplefile.txt");
    
    // Optional. Set the object metadata. 
    // ObjectMetadata metadata = new ObjectMetadata();
    // metadata.setContentType("application/octet-stream"); // Set content-type. 
    // metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); // Verify the MD5 hash of the object. 
    // put.setMetadata(metadata);
    
    try {
        PutObjectResult putResult = oss.putObject(put);
    
        Log.d("PutObject", "UploadSuccess");
        Log.d("ETag", putResult.getETag());
        Log.d("RequestId", putResult.getRequestId());
    } catch (ClientException e) {
        // Handle client-side exceptions such as network errors. 
        e.printStackTrace();
    } catch (ServiceException e) {
        // Server-side exceptions. 
        Log.e("RequestId", e.getRequestId());
        Log.e("ErrorCode", e.getErrorCode());
        Log.e("HostId", e.getHostId());
        Log.e("RawMessage", e.getRawMessage());
    }

    For partition storage of Android 10 and later, you can use the URI of an object to upload the object to OSS.

    // Construct a request to upload the object. 
    // Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. 
    // The full path of the object cannot contain the bucket name. 
    PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt",fileUri);
    
    // Optional. Set the object metadata. 
    // ObjectMetadata metadata = new ObjectMetadata();
    // metadata.setContentType("text/plain"); // Set Content-Type. 
    // metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); // Verify the MD5 hash of the object. 
    // put.setMetadata(metadata);
    
    try {
        PutObjectResult putResult = oss.putObject(put);
    
        Log.d("PutObject", "UploadSuccess");
        Log.d("ETag", putResult.getETag());
        Log.d("RequestId", putResult.getRequestId());
    } catch (ClientException e) {
        // Handle client-side exceptions such as network errors. 
        e.printStackTrace();
    } catch (ServiceException e) {
        // Server-side exceptions. 
        Log.e("RequestId", e.getRequestId());
        Log.e("ErrorCode", e.getErrorCode());
        Log.e("HostId", e.getHostId());
        Log.e("RawMessage", e.getRawMessage());
    }
  • Upload a local file in asynchronous mode
    Note When you use OSS SDK for Android, you can use the synchronous mode to upload a local file to OSS only in the subthread rather than in the UI thread. Otherwise, an exception occurs. To upload a local file in the UI thread, use the asynchronous mode to upload the file.

    The following code provides an example on how to upload a local file named examplefile.txt to the exampleobject.txt object in the exampledir directory of the examplebucket destination bucket in asynchronous mode:

    // Construct a request to upload the object. 
    // Specify the name of the bucket such as examplebucket, the full path of the object such as exampledir/exampleobject.txt, and the full path of the local file such as /storage/emulated/0/oss/examplefile.txt. 
    // The full path of the object cannot contain the bucket name. 
    PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", "/storage/emulated/0/oss/examplefile.txt");
    
    // When you upload the local file in asynchronous mode, you can set the progress callback. 
    put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
        @Override
        public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
            Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
        }
    });
    
    OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
        @Override
        public void onSuccess(PutObjectRequest request, PutObjectResult result) {
            Log.d("PutObject", "UploadSuccess");
            Log.d("ETag", result.getETag());
            Log.d("RequestId", result.getRequestId());
        }
    
        @Override
        public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // Handle request exceptions. 
            if (clientExcepion != null) {
                // Handle client-side exceptions such as network errors. 
                clientExcepion.printStackTrace();
            }
            if (serviceException != null) {
                // Server-side exceptions. 
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
    // task.cancel(); // You can cancel the upload task. 
    // task.waitUntilFinished(); // Wait until the task is finished. 

    For partition storage of Android 10 and later, you can use the URI of an object to upload the object to OSS.

    // Construct a request to upload the object. 
    // Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. 
    // The full path of the object cannot contain the bucket name. 
    PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", fileUri);
    
    // When you upload the object in asynchronous mode, you can set the progress callback. 
    put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
        @Override
        public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
            Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
        }
    });
    
    OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
        @Override
        public void onSuccess(PutObjectRequest request, PutObjectResult result) {
            Log.d("PutObject", "UploadSuccess");
            Log.d("ETag", result.getETag());
            Log.d("RequestId", result.getRequestId());
        }
    
        @Override
        public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
            // Handle request exceptions. 
            if (clientExcepion != null) {
                // Handle client-side exceptions such as network errors. 
                clientExcepion.printStackTrace();
            }
            if (serviceException != null) {
                // Server-side exceptions. 
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
    // task.cancel(); // You can cancel the upload task. 
    // task.waitUntilFinished(); // Wait until the task is finished. 

Upload a byte array in binary

The following code provides an example on how to upload a byte array in binary to the exampleobject.txt object in the exampledir directory of the examplebucket bucket in asynchronous mode:

byte[] uploadData = new byte[100 * 1024];
new Random().nextBytes(uploadData);

// Construct a request to upload the object. 
// Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. 
// The full path of the object cannot contain the bucket name. 
PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", uploadData);

try {
    PutObjectResult putResult = oss.putObject(put);

    Log.d("PutObject", "UploadSuccess");
    Log.d("ETag", putResult.getETag());
    Log.d("RequestId", putResult.getRequestId());
} catch (ClientException e) {
    // Handle client-side exceptions such as network errors. 
    e.printStackTrace();
} catch (ServiceException e) {
    // Server-side exceptions. 
    Log.e("RequestId", e.getRequestId());
    Log.e("ErrorCode", e.getErrorCode());
    Log.e("HostId", e.getHostId());
    Log.e("RawMessage", e.getRawMessage());
}