簡易アップロードには、ストリーミングアップロードとファイルアップロードが含まれます。 ストリーミングアップロードは、ソースオブジェクトとして InputStream を使用します。 ファイルアップロードは、ソースファイルとしてローカルファイルを使用します。

簡易アップロードのための完全なコードについては、『GitHub』をご参照ください。

ストリーミングアップロード

データストリームを OSS へアップロードするには、ossClient.putObject を使用します。

  • 文字列のアップロード

    次のコードを実行し、文字列をアップロードします。

    // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    // Create an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    // Upload a string.
    String content = "Hello OSS";
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content.getBytes()));
    
    // Close your OSSClient instance.
    ossClient.shutdown();
    
  • バイト配列のアップロード

    次のコードを実行し、バイト配列をアップロードします。

    // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    // Create an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);
    
    // Upload a byte array.
    byte[] content = "Hello OSS".getBytes();
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", new ByteArrayInputStream(content));
    
    // Close your OSSClient instance.
    ossClient.shutdown();
    
  • ネットワークストリームのアップロード

    次のコードを実行し、ネットワークストリームをアップロードします。

    // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    // Create an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    // Upload a network stream.
    InputStream inputStream = new URL("https://www.aliyun.com/").openStream();
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
    
    // Close your OSSClient instance.
    ossClient.shutdown();
    
  • ファイルストリームのアップロード

    次のコードを実行し、ファイルストリームをアップロードします。

    // This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
    String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. To ensure cloud security, we recommend that you follow best practices of Resource Access Management and log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
    String accessKeyId = "<yourAccessKeyId>";
    String accessKeySecret = "<yourAccessKeySecret>";
    
    // Create an OSSClient instance.
    OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
    
    // Upload a file stream.
    InputStream inputStream = new FileInputStream("<yourlocalFile>");
    ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
    
    // Close your OSSClient instance.
    ossClient.shutdown();
    

ファイルのアップロード

次のコードを実行し、ローカルファイルをアップロードします。

// This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

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

// Upload a file. <yourLocalFile> consists of a local file path and a file name that includes an extension, for example, /users/local/myfile.txt.
ossClient.putObject("<yourBucketName>", "<yourObjectName>", new File("<yourLocalFile>"));

// Close your OSSClient instance.
ossClient.shutdown();