Search code examples
javagoogle-api-java-clientgoogle-cloud-storagegoogle-api-client

Can Insert objects created from the same Storage object be executed concurrently?


I'm using Google's Java API to work with Google Cloud Storage (GCS).

I have multiple worker threads that insert objects into GCS. Currently they share a common Storage instance, and use this to create Storage.Object.Insert instances:

synchronized(LOCK)
{
    insertObject = mStorage.objects().insert(mBucketName, objectMetadata, mediaContent);
}

They later call execute() on the Insert instance, uploading a file to GCS.

insertObject.execute();

My question is can I run the execute() call concurrently in different threads? The Insert object is unique to that thread, but the Storage object it was created with is shared between threads, so I'm worried this may cause problems.

Thanks in advance!


Solution

  • Yes! :)

    That is, assuming the HttpTransport instance you are using is thread-safe, then having a shared Storage instance should be thread safe. What is not safe is to share the request class itself or its response across threads without additional locking that you provide. But as long as each thread is using its own request class, it is safe and in fact recommended to share the Storage and instance across threads.

    Note: I am an owner of the google-api-java-client project