How to simulate file system operations using REST?

For some basic file system operations (e.g. ls and rm ), there are obvious analogs, but how would you implement more than just RESTful actions like cp or mv ?

How to answer the question How to implement a copied copy of a resource in REST? The preferred way to implement cp would be to include a getting resource, DELETing it and return it again with a new name.

But what if I need to do this effectively? For example, if the size of the resources will be huge? How can I eliminate the excessive transfer of the resource payload to the client and back to the source server?

Here is an illustration. I have a resource:

  /videos/my_videos/2-gigabyte-video.avi

and I want to copy it to a new resource:

  /videos/johns_videos/copied-2-gigabyte-video.avi

How can I implement copy, move, or other RESTful file systems? Or is there even the right way? Am I doing all this wrong?

+4
source share
6 answers

[... the preferred way to implement cp would include getting the resource, DELETING AND CANCELING with a new name.]

One of the problems with the above approach is the lack of atomicity and consistency. Since each of the operations (GET, DELETE and PUT) occurs via HTTP (which is essentially idle), the server cannot provide atomicity. For any reason, the client may interrupt after any step to the last step and which would leave the server with an inconsistent state according to its data.

Possible approach:

  • If resources are documents (I think they are in your case), I would consider using WebDAV .
  • If WebDAV is not an option,
    • create a controller object on the server to control copy and move operations, the client can POST for something like / videos / my _videos / [video_id] / copy
    • In the response, you can specify the URI for the copied resource in the lines:

HTTP / 1.1 201 Created

Content Type: video / x-msvideo

Location: / video / johns_videos / 8765

Note. I prefer to send the identifier back and work with resource identifiers, rather than something like

Location: / videos / johns _videos / copied-2-gigabyte-video.avi

The move operation is pretty similar, except that the server can accept the destination resource. Example:

http://example.com//videos/johns_videos/8765/move?destination=[destination]

You can extend the approach described above so that the server sends the Last-Modified tag to the client, and the client sends it along with its request. The server will only perform copy / move operations when this value remains unchanged. This will fix concurrency issues when changing a resource during copy / move operations.

+3
source

I do not believe that any of these answers are RESTful. Here is what I will do.

To copy:

 PUT /videos/johns_videos/copied-2-gigabyte-video.avi HOST: www.server.com Content-Location: /videos/johns_videos/2-gigabyte-video.avi [empty-body] 

Put the contents in the folder (/videos/johns_videos/2-gigabyte-video.avi) in (/videos/johns_videos/copied-2-gigabyte-video.avi).

The move will be a copy with deletion, in order to check the consistency between copy and deletion, you will need to use the version number provided to you in the PUT response.

 PUT /videos/johns_videos/copied-2-gigabyte-video.avi HOST: www.server.com Content-Location: /videos/johns_videos/2-gigabyte-video.avi [empty-body] 201 Created ETag: "3e32f5a1123afb12" (an md5 of the file) Location: /videos/johns_videos/copied-2-gigabyte-video.avi [empty-body] DELETE /videos/johns_videos/2-gigabyte-video.avi HOST: www.server.com If-Match: "3e32f5a1123afb12" [empty-body] 204 No Content [empty-body] 

Why is this RESTful?

  • Doesn't add โ€œmoveโ€ or โ€œcopyโ€ to a URI (which is an RPC)
  • It uses PUT (POST to add to the collection, the target URI is not fully known)
  • It does not use the commands sent (for example, XML instructions), which are RPC, not REST.
  • Lack of understanding of the underscore repository. The client does not care about hard / indirect links or write-based optimization and should never know about them.

Mike brown

+4
source

You can open a new service that accepts (POST) a simple XML document that describes what you want to do.

 <move> <target>/videos/my_videos/2-gigabyte-video.avi</target> <destination>/videos/johns_videos/copied-2-gigabyte-video.avi<destination> <move> 

Then this service can return the URI where the client can go and check the status of the operation. The client can then interact with this new resource to say, cancel the move, if it has not already been completed, or check its success.

+1
source

In my opinion, video is a resource. So this resource has a path. What if you use UPDATE, which change the path to the resource?

Then in your code, if it changes the path, you just need to move the file.

0
source

One way to do this is to formulate your PUT / POST requests so that you can either provide the actual data or provide a resource URL, possibly with the ability to make a hard or symbolic link. If this URL is hosted on your own system, you can simply point internally to the same file, possibly saving a bit for โ€œcopy-on-writeโ€ or something in that direction to make it efficient.

0
source

REST is not limited to HTTP! The best way is to use webdav for your problem.

0
source

Source: https://habr.com/ru/post/1310154/


All Articles