Creating a new object store
1. create an object store
curl -i -X POST \
https://api.fastly.com/resources/stores/object \
-H "Fastly-Key: $FASTLY_API_KEY" \
-d '{"name":"test-object-store"}'
{"id":"7rr7ojk2wzhys2qkmvmpda","name":"test-object-store","created_at":"2022-09-14T02:15:42.280Z","updated_at":"2022-09-14T02:15:42.280Z"}%
2. clone your service (Run the fastly command in the C@E project dir)
$ fastly service-version clone --version=1
SUCCESS: Cloned service 2ivbqnv5JRGzmXJN42eay9 version 1 to version 2
3. create a resource (associate the store with the service)
curl -i -X POST \
https://api.fastly.com/service/2ivbqnv5JRGzmXJN42eay9/version/2/resource \
-H "Fastly-Key: $FASTLY_API_KEY" \
-d "name=test-resource&resource_id=7rr7ojk2wzhys2qkmvmpda"
{"id":"7jn31eG07U0pKcO9BeSFW4","name":"test-resource","service_id":"2ivbqnv5JRGzmXJN42eay9","version":2,"created_at":"2022-09-14T03:05:42Z","updated_at":"2022-09-14T03:05:42Z","deleted_at":null,"resource_id":"7rr7ojk2wzhys2qkmvmpda","resource_type":"object-store","href":null}%
fastly service-version activate --version=2
SUCCESS: Activated service 2ivbqnv5JRGzmXJN42eay9 version 2
use fastly:: http:: { Method , StatusCode } ;
use fastly:: { ObjectStore , panic_with_status, mime, Error , Request , Response } ;
const RESOURCE_NAME : & str = "test-resource" ;
#[ fastly:: main]
fn main ( mut req : Request ) -> Result < Response , Error > {
let mut object_store = match ObjectStore :: open ( RESOURCE_NAME ) . unwrap_or_else ( |_| {
panic_with_status ! ( 501 , "cached_object_store API not available on this host" ) ;
} ) {
Some ( store) => store,
None => panic_with_status ! ( 501 , "Store {} not found" , RESOURCE_NAME ) ,
} ;
let key = req. get_path ( ) . to_owned ( ) ;
match * req. get_method ( ) {
Method :: GET => match object_store. lookup ( & key) ? {
Some ( value) => Ok ( Response :: from_body ( value) . with_content_type ( mime:: TEXT_PLAIN_UTF_8 ) ) ,
None => Ok ( Response :: from_status ( StatusCode :: NOT_FOUND ) ) ,
} ,
Method :: POST => {
object_store. insert ( & key, req. take_body ( ) ) ?;
Ok ( Response :: from_status ( StatusCode :: CREATED ) )
} ,
_ => Ok ( Response :: from_status ( StatusCode :: METHOD_NOT_ALLOWED ) ) ,
}
}