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

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");
    
    // Configure object metadata. 
    // ObjectMetadata metadata = new ObjectMetadata();
    // Specify Content-Type. 
    // metadata.setContentType("application/octet-stream"); 
    // Specify the MD5 hash that is used for MD5 verification. 
    // metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); 
    // Specify the access control list (ACL) of the object. The default value is private. 
    // metadata.setHeader("x-oss-object-acl", "private");
    // Specify the storage class of the object. By default, the storage class of the object is the same as the storage class of the bucket in which the object is stored. In this example, this parameter is set to Standard. 
    // metadata.setHeader("x-oss-storage-class", "Standard");
    // 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) {
        // Handle server-side exceptions. 
        Log.e("RequestId", e.getRequestId());
        Log.e("ErrorCode", e.getErrorCode());
        Log.e("HostId", e.getHostId());
        Log.e("RawMessage", e.getRawMessage());
    }

    For scoped storage of Android 10 and later versions, you can use the URI of a file to upload the file to OSS.

    // Construct a request to upload the local file. 
    // 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);
    
    // Configure object metadata. 
    // ObjectMetadata metadata = new ObjectMetadata();
    // Specify Content-Type. 
    // metadata.setContentType("text/plain"); 
    // Specify the MD5 hash that is used for MD5 verification. 
    // metadata.setContentMD5(BinaryUtil.calculateBase64Md5(uploadFilePath)); 
    // 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) {
        // Handle 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 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 bucket in asynchronous 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");
    
    // When you upload the local file in asynchronous mode, you can configure 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) {
                // Handle server-side exceptions. 
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
    // Cancel the upload task. 
    // task.cancel(); 
    // Wait until the upload task is completed. 
    // task.waitUntilFinished(); 

    For scoped storage of Android 10 and later versions, you can use the URI of a file to upload the file to OSS.

    // Construct a request to upload the local file. 
    // 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 local file in asynchronous mode, you can configure 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) {
                // Handle server-side exceptions. 
                Log.e("ErrorCode", serviceException.getErrorCode());
                Log.e("RequestId", serviceException.getRequestId());
                Log.e("HostId", serviceException.getHostId());
                Log.e("RawMessage", serviceException.getRawMessage());
            }
        }
    });
    // Cancel the upload task. 
    // task.cancel(); 
    // Wait until the upload task is completed. 
    // task.waitUntilFinished(); 

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 byte array in binary. 
// 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) {
    // Handle server-side exceptions. 
    Log.e("RequestId", e.getRequestId());
    Log.e("ErrorCode", e.getErrorCode());
    Log.e("HostId", e.getHostId());
    Log.e("RawMessage", e.getRawMessage());
}

References

  • For more information about the complete sample code of simple upload, visit GitHub.
  • For more information about the API operation that you can call to perform simple upload, see PutObject.