Storage Control Create Anywhere Cache
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
C++
For more information, see the Cloud Storage C++ API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
namespace
storagecontrol
=
google
::
cloud
::
storagecontrol_v2
;
[](
storagecontrol
::
StorageControlClient
client
,
std
::
string
const
&
bucket_name
,
std
::
string
const
&
cache_name
,
std
::
string
const
&
zone_name
)
{
google
::
storage
::
control
::
v2
::
AnywhereCache
cache
;
cache
.
set_name
(
cache_name
);
cache
.
set_zone
(
zone_name
);
google
::
storage
::
control
::
v2
::
CreateAnywhereCacheRequest
request
;
request
.
set_parent
(
std
::
string
{
"projects/_/buckets/"
}
+
bucket_name
);
*
request
.
mutable_anywhere_cache
()
=
cache
;
// Start a create operation and block until it completes. Real applications
// may want to setup a callback, wait on a coroutine, or poll until it
// completes.
auto
anywhere_cache
=
client
.
CreateAnywhereCache
(
request
).
get
();
if
(
!
anywhere_cache
)
throw
std
::
move
(
anywhere_cache
).
status
();
std
::
cout
<<
"Created anywhere cache: "
<<
anywhere_cache
-
> name
()
<<
"
\n
"
;
}
Java
For more information, see the Cloud Storage Java API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
import
com.google.api.gax.longrunning. OperationFuture
;
import
com.google.storage.control.v2. AnywhereCache
;
import
com.google.storage.control.v2. BucketName
;
import
com.google.storage.control.v2. CreateAnywhereCacheMetadata
;
import
com.google.storage.control.v2. CreateAnywhereCacheRequest
;
import
com.google.storage.control.v2. StorageControlClient
;
import
java.io.IOException
;
import
java.util.concurrent.ExecutionException
;
public
final
class
AnywhereCacheCreate
{
public
static
void
anywhereCacheCreate
(
String
bucketName
,
String
cacheName
,
String
zoneName
)
throws
InterruptedException
,
ExecutionException
,
IOException
{
try
(
StorageControlClient
storageControl
=
StorageControlClient
.
create
())
{
CreateAnywhereCacheRequest
request
=
CreateAnywhereCacheRequest
.
newBuilder
()
// Set project to "_" to signify globally scoped bucket
.
setParent
(
BucketName
.
format
(
"_"
,
bucketName
))
.
setAnywhereCache
(
AnywhereCache
.
newBuilder
().
setName
(
cacheName
).
setZone
(
zoneName
).
build
())
.
build
();
// Start a long-running operation (LRO).
OperationFuture<AnywhereCache
,
CreateAnywhereCacheMetadata
>
operation
=
storageControl
.
createAnywhereCacheAsync
(
request
);
// Await the LROs completion.
AnywhereCache
anywhereCache
=
operation
.
get
();
System
.
out
.
printf
(
"Created anywhere cache: %s%n"
,
anywhereCache
.
getName
());
}
}
}
PHP
For more information, see the Cloud Storage PHP API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
use Google\Cloud\Storage\Control\V2\AnywhereCache;
use Google\Cloud\Storage\Control\V2\Client\StorageControlClient;
use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest;
/**
* Creates an Anywhere Cache instance.
*
* @param string $bucketName The name of your Cloud Storage bucket.
* (e.g. 'my-bucket')
* @param string $zone The zone in which the cache instance is running.
* (e.g. 'us-east1-b')
*/
function create_anywhere_cache(string $bucketName, string $zone): void
{
$storageControlClient = new StorageControlClient();
// Set project to "_" to signify global bucket
$formattedName = $storageControlClient->bucketName('_', $bucketName);
$anywhereCache = new AnywhereCache([
'zone' => $zone,
]);
$request = new CreateAnywhereCacheRequest([
'parent' => $formattedName,
'anywhere_cache' => $anywhereCache,
]);
// Start a create operation and block until it completes. Real applications
// may want to setup a callback, wait on a coroutine, or poll until it
// completes.
$operation = $storageControlClient->createAnywhereCache($request);
printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName());
$operation->pollUntilComplete([
'totalPollTimeoutMillis' => 5400000,
'initialPollDelayMillis' => 1000, // Start with 1 second delay
'pollDelayMultiplier' => 2, // Double delay each time
'maxPollDelayMillis' => 60000, // Max 60 seconds delay between polls
]);
/** @var AnywhereCache $anywhereCacheResult */
$anywhereCacheResult = $operation->getResult();
printf('Created anywhere cache: %s' . PHP_EOL, $anywhereCacheResult->getName());
}
Ruby
For more information, see the Cloud Storage Ruby API reference documentation .
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries .
require
"google/cloud/storage/control"
# Creates a new Anywhere Cache for a specified bucket and waits for it
# to become active.
#
# This method initiates the creation of an Anywhere Cache in the given zone for
# the specified bucket. After sending the creation request, it polls the status
# of the cache with an exponential backoff strategy until the cache's state is
# "running". Progress and final status are printed to the console.
#
# @param bucket_name [String] The name of the bucket.
# @param zone [String] The zone where the Anywhere Cache instance should be
# located (e.g., "us-east1-b").
#
# @example
# create_anywhere_cache(
# bucket_name: "your-unique-bucket-name",
# zone: "us-east1-b"
# )
#
def
create_anywhere_cache
bucket_name
:,
zone
:
# Create a client object. The client can be reused for multiple calls.
storage_control_client
=
Google
::
Cloud
::
Storage
::
Control
.
storage_control
# Set project to "_" to signify global bucket
parent
=
"projects/_/buckets/
#{
bucket_name
}
"
name
=
"
#{
parent
}
/anywhereCaches/
#{
zone
}
"
anywhere_cache
=
Google
::
Cloud
::
Storage
::
Control
::
V2
::
AnywhereCache
.
new
(
name
:
name
,
zone
:
zone
)
# Create a request.
request
=
Google
::
Cloud
::
Storage
::
Control
::
V2
::
CreateAnywhereCacheRequest
.
new
(
parent
:
parent
,
anywhere_cache
:
anywhere_cache
)
# The request creates a new cache in the specified zone.
# The cache is created in the specified bucket.
begin
operation
=
storage_control_client
.
create_anywhere_cache
request
puts
"AnywhereCache operation created -
#{
operation
.
name
}
"
get_request
=
Google
::
Cloud
::
Storage
::
Control
::
V2
::
GetAnywhereCacheRequest
.
new
(
name
:
name
)
result
=
storage_control_client
.
get_anywhere_cache
get_request
min_delay
=
30
# 30 seconds
max_delay
=
900
# 15 minutes
while
result
.
state
& .
downcase
!=
"running"
unless
[
"paused"
,
"disabled"
,
"creating"
].
include?
result
.
state
& .
downcase
raise
Google
::
Cloud
::
Error
,
"AnywhereCache operation failed on the backend with state
#{
result
.
state
& .
downcase
}
."
end
puts
"Cache not running yet, current state is
#{
result
.
state
& .
downcase
}
. Retrying in
#{
min_delay
}
seconds."
sleep
min_delay
min_delay
=
[
min_delay
*
2
,
max_delay
].
min
# Exponential backoff with a max delay
result
=
storage_control_client
.
get_anywhere_cache
get_request
end
puts
"Successfully created anywhereCache -
#{
result
.
name
}
."
rescue
Google
::
Cloud
::
Error
=
>
e
puts
"Failed to create AnywhereCache. Error:
#{
e
.
message
}
"
end
end
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

