ファイル操作
OSS Android SDK では、ファイルの操作は ‘OSSFile’ で収集されます。これは通常、’OSSData’ と同じ方法で使用されます。実際、重要な違いは、’OSSData’ はメモリ内のデータの取得に使用できるのに対し、’OSSFile’ は端末でローカルファイルを直接アップロードしたり、OSS データをローカルファイルにダウンロードするために使用する点です。
アップロードファイルが大きくて長い時間がかかる場合、アップロードの失敗のために処理中に例外が発生する可能性があります。そのため、OSS Android SDK には大きいファイルのアップロードのためのブレークポイント再開インターフェイスがあります。
1. ファイルへのダウンロード
ファイルダウンロード操作はデータダウンロード操作と似ています。唯一の違いは、ダウンロードの間にダウンロード先のファイルパスを指定する必要があることです。ダウンロードメソッドはデータを返しません。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "sample-data");
ossFile.downloadTo("/path/to/file"); // Failed downloads will throw an exception
非同期バージョン:
ossFile.downloadToInBackground("/path/to/file", new GetFileCallback() {
@Override
public void onSuccess(String objectKey, String filePath) {
}
@Override
public void onProgress(String objectKey, int byteCount, int totalSize) {
}
@Override
public void onFailure(String objectKey, OSSException ossException) {
}
});
非同期インターフェイスの ‘onSuccess’ コールバックメソッドには byte[] 配列パラメーターがありません。これは、ダウンロードが成功した後でファイル保存パスに置き換えられます。
3. ファイル入力ストリームの取得
同様に、オブジェクトをダウンロードするときは、入力ストリームを直接取得して必要な処理を実行することもできます。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "sample-data"); // Constructs an OSSFile instance
try {
InputStream inputStream = ossFile.getObjectInputStream(); // A failed retrieval will throw an exception
// do something with inputStream
} catch (OSSException ossException) {
}
3. ファイルからのアップロード
ファイルからのアップロードインターフェイスも変更されています。アップロードする前に、アップロードファイルのパスを指定する必要があります。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "sample-data");
ossFile.setUploadFilePath("/path/to/file", "content type"); // Specifies the path of the file to be uploaded and the file content type. If the file does not exist, the system will throw an exception
ossFile.enableUploadCheckMd5sum(); // Enables the upload MD5 check
ossFile.upload(); // Failed uploads will throw an exception
非同期バージョン:
ossFile.uploadInBackground(new SaveCallback() {
@Override
public void onSuccess(String objectKey) {
}
@Override
public void onProgress(String objectKey, int byteCount, int totalSize) {
}
@Override
public void onFailure(String objectKey, OSSException ossException) {
}
});
4. その他の一般的な操作
‘OSSFile’ と ‘OSSData’ の違いについては、前の 2 つのセクションで既に説明しました。どちらも OSS に格納されるデータセグメントであるため、OSS の観点からは両者に違いはありません。したがって、データ形式に関するアップロード/ダウンロードインターフェイスの違いを除き、他のインターフェイスの使用方法は同じです。これには、コピー、削除、ダウンロード範囲の指定、カスタムメタ属性の追加、URL の生成、ジョブのキャンセルが含まれます。
したがって、これらについてはここでは説明しません。
5. ブレークポイントダウンロード
大きいファイルのダウンロードには時間がかかるため、処理が失敗するリスクが高くなります。そのため、OSS Android SDK にはブレークポイントダウンロードインターフェイスがあります。このインターフェイスを使用してファイルをダウンロードすると、なんらかの理由でダウンロードが途中で失敗した場合でも、ファイル保存パスを変更することなく、次のダウンロードで同じ ‘OSSbucket’ と ‘ObjectKey’ を指定できます。その場合、次のダウンロードは、前のダウンロードが失敗した場所から続行されます。
同時に、このインターフェイスを呼び出すと、タスクの ‘handler’ が返されます。ダウンロード処理の間にこの ‘handler’ を使用して、いつでもダウンロードタスクをキャンセルできます。失敗したタスクは、’onFailure()’ ロジックに移行します。
このインターフェイスは非同期バージョンのみです。次にサンプルコードを示します。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "test.jpg");
TaskHandler tk = ossFile.ResumableDownloadToInBackground("/path/to/file", new GetFileCallback() {
@Override
public void onSuccess(String objectKey, String filePath) {
// TODO Auto-generated method stub
}
@Override
public void onProgress(String objectKey, int byteCount, int totalSize) {
// TODO Auto-generated method stub
}
@Override
public void onFailure(String objectKey, OSSException ossException) {
// TODO Auto-generated method stub
}
});
tk.cancel(); // During the download task execution, you can discard the download at any time
ブレークポイントダウンロードについては、記録されるファイルのストレージパス、同時スレッドの最大数、自動再試行回数など、いくつかの設定を指定できます。これらのオプションを設定しないと、デフォルト値が使用されます。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "test.jpg");
ResumableTaskOption option = new ResumableTaskOption();
option.setThreadNum(2); // Default: 3, maximum: 5
option.setRecordFileDirectory("record/file/dir"); // Stored in the application data directory by default
option.setAutoRetryTime(1); // Default: 2, maximum: 3
TaskHandler tk = ossFile.ResumableDownloadToInBackground("/path/to/file", option, new GetFileCallback() {
@Override
public void onSuccess(String objectKey, String filePath) {
// TODO Auto-generated method stub
}
@Override
public void onProgress(String objectKey, int byteCount, int totalSize) {
// TODO Auto-generated method stub
}
@Override
public void onFailure(String objectKey, OSSException ossException) {
// TODO Auto-generated method stub
}
});
6. ブレークポイントからの再開
同様に、OSS Android SDK にはブレークポイント再開インターフェイスもあります。このインターフェイスを使用してファイルをアップロードすると、なんらかの理由でアップロードが途中で失敗した場合でも、アップロードするファイルを変更することなく、次のアップロードで同じ ‘OSSbucket’ と ‘ObjectKey’ を指定できます。その場合、次のアップロードは、前のアップロードが失敗した場所から続行されます。
同時に、このインターフェイスを呼び出すと、タスクの ‘handler’ が返されます。アップロード処理の間にこの ‘handler’ を使用して、いつでもアップロードタスクをキャンセルできます。失敗したタスクは、’onFailure()’ ロジックに移行します。
このインターフェイスは非同期バージョンのみです。次にサンプルコードを示します。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "large.data");
ossFile.setUploadFilePath("/path/to/file", "file content type"); // Specifies the path of the file to be uploaded and the file content type
TaskHandler tk = ossFile.ResumableUploadInBackground(new SaveCallback() {
@Override
public void onSuccess(String objectKey) {
// TODO Auto-generated method stub
}
@Override
public void onProgress(String objectKey, int byteCount, int totalSize) {
// TODO Auto-generated method stub
}
@Override
public void onFailure(String objectKey, OSSException ossException) {
// TODO Auto-generated method stub
}
});
tk.cancel(); // During the upload task execution, you can discard the upload at any time
ブレークポイント再開タスクについても、いくつかの設定を指定できます。これらの設定は、前の章のブレークポイントダウンロードと同じです。
OSSFile ossFile = ossService.getOssFile(sampleBucket, "test.jpg");
ResumableTaskOption option = new ResumableTaskOption();
option.setThreadNum(2); // Default: 3, maximum: 5
option.setRecordFileDirectory("record/file/dir"); // Stored in the application data directory by default
option.setAutoRetryTime(1); // Default: 2, maximum: 3
TaskHandler tk = ossFile.ResumableUploadInBackground(option, new SaveCallback() {
@Override
public void onSuccess(String objectKey) {
// TODO Auto-generated method stub
}
@Override
public void onProgress(String objectKey, int byteCount, int totalSize) {
// TODO Auto-generated method stub
}
@Override
public void onFailure(String objectKey, OSSException ossException) {
// TODO Auto-generated method stub
}
});