すべてのプロダクト
Search
ドキュメントセンター

:OSSClient

最終更新日:Dec 22, 2023

OSSClient

OSSClient は OSS サービスの Java クライアントです。OSS サービスの操作のために呼び出される一連の関数を提供します。

OSSClient の作成

以下のコードに示すように、OSSClient は非常に簡単に作成できます。

String key = "<key>";
String secret = "<secret>";
// Uses Hangzhou as an example
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);

ユーザーが 必ずAccessKey をインポートし、バケットのエンドポイントにアクセスすることが重要です。ユーザーが HTTPS プロトコルを使用する場合、エンドポイントは https:// で始まる必要があります。

OSSClient の設定

OSSClient の詳細なパラメーターを設定するには、OSSClient の作成時に ClientConfiguration オブジェクトをインポートできます。ClientConfiguration は、OSS サービス設定用の型です。これを使用すると、クライアントのプロキシ、最大接続数、およびその他のパラメーターを設定できます。

プロキシの使用

以下のコードでは、クライアントはプロキシを使用して OSS サービスにアクセスできます。

// Creates a ClientConfiguration instance
ClientConfiguration conf = new ClientConfiguration();

// Configures the proxy for the local port 8080
conf.setProxyHost("127.0.0.1");
conf.setProxyPort(8080);

// Creates the OSSClient
client = new OSSClient(endpoint, accessKeySecret, accessKeySecret, conf);

上記のコードにより、127.0.0.1 のポート 8080 を経由するプロキシによってクライアントですべての操作が実行可能になります。ユーザーを認証するプロキシの場合は、ユーザー名とパスワードを設定できます。

// Creates a ClientConfiguration instance
ClientConfiguration conf = new ClientConfiguration();

// Configures the proxy for the local port 8080
conf.setProxyHost("127.0.0.1");
conf.setProxyPort(8080);

//Sets the username and password
conf.setProxyUsername("username");
conf.setProxyPassword("password");

ネットワークパラメーターの設定

ClientConfiguration を使用して一部のネットワークパラメーターを設定できます。

ClientConfiguration conf = new ClientConfiguration();

// Sets the maximum number of HTTP connections to 10
conf.setMaxConnections(10);

// Sets TCP connection timeout to 5000 milliseconds
conf.setConnectionTimeout(5000);

// Sets the maximum number of retries to 3
conf.setMaxErrorRetry(3);

// Sets the Socket data transmission time out to 2000 milliseconds
conf.setSocketTimeout(2000);

ClientConfiguration を使用して、次のパラメーターを設定できます。

パラメーター 説明
UserAgent ユーザープロキシ、HTTP User-Agent ヘッダーを指定。デフォルト値は “aliyun-sdk-java”
ProxyHost プロキシのホストアドレス
ProxyPort プロキシのポート
ProxyUsername プロキシによって検証されるユーザー名
ProxyPassword プロキシによって検証されるパスワード
ProxyDomain NTLM 検証されるプロキシにアクセスする Windows ドメイン名
ProxyWorkstation NTLM プロキシの Windows ワークステーションの名前
MaxConnections 有効にすることができる HTTP 最大接続数。デフォルト値は 1024
SocketTimeout データ伝送のための接続を有効にするタイムアウト (単位: ミリ秒)。デフォルト値は 50000 ミリ秒
ConnectionTimeout 接続確立タイムアウト値 (単位: ミリ秒)。デフォルト値は 50000 ミリ秒
MaxErrorRetry 失敗したリクエストを再試行する最大回数。デフォルト値は 3