為確保高品質並與其他 Cloud 程式庫保持一致,Storage 移轉服務的文件現在使用 Cloud 用戶端程式庫,而非 Google API 用戶端程式庫。如要進一步瞭解這兩個選項,請參閱「用戶端程式庫說明」。
Google API 用戶端程式庫會持續更新,但說明文件不再提及。
本指南說明使用 Storage 移轉服務時的主要差異,並提供操作說明,協助您在遷移至 Cloud 用戶端程式庫時更新用戶端。
Java
更新依附元件
如要改用新程式庫,請將 google-api-services-storagetransfer 依附元件替換為 google-cloud-storage-transfer。
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage-transfer</artifactId>
<version>1.80.0</version>
</dependency>如果您使用無 BoM 的 Gradle,請在依附元件中加入以下指令:
implementation 'com.google.cloud:google-cloud-storage-transfer:1.80.0'
如果您搭配使用 Maven 與 BOM,請將下列指令新增至 pom.xml 檔案:
<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>
</dependencies>
在大多數情況下,程式碼可以輕鬆從 API 用戶端程式庫轉換為 Cloud 用戶端程式庫。以下是這兩個 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, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
return new Storagetransfer.Builder(httpTransport, jsonFactory, credential)
.build();
}
}
Cloud 用戶端程式庫
public class StoragetransferExample {
public static void main(String args[]) throws Exception {
StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create();
ListTransferJobsRequest request =
ListTransferJobsRequest.newBuilder().setFilter("{\"projectId\": \"my-project-id\"}").build();
for (TransferJob job : storageTransfer.listTransferJobs(request).iterateAll()) {
System.out.println(job);
}
}
}
比較範例
以下列出舊版 API 用戶端程式庫範例,以及使用 Cloud 用戶端程式庫的對等範例。如果您先前使用過這些範例,可以透過這項比較,瞭解如何將程式碼移至新的 Cloud 用戶端程式庫。
從 Amazon S3 移轉
API 用戶端程式庫
Cloud 用戶端程式庫
轉移至 Nearline
API 用戶端程式庫
Cloud 用戶端程式庫
查看最新轉移作業
API 用戶端程式庫
Cloud 用戶端程式庫
Python
更新依附元件
如要使用新程式庫,請新增 google-cloud-storage-transfer 依附元件。系統會使用這個用戶端,而非 google-api-python-client 的探索用戶端。
pip install --upgrade google-cloud-storage-transfer
用戶端例項化
請使用 storage_transfer 模組,而非 googleapiclient.discovery。
API 用戶端程式庫
Cloud 用戶端程式庫
比較範例
為說明這兩個程式庫之間的差異,以下並列呈現舊版 API 用戶端範例和 Cloud 用戶端程式庫中的對應範例。
從 Amazon S3 移轉
API 用戶端程式庫
Cloud 用戶端程式庫
轉移至 Nearline
API 用戶端程式庫
Cloud 用戶端程式庫
請留意 google.protobuf.duration_pb2.Duration 的匯入作業。
查看最新轉移作業
API 用戶端程式庫
Cloud 用戶端程式庫
請注意,使用 storage_transfer.TransferOperation.deserialize