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 byte arrays in binary.
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 on resources in the account. In contrast, RAM users and RAM roles associated with an Alibaba Cloud account initially have no permissions. To manage resources by using a RAM user or role, you must grant the required permissions via RAM policies or Bucket policies.
API | Action | Description |
PutObject |
| Grants the permission to upload objects. |
| Specifies the tags of the object by using the x-oss-tagging header when you upload objects. | |
| When uploading an object, if the object's metadata includes X-Oss-Server-Side-Encryption: KMS, permissions for both operations 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.