Message Boards Message Boards

[✓]  Refresh or clear cached CloudObjects[]?

GROUPS:

cross posted on mathematica.stackexchange.com

Let's create an API which creates one file each time it is called/accessed:

obj = CloudDeploy[
  APIFunction[{}
  , Put[$RequesterWolframID
    , "testFiles/" <> DateString["ISODateTime"] <> ".txt"
    ] &
  ]
, "apiTest"
, Permissions -> "Public"
];

CloudEvaluate@CreateDirectory@"testFiles/";

and let's call it:

URLRead[obj]
CloudObjects["testFiles/"]
URLRead[obj]
CloudObjects["testFiles/"]

enter image description here

But this is not true, if you visit a browser interface they will be there:

enter image description here

In fact, once you visit it, CloudObjects cache changes, but only once:

URLRead[obj]
CloudObjects["testFiles/"]
URLRead[obj]
CloudObjects["testFiles/"]

enter image description here

So how to make sure CloudObjects returns up to date result?

I am fine with an additional step to flush the cache, I just want it to be possible programmatically.

POSTED BY: Kuba Podkalicki
Answer
4 months ago

When you use Put with a string as the location it just creates a regular file, not a CloudObject. What's happening is that viewing files with the cloud file manager is automatically generating the CloudObject metadata for those files. You'll want to change your code to either Put to a CloudObject explicitly, or preferably just use CloudPut:

obj = CloudDeploy[
  APIFunction[{}
  , CloudPut[$RequesterWolframID
    , "testFiles/" <> DateString["ISODateTime"] <> ".txt"
    ] &
  ]
, "apiTest"
, Permissions -> "Public"
];

Also, there's no need to create the target directory when going directly to cloud objects, since it will be created automatically (as a CloudObject) if necessary.

POSTED BY: Richard Hennigan
Answer
4 months ago

Thanks for your answer, it is a good solution for this case. Can I ask you one further question?

The real use case is based on OpenAppend and after having problems with auto creating file or directory I just do

If[Not @ FileExistsQ @ #, CreateFile @ #]& @ logFile

I only need CloudObjects to know the file is there, metadata can be out of date. What would be the fastest way to CreateFile which is found by CloudObjects? CloudPut["init", logFile]?

POSTED BY: Kuba Podkalicki
Answer
4 months ago

PutAppend supports cloud objects, by the way. You can try that, it should be better performing than having to check for the object existing prior to every write.

But yes, a CloudPut of a tiny expression is probably the fastest way to create a cloud object. (You can include the IconRules -> {} option setting, the icon is generated asynchronously so it won't improve your latency if this is done infrequently, but if you really are hammering the system, this will reduce the overall load on the system and may help some.)

POSTED BY: Joel Klein
Answer
4 months ago

Thanks for feedback, will use IconRules just in case. Would be nice to be able to read more about cloud objects 'configuration' step, what is involved for different types of them etc.

p.s. I do not check for every write but for every OpenAppend.

POSTED BY: Kuba Podkalicki
Answer
4 months ago

Group Abstract Group Abstract