Simple upload uses the PutObject method to upload a single file, which is called an object. You can use simple upload to upload local files or binary byte[] arrays.
Usage notes
Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.
API | Action | Definition |
PutObject |
| Uploads an object. |
| When uploading an object, if you specify object tags through | |
| When uploading an object, if the object metadata contains | |
|
Upload a local file
You can upload local files to OSS synchronously or asynchronously.
Upload a binary byte[] array
The following code shows how to synchronously upload a binary byte[] array as an object named exampleobject.txt to the exampledir/ folder in the examplebucket bucket.
byte[] uploadData = new byte[100 * 1024];
new Random().nextBytes(uploadData);
// Create an upload request.
// Specify the bucket name (for example, examplebucket) and the full path of the object (for example, 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) {
// Client exception, such as a network exception.
e.printStackTrace();
} catch (ServiceException e) {
// Server exception.
Log.e("RequestId", e.getRequestId());
Log.e("ErrorCode", e.getErrorCode());
Log.e("HostId", e.getHostId());
Log.e("RawMessage", e.getRawMessage());
}References
For the complete sample code for a simple upload, see the GitHub example.
For more information about the API operation for a simple upload, see PutObject.
For more information about how to initialize an OSSClient instance, see Initialize an OSSClient instance for Android.