如何使用REST为文件系统操作建模? [英] How to model file system operations with REST?

查看:130
本文介绍了如何使用REST为文件系统操作建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些文件系统的基本操作(例如 ls rm )有明显的对应关系,但是你将如何实现不简单的RESTful行为,如 cp mv



作为问题的答案如何在REST中实现一个资源的复制粘贴?建议,实现 cp 的首选方式包括获取资源,删除它并将其重新放回一个新的名称。



但是如果我需要有效地做到这一点呢?例如,如果资源的规模会很大?我将如何消除资源有效负载的多余传输到客户端并返回到原始服务器?



下面是一个例子。我有一个资源:

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

我想把它复制到一个新的资源中:

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

如何以RESTful方式执行复制,移动或其他文件系统操作?还是有一个正确的方法?我做这一切都是错的?

解决方案


[...实现$ b的首选方法$ b cp将包括获取资源,
删除它并将其重新放回
并添加一个新名称。]



上述方法的一个问题是缺乏原子性和一致性。由于每个操作(GET,DELETE和PUT)都是通过HTTP(本质上是无状态的)发生的,所以服务器不能执行原子性。出于任何原因,客户端可能会在最后一步之前的任何步骤之后中止,这将使服务器的数据状态不一致。

可能的方法:


  • 如果资源是文档(我猜这是你的情况),我会探索选项使用 WebDAV

  • 如果WebDAV不是一个选项 -


    • 在服务器上创建一个控制器对象来管理复制和移动操作,客户端可以POST到/ videos / my_videos / [video_id] / copy>

    • 在您的回复中,您可以指定复制资源的URI,如下所示:




HTTP / 1.1 201已创建

内容类型:video / x -msvideo



位置:/ videos / johns_videos / 8765

注意:我是发送一个ID,并使用资源ID而不是类似的东西


位置:
/ videos / johns_videos / copied-2 -gigabyte-video.avi

除非服务器可以接受目标资源,否则移动操作非常相似。示例:


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


您可以扩展上述方法,以便服务器向客户端发送Last-Modified标记,客户端将其发送请求发送给客户端。只有当该值仍然一致时,服务器才会执行复制/移动操作。这将解决您的复制/移动操作仍在进行时正在更改的资源的并发问题。

There are obvious counterparts for some of file systems' basic operations (eg. ls and rm), but how would you implement not straightforwardly RESTful actions such as cp or mv?

As answers to the question How to implement copy paste of a resource in REST? suggest, the preferred way of implementing cp would include GETting the resource, DELETing it and PUTting it back again with a new name.

But what if I would need to do it efficiently? For instance, if the resource's size would be huge? How would I eliminate the superfluous transmission of resource's payload to client and back to the originating server?

Here is an illustration. I have a resource:

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

and I want copy it into a new resource:

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

How would I implement the copy, move or other file system actions the RESTful way? Or is there even a proper way? Am I doing it all wrong?

解决方案

[...the preferred way of implementing cp would include GETting the resource, DELETing it and PUTting it back again with a new name.]

One problem with the above approach is lack of atomicity and consistency. Since each of the operations (GET, DELETE and PUT) happen over HTTP (which is stateless inherently) the server cannot enforce atomicity. For any reason, the client may abort after any step before the last step and that would leave the server with an inconsistent state in terms of its data.

A possible approach:

  • If the resources are documents (which I guess, they are in your case) I'd explore the option of using WebDAV.
  • If WebDAV is not an option --
    • create a controller object on the server to manage copy and move operations, client can POST to something like /videos/my_videos/[video_id]/copy
    • In your response you can specify the URI to the copied resource, in the lines of:

HTTP/1.1 201 Created

Content-type:video/x-msvideo

Location:/videos/johns_videos/8765

Note: I prefer sending an ID back and working with resource IDs rather than something like

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

Move operation is pretty similar except that the server may accept a destination resource. Example:

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

You can extend the above approach such that the server sends a Last-Modified tag to the client and client sends that along with its request. The server will perform the copy/move operations only when that value is still consistent. This will address concurrency issues with the resource being changed while your copy/move operations are still in progress.

这篇关于如何使用REST为文件系统操作建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆