Para garantizar una alta calidad y ser coherente con nuestras otras bibliotecas de Cloud, el documentación del Servicio de transferencia de almacenamiento ahora usa el cliente de Cloud Bibliotecas en lugar de las bibliotecas cliente de la API de Google. Consulta Explicación de las bibliotecas cliente para obtener más información información sobre las dos opciones.
La biblioteca cliente de la API de Google sigue recibiendo actualizaciones, pero ya no se hace referencia a ella en la documentación.
En esta guía, se abarcan las principales diferencias en cuanto al uso de el Servicio de transferencia de almacenamiento, y brinda instrucciones para actualizar a tus clientes cuando migrar a la biblioteca cliente de Cloud.
Java
Actualiza las dependencias
Para cambiar a la nueva biblioteca, reemplaza tu dependencia en
google-api-services-storagetransfer
con google-cloud-storage-transfer
.
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-storage-transfer</artifactId> <version>0.2.3</version> </dependency>
Si usas Gradle sin BoM, agrega lo siguiente a tus dependencias:
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>
En general, el código se puede convertir fácilmente desde el cliente de API. en la biblioteca cliente de Cloud. A continuación, se muestran algunas diferencias clave entre los dos clientes de Java
Creación de instancias del cliente
La biblioteca cliente de Cloud reduce gran parte del código estándar asociado con la creación de instancias de un cliente manejándola en segundo plano.
Biblioteca cliente de la 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();
Biblioteca cliente de Cloud
StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create();
Creadores para clases de modelos
Las clases de modelos en la biblioteca cliente de Cloud usan compiladores en lugar de constructores.
Biblioteca cliente de la API
TransferJob transferJob =
new TransferJob()
.setStatus("ENABLED");
Biblioteca cliente de Cloud
TransferJob transferJob =
TransferJob.newBuilder()
.setStatus(Status.ENABLED)
.build();
Las operaciones de lista muestran iterables.
Las operaciones de lista en la biblioteca cliente de Cloud muestran iterables simples en lugar de los resultados paginados en la biblioteca cliente de la API.
Biblioteca cliente de la 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();
}
}
Biblioteca cliente de Cloud
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);
}
}
}
Comparaciones de ejemplo
Aquí, incluimos las muestras de la biblioteca cliente de la API anterior en comparación con sus muestras equivalentes con la biblioteca cliente de Cloud. Si usaste estas muestras antes, puedes usar esta comparación para comprender cómo mover tu código a la nueva biblioteca cliente de Cloud.
Transferir desde Amazon S3
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Transferencia a Nearline
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Cómo verificar la operación de transferencia más reciente
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Python
Actualiza las dependencias
Para usar la biblioteca nueva, agrega una dependencia en google-cloud-storage-transfer
.
Se usará en lugar del cliente de descubrimiento de
google-api-python-client
pip install --upgrade google-cloud-storage-transfer
Creación de instancias del cliente
Usa el módulo storage_transfer
en lugar de googleapiclient.discovery
.
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Comparaciones de ejemplo
Para ilustrar las diferencias entre las dos bibliotecas, aquí están las muestras de cliente de API antiguas en paralelo con sus equivalentes en la biblioteca cliente de Cloud.
Transferencia desde Amazon S3
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Transferencia a Nearline
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Observa la importación de google.protobuf.duration_pb2.Duration
.
Cómo verificar la operación de transferencia más reciente
Biblioteca cliente de la API
Biblioteca cliente de Cloud
Ten en cuenta el uso de storage_transfer.TransferOperation.deserialize