To implement simple upload, you can call PutObject to upload a single object. Simple upload includes streaming upload and object upload. Streaming upload uses InputStream as the data source of an object uploaded to Object Storage Service (OSS). Object upload uses a local file as the data source of an object uploaded to OSS. This topic describes how to use streaming upload and object upload to upload an object.

Note

Streaming upload

Streaming upload allows you to upload a data stream to an OSS object.

  • Upload a string

    The following code provides an example on how to upload a string to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Specify a string. 
    String content = "Hello OSS";
    
    // Create a PutObjectRequest object. 
    // 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 bucket names. 
    PutObjectRequest putObjectRequest = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", new ByteArrayInputStream(content.getBytes()));
    
    // Optional. Specify the storage class and the access control list (ACL) of the object. 
    // ObjectMetadata metadata = new ObjectMetadata();
    // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
    // metadata.setObjectAcl(CannedAccessControlList.Private);
    // putObjectRequest.setMetadata(metadata);
    
    // Upload the string. 
    ossClient.putObject(putObjectRequest);
    
    // Shut down the OSSClient instance. 
    ossClient.shutdown();                   
  • Upload a byte array

    The following code provides an example on how to upload a byte array to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, go to https://ram.console.aliyun.com. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
    
    // Specify a byte array. 
    byte[] content = "Hello OSS".getBytes();
    // 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 bucket names. 
    ossClient.putObject("examplebucket", "exampledir/exampleobject.txt", new ByteArrayInputStream(content));
    
    // Shut down the OSSClient instance. 
    ossClient.shutdown();
  • Upload a network stream

    The following code provides an example on how to upload a network stream to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Specify the address of the network stream. 
    InputStream inputStream = new URL("https://www.aliyun.com/").openStream();
    // 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 bucket names. 
    ossClient.putObject("examplebucket", "exampledir/exampleobject.txt", inputStream);
    
    // Shut down the OSSClient instance. 
    ossClient.shutdown();
  • Upload a file stream

    The following code provides an example on how to upload a file stream to the exampleobject.txt object in the exampledir directory of the examplebucket destination bucket:

    // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    String endpoint = "yourEndpoint";
    // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    String accessKeyId = "yourAccessKeyId";
    String accessKeySecret = "yourAccessKeySecret";
    
    // Create an OSSClient instance. 
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    
    // Specify the full path of the local file to upload. If the path of the local file is not specified, the file stream is uploaded from the path of the project to which the sample program belongs. 
    InputStream inputStream = new FileInputStream("D:\\localpath\\examplefile.txt");
    // 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 bucket names. 
    ossClient.putObject("examplebucket", "exampledir/exampleobject.txt", inputStream);
    
    // Shut down the OSSClient instance. 
    ossClient.shutdown();

Upload an object

Object upload allows you to upload a local file to an OSS object.

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 destination bucket:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String endpoint = "yourEndpoint";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Create a PutObjectRequest object. 
// Specify the bucket name such as examplebucket, the full path of the object such as exampledir/exampleobject.txt, and the full path of the uploaded local file. The full path of the object cannot contain bucket names. 
// By default, if the path of the local file is not specified, the local file is uploaded from the path of the project to which the sample program belongs. 
PutObjectRequest putObjectRequest = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", new File("D:\\localpath\\examplefile.txt"));

// Optional. Specify the storage class and the ACL of the object. 
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);

// Upload the object. 
ossClient.putObject(putObjectRequest);

// Shut down the OSSClient instance. 
ossClient.shutdown();