Storage Transfer Service クラウド クライアント ライブラリへの移行

品質を維持し、他の 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 クライアント ライブラリは、バックグラウンドで処理することで、クライアントのインスタンス化に関連するボイラープレートを大幅に削減します。

API クライアント ライブラリ

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();

Cloud クライアント ライブラリ

StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create();

モデルクラスのビルダー

Cloud クライアント ライブラリのモデルクラスは、コンストラクタではなくビルダーを使用します。

API クライアント ライブラリ

TransferJob transferJob =
   new TransferJob()
       .setStatus("ENABLED");

Cloud クライアント ライブラリ

TransferJob transferJob =
   TransferJob.newBuilder()
       .setStatus(Status.ENABLED)
   .build();

イテラブルを返すリスト オペレーション

Cloud クライアント ライブラリのリスト オペレーションは単純なイテラブルを返します。API クライアント ライブラリのようにページ分けされた結果は返しません。

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,