To ensure high quality and to be consistent with our other Cloud libraries, the documentation for Storage Transfer Service is now using the Cloud Client Librariesinstead of the Google API Client Libraries. See Client libraries explained for more information about the two options.
The Google API Client Library continues to receive updates, but is no longer referenced in the documentation.
This guide covers the main differences as they apply to using Storage Transfer Service, and provides instructions on updating your clients when migrating to the Cloud Client Library.
Java
Updating dependencies
To switch to the new library, replace your dependency on google-api-services-storagetransfer
with google-cloud-storage-transfer
.
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-storage-transfer</artifactId> <version>0.2.3</version> </dependency>
If you are using Gradle without BOM, add this to your dependencies:
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
>
For the most part, code can be pretty easily converted from the API Client Library to the Cloud Client Library. The following are some key differences between the two Java clients
Client instantiation
The Cloud Client Library reduces a lot of the boilerplate associated with client instantiation by handling it behind the scenes.
API Client Library
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 Client Library
StorageTransferServiceClient
storageTransfer
=
StorageTransferServiceClient
.
create
();
Builders for model classes
Model classes in the Cloud Client Library use builders instead of constructors.
API Client Library
TransferJob
transferJob
=
new
TransferJob
()
.
setStatus
(
"ENABLED"
);
Cloud Client Library
TransferJob
transferJob
=
TransferJob
.
newBuilder
()
.
setStatus
(
Status
.
ENABLED
)
.
build
();
List operations return iterables
List operations in the Cloud Client Library return simple iterables instead of the paginated results in the API Client Library.
API Client Library
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 Client Library
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
);
}
}
}
Sample comparisons
Here, we include the old API Client Library samples, compared to their equivalent samples using the Cloud Client Library. If you used these samples before, you can use this comparison to understand how to move your code to the new Cloud Client Library.
Transfer from Amazon S3
API Client Library
Cloud Client Library
Transfer to nearline
API Client Library
Cloud Client Library
Check latest transfer operation
API Client Library
Cloud Client Library
Python
Updating dependencies
To use the new library, add a dependency on google-cloud-storage-transfer
.
This will be used instead of the discovery client from google-api-python-client
.
pip install --upgrade google-cloud-storage-transfer
Client instantiation
Use the storage_transfer
module instead of googleapiclient.discovery
.
API Client Library
Cloud Client Library
Sample comparisons
To illustrate the differences between the two libraries, here are the old API Client Samples side-by-side with their equivalents in the Cloud Client Library.
Transfer from Amazon S3
API Client Library
Cloud Client Library
Transfer to nearline
API Client Library
Cloud Client Library
Note the import of google.protobuf.duration_pb2.Duration
.
Check latest transfer operation
API Client Library
Cloud Client Library
Note the use of storage_transfer.TransferOperation.deserialize