Simple upload is an upload method that you can use to upload an object by calling the PutObject operation. You can use simple upload to upload local files and 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 Policy.
API | Action | Definition |
PutObject |
| Uploads an object. |
| When uploading an object, if you specify object tags through x-oss-tagging, this permission is required. | |
| When uploading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. | |
|
Upload a local file
You can upload a local file to Object Storage Service (OSS) in synchronous or asynchronous mode.
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 synchronous mode:
byte[] uploadData = new byte[100 * 1024];
new Random().nextBytes(uploadData);
// Construct an upload request.
// Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt.
// Do not include the bucket name in the full path of the object.
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 the complete sample code that is used to perform simple upload, visit GitHub.
For more information about the API operation that you can call to perform simple upload, see PutObject.
For more information about how to initialize an OSSClient instance, see Initialization.