Sample code for 30+ languages & platforms
Java

OSS Upload String (Alibaba Cloud)

See more Alibaba Cloud OSS Examples

Demonstrates how to upload the contents of a string to an AliCloud OSS bucket.

The Chilkat S3 functions in the HTTP class are compatible with Alibaba Cloud's OSS service.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Insert your AccessKey ID here:
        http.setAwsAccessKey("access-key");

        // Insert your AccessKey Secret here:
        http.setAwsSecretKey("secret-key");

        // To upload to a bucket located in a different region, use the endpoint for that region, such as "oss-cn-hangzhou.aliyuncs.com     "
        // See Alibaba Object Storage Service Regions and Endpoints
        http.setAwsEndpoint("oss-us-east-1.aliyuncs.com");

        String bucketName = "chilkat";
        String objectName = "orchard.json";
        String contentType = "application/json";

        http.setKeepResponseBody(true);

        // Let's say we have JSON in a string and want to upload it to a file in Aliyun..
        String jsonStr = "{ \"orchard\": \"apple\" }";

        // The charset indicates the byte representation of what is uploaded.
        // If needed, Chilkat will internally convert to the desired byte representation before uploading.
        String charset = "utf-8";

        try {
            http.s3UploadString(jsonStr, charset, contentType, bucketName, objectName);
            System.out.println("String uploaded.");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());

            Xml xml = new Xml();
            xml.loadXml(http.getLastResponseBody());
            System.out.println(xml.getXml());
        }
    }
}