品質を維持し、他の Google Cloud ライブラリとの整合性を保つため、Storage Transfer Service のドキュメントでは、Google API クライアント ライブラリの代わりに Cloud クライアント ライブラリを使用しています。この 2 つのオプションの詳細については、クライアント ライブラリの説明をご覧ください。
Google API クライアント ライブラリは引き続き更新されますが、ドキュメントで参照されることはありません。
このガイドでは、Storage Transfer Service を使用する場合の主な違いと、Cloud クライアント ライブラリに移行する際のクライアントの更新手順について説明します。
Java
依存関係の更新
新しいライブラリに切り替えるには、google-api-services-storagetransfer
の依存関係を google-cloud-storage-transfer
に置き換えます。
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-storage-transfer</artifactId> <version>0.2.3</version> </dependency>
Gradle を BOM なしで使用している場合は、次のものを依存関係に追加します。
implementation 'com.google.cloud:google-cloud-storage-transfer:0.2.3'
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>24.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage-transfer</artifactId>
</dependency>
ほとんどの場合、コードは API クライアント ライブラリから Cloud クライアント ライブラリに簡単に変換できます。2 つの Java クライアントの主な違いは次のとおりです。
クライアントのインスタンス化
Cloud クライアント ライブラリは、バックグラウンドで処理することで、クライアントのインスタンス化に関連するボイラープレートを大幅に削減します。
GoogleCredentials credential = GoogleCredentials.getApplicationDefault();
if (credential.createScopedRequired()) {
credential = credential.createScoped(StoragetransferScopes.all());
}
Storagetransfer storageTransfer = new Storagetransfer.Builder(Utils.getDefaultTransport(),
Utils.getDefaultJsonFactory(), new HttpCredentialsAdapter(credential))
.build();
StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create();
モデルクラスのビルダー
Cloud クライアント ライブラリのモデルクラスは、コンストラクタではなくビルダーを使用します。
TransferJob transferJob =
new TransferJob()
.setStatus("ENABLED");
TransferJob transferJob =
TransferJob.newBuilder()
.setStatus(Status.ENABLED)
.build();
イテラブルを返すリスト オペレーション
Cloud クライアント ライブラリのリスト オペレーションは単純なイテラブルを返します。API クライアント ライブラリのようにページ分けされた結果は返しません。
public class StoragetransferExample {
public static void main(String args[]) throws IOException, GeneralSecurityException {
Storagetransfer storagetransferService = createStoragetransferService();
Storagetransfer.TransferJobs.List request = storagetransferService.transferJobs().list();
ListTransferJobsResponse response;
do {
response = request.execute();
if (response.getTransferJobs() == null) {
continue;
}
for (TransferJob transferJob : response.getTransferJobs()) {
System.out.println(transferJob);
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
}
public static Storagetransfer createStoragetransferService()
throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
}
return new Storagetransfer.Builder(httpTransport, jsonFactory, credential)
.build();
}
}
public class StoragetransferExample {
public static void main(String args[]) throws Exception {
StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create();
ListTransferJobsRequest request = ListTransferJobsRequest.newBuilder().build();
for (TransferJob job : client.listTransferJobs(request).iterateAll()) {
System.out.println(job);
}
}
}
サンプル比較
ここでは、以前の API クライアント ライブラリのサンプルと、Cloud クライアント ライブラリを使用した同等のサンプルを比較します。これらのサンプルを使用したことがある場合は、この比較を参照すれば、新しい Cloud クライアント ライブラリへのコードの移行方法を理解できます。
Amazon S3 から転送する
Nearline に転送する
最新の転送オペレーションを確認する
Python
依存関係の更新
新しいライブラリを使用するには、google-cloud-storage-transfer
の依存関係を追加します。これは、google-api-python-client
の検出クライアントの代わりに使用されます。
pip install --upgrade google-cloud-storage-transfer
クライアントのインスタンス化
googleapiclient.discovery
ではなく storage_transfer
モジュールを使用します。
サンプル比較
以下では、古い API クライアントのサンプルと Cloud クライアント ライブラリを使用した同等のサンプルを比較して、この 2 つのライブラリの違いを説明します。
Amazon S3 から転送する
Nearline に転送する
google.protobuf.duration_pb2.Duration
をインポートしている点に注意してください。
最新の転送オペレーションを確認する
storage_transfer.TransferOperation.deserialize
を使用している点に注意してください。